Skip to content

Commit 2ec05cb

Browse files
committed
Merge branch 'master' into IPC
2 parents 9b1cc13 + 8e702b6 commit 2ec05cb

File tree

7 files changed

+167
-64
lines changed

7 files changed

+167
-64
lines changed

package-lock.json

Lines changed: 57 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tf2autobot",
3-
"version": "5.9.1",
3+
"version": "5.10.0",
44
"description": "Fully automated TF2 trading bot advertising on www.backpack.tf using prices from www.prices.tf, Originally made by Nicklason.",
55
"main": "dist/app.js",
66
"scripts": {
@@ -33,7 +33,7 @@
3333
"@tf2autobot/steamcommunity": "^3.47.2",
3434
"@tf2autobot/tf2": "^1.3.5",
3535
"@tf2autobot/tf2-currencies": "^2.0.1",
36-
"@tf2autobot/tf2-schema": "^4.3.0",
36+
"@tf2autobot/tf2-schema": "^4.3.1",
3737
"@tf2autobot/tf2-sku": "^2.0.4",
3838
"@tf2autobot/tradeoffer-manager": "^2.15.2",
3939
"async": "^3.2.5",

src/app.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ if (process.env.DOCKER !== undefined) {
5757
);
5858
}
5959

60+
import SchemaManager from '@tf2autobot/tf2-schema';
61+
import { apiRequest } from './lib/apiRequest';
62+
63+
// Make the schema manager request the schema from schema.autobot.tf
64+
65+
/*eslint-disable */
66+
SchemaManager.prototype.getSchema = function (callback): void {
67+
apiRequest('GET', 'https://schema.autobot.tf/schema')
68+
.then(schema => {
69+
this.setSchema(schema, true);
70+
callback(null, this.schema);
71+
})
72+
.catch(err => {
73+
callback(err);
74+
});
75+
};
76+
/*eslint-enable */
77+
6078
import BotManager from './classes/BotManager';
6179
const botManager = new BotManager(
6280
getPricer({

src/classes/Commands/functions/utils.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,9 @@ export function getItemAndAmount(
1818
prefix: string,
1919
from?: 'buy' | 'sell' | 'buycart' | 'sellcart'
2020
): { match: Entry; priceKey: string; amount: number } | null {
21-
let name = removeLinkProtocol(message);
22-
let amount = 1;
23-
const args = name.split(' ');
24-
if (/^[-]?\d+$/.test(args[0]) && args.length > 2) {
25-
// Check if the first part of the name is a number, if so, then that is the amount the user wants to trade
26-
amount = parseInt(args[0]);
27-
name = name.replace(amount.toString(), '').trim();
28-
} else if (Pricelist.isAssetId(args[0]) && args.length === 1) {
29-
// Check if the only parameter is an assetid
30-
name = args[0];
31-
}
32-
33-
if (1 > amount) {
34-
amount = 1;
35-
}
21+
const parsedMessage = parseItemAndAmountFromMessage(message);
22+
const name = parsedMessage.name;
23+
let amount = parsedMessage.amount;
3624

3725
if (['!price', '!sellcart', '!buycart', '!sell', '!buy', '!pc', '!s', '!b'].includes(name)) {
3826
bot.sendMessage(
@@ -210,6 +198,26 @@ export function getItemAndAmount(
210198
};
211199
}
212200

201+
export function parseItemAndAmountFromMessage(message: string): { name: string; amount: number } {
202+
let name = removeLinkProtocol(message);
203+
let amount = 1;
204+
const args = name.split(' ');
205+
if (/^[-]?\d+$/.test(args[0]) && args.length > 1) {
206+
// Check if the first part of the name is a number, if so, then that is the amount the user wants to trade
207+
amount = parseInt(args[0]);
208+
name = name.replace(amount.toString(), '').trim();
209+
} else if (Pricelist.isAssetId(args[0]) && args.length === 1) {
210+
// Check if the only parameter is an assetid
211+
name = args[0];
212+
}
213+
214+
if (1 > amount) {
215+
amount = 1;
216+
}
217+
218+
return { name: name, amount: amount };
219+
}
220+
213221
export function getItemFromParams(
214222
steamID: SteamID | string,
215223
params: UnknownDictionaryKnownValues,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { parseItemAndAmountFromMessage } from '../../../Commands/functions/utils';
2+
3+
it('can parse one word item names', () => {
4+
let messageArgs = '5 Maul';
5+
let parsedMessage = parseItemAndAmountFromMessage(messageArgs);
6+
expect(parsedMessage).toEqual({ name: 'Maul', amount: 5 });
7+
8+
messageArgs = 'Maul';
9+
parsedMessage = parseItemAndAmountFromMessage(messageArgs);
10+
expect(parsedMessage).toEqual({ name: 'Maul', amount: 1 });
11+
});
12+
13+
it('can parse multiple word item names', () => {
14+
let messageArgs = '5 Nostromo Napalmer';
15+
let parsedMessage = parseItemAndAmountFromMessage(messageArgs);
16+
expect(parsedMessage).toEqual({ name: 'Nostromo Napalmer', amount: 5 });
17+
18+
messageArgs = 'Nostromo Napalmer';
19+
parsedMessage = parseItemAndAmountFromMessage(messageArgs);
20+
expect(parsedMessage).toEqual({ name: 'Nostromo Napalmer', amount: 1 });
21+
});

src/lib/apiRequest.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import axios, { AxiosRequestConfig, Method, AxiosError } from 'axios';
2+
import filterAxiosError from '@tf2autobot/filter-axios-error';
3+
4+
export async function apiRequest<B>(
5+
httpMethod: string,
6+
url: string,
7+
params?: Record<string, any>,
8+
data?: Record<string, any>,
9+
headers?: Record<string, unknown>
10+
): Promise<B> {
11+
if (!headers) {
12+
headers = {};
13+
}
14+
15+
const options: AxiosRequestConfig = {
16+
method: httpMethod as Method,
17+
url,
18+
headers: {
19+
'User-Agent': 'TF2Autobot@' + process.env.BOT_VERSION,
20+
...headers
21+
},
22+
timeout: 30000
23+
};
24+
25+
if (params) {
26+
options.params = params;
27+
}
28+
29+
if (data) {
30+
options.data = data;
31+
}
32+
33+
return new Promise((resolve, reject) => {
34+
void axios(options)
35+
.then(response => {
36+
const body = response.data as B;
37+
resolve(body);
38+
})
39+
.catch((err: AxiosError) => {
40+
if (err) {
41+
reject(filterAxiosError(err));
42+
}
43+
});
44+
});
45+
}

src/lib/extend/item/getSKU.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ function getCraftNumber(item: EconItem, schema: Schema, normalizeCraftNumber: bo
506506
return null;
507507
}
508508

509-
const name = item.market_hash_name;
509+
const name = item.name;
510510
const withoutNumber = name.replace(/#\d+/, '');
511511
if (name === withoutNumber) {
512512
// no change

0 commit comments

Comments
 (0)