Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare v5.1.0 #1233

Merged
merged 23 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
dce09f9
🔄 bump @tf2autobot/filter-axios-error
idinium96 Jul 17, 2022
632793b
🔄 bump @tf2autobot/bptf-listings
idinium96 Jul 17, 2022
93c639a
✅ properly handle inventory fetch
idinium96 Jul 17, 2022
93b3716
🔉 add some debugging log
idinium96 Jul 18, 2022
9549e57
🔄 should be here
idinium96 Jul 18, 2022
7dc6a71
🔨 fix typo
idinium96 Jul 18, 2022
833eeef
✅ Don't fetch inventory if still active
idinium96 Jul 18, 2022
39b4a27
✅ Also don't fetch on Countered or Declined Countered
idinium96 Jul 18, 2022
6dbf874
🔀 Merge pull request #1228 from TF2Autobot/fetch-inventory
idinium96 Jul 18, 2022
bb40635
💯 allow Discord bot to chat with non-admin
idinium96 Jul 18, 2022
0025d5e
🛑 ignore message if not starts with !
idinium96 Jul 18, 2022
3f057fe
🔀 Merge pull request #1234 from TF2Autobot/discord-allow-non-admin
idinium96 Jul 18, 2022
17ed364
🛑 throw an error on reptf invalid response message
idinium96 Jul 18, 2022
84ba0f5
🚧 just in case
idinium96 Jul 18, 2022
a056bdd
🔄 separate mptf ban
idinium96 Jul 18, 2022
f043dee
🚮 remove unrelated type
idinium96 Jul 18, 2022
f82f070
🔀 Merge pull request #1235 from TF2Autobot/reptf-throw-if-failed
idinium96 Jul 18, 2022
730247f
✨ add `!links` command
idinium96 Jul 18, 2022
99b74f3
🔀 Merge pull request #1236 from TF2Autobot/links-command
idinium96 Jul 18, 2022
32910ec
🔄 update `!stock` command - include assetids
idinium96 Jul 19, 2022
8ebe474
🔀 Merge pull request #1237 from TF2Autobot/update-stock-command
idinium96 Jul 19, 2022
a39cf2c
🚮 remove `!message` command (Discord)
idinium96 Jul 19, 2022
483dbbe
🔨 refactor try-catch duplication
idinium96 Jul 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"updateMessage": "Bug fixed",
"homepage": "https://github.com/TF2Autobot/tf2autobot#readme",
"dependencies": {
"@tf2autobot/bptf-listings": "^5.6.1",
"@tf2autobot/bptf-listings": "^5.6.2",
"@tf2autobot/bptf-login": "^2.3.3",
"@tf2autobot/filter-axios-error": "^1.3.0",
"@tf2autobot/filter-axios-error": "^1.3.1",
"@tf2autobot/jsonlint": "^1.0.0",
"@tf2autobot/steamcommunity": "^3.45.3",
"@tf2autobot/tf2": "^1.2.1",
Expand Down
6 changes: 3 additions & 3 deletions src/classes/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,9 +1239,6 @@ export default class Bot {
}

sendMessage(steamID: SteamID | string, message: string): void {
const steamID64 = steamID.toString();
const friend = this.friends.getFriend(steamID64);

if (steamID instanceof SteamID && steamID.redirectAnswerTo) {
const origMessage = steamID.redirectAnswerTo;
if (origMessage instanceof DiscordMessage) {
Expand All @@ -1252,6 +1249,9 @@ export default class Bot {
return;
}

const steamID64 = steamID.toString();
const friend = this.friends.getFriend(steamID64);

if (!friend) {
// If not friend, we send message with chatMessage
this.client.chatMessage(steamID, message);
Expand Down
40 changes: 36 additions & 4 deletions src/classes/Commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ export default class Commands {
const command = CommandParser.getCommand(message.toLowerCase());
const isAdmin = this.bot.isAdmin(steamID);
const isWhitelisted = this.bot.isWhitelisted(steamID);
const isInvalidType = steamID.type === 0;

const checkMessage = message.split(' ').filter(word => word.includes(`!${command}`)).length;

if (checkMessage > 1 && !isAdmin) {
return this.bot.sendMessage(steamID, "⛔ Don't spam");
}

log.debug('Read processMessage');

if (message.startsWith('!')) {
if (command === 'help') {
void this.help.helpCommand(steamID);
Expand All @@ -112,34 +115,65 @@ export default class Commands {
} else if (['price', 'pc'].includes(command)) {
this.priceCommand(steamID, message);
} else if (['buy', 'b', 'sell', 's'].includes(command)) {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.buyOrSellCommand(steamID, message, command as Instant);
} else if (command === 'buycart') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.buyCartCommand(steamID, message);
} else if (command === 'sellcart') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.sellCartCommand(steamID, message);
} else if (command === 'cart') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.cartCommand(steamID);
} else if (command === 'clearcart') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.clearCartCommand(steamID);
} else if (command === 'checkout') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.checkoutCommand(steamID);
} else if (command === 'cancel') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.cancelCommand(steamID);
} else if (command === 'queue') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.queueCommand(steamID);
} else if (['time', 'uptime', 'pure', 'rate', 'owner', 'discord', 'stock'].includes(command)) {
if (command === 'stock') {
return this.misc.miscCommand(steamID, command as Misc, message);
}
this.misc.miscCommand(steamID, command as Misc);
} else if (['link', 'links'].includes(command)) {
this.misc.links(steamID);
} else if (command === 'sku') {
this.getSKU(steamID, message);
} else if (command === 'message') {
if (isInvalidType) {
return this.bot.sendMessage(steamID, '❌ Command not available.');
}
this.message.message(steamID, message);
} else if (command === 'paints' && isAdmin) {
this.misc.paintsCommand(steamID);
} else if (command === 'more') {
this.help.moreCommand(steamID);
} else if (command === 'autokeys') {
this.manager.autokeysCommand(steamID);
} else if (command === 'message') {
this.message.message(steamID, message);
} else if (['craftweapon', 'craftweapons', 'uncraftweapon', 'uncraftweapons'].includes(command)) {
void this.misc.weaponCommand(
steamID,
Expand Down Expand Up @@ -247,8 +281,6 @@ export default class Commands {
this.donateCartCommand(steamID);
} else if (command === 'premium' && isAdmin) {
this.buyBPTFPremiumCommand(steamID, message);
} else if (command === 'sku' && isAdmin) {
this.getSKU(steamID, message);
} else if (command === 'refreshschema' && isAdmin) {
this.manager.refreshSchema(steamID);
} else if (command === 'crafttoken' && isAdmin) {
Expand Down
19 changes: 19 additions & 0 deletions src/classes/Commands/sub-classes/Help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ export default class HelpCommands {
const isAdmin = this.bot.isAdmin(steamID);
const isCustomPricer = this.bot.pricelist.isUseCustomPricer;

if (steamID instanceof SteamID && steamID.redirectAnswerTo && steamID.type === 0) {
return this.bot.sendMessage(
steamID,
`\nDo not include characters <> nor [ ] - <> means required and [] means optional.` +
"\n\n📜 Here's a list of my commands:" +
'\n- ' +
[
'!help - Get a list of commands.',
'!how2trade - Guide on how to trade with the bot.',
"!links - Links to the bot's Steam, Backpack.tf, and Rep.tf.",
'!price [amount] <name> - Get the price and stock of an item.',
"!sku <Full Item Name|Item's sku> - Get the sku of an item.",
"!owner - Get the owner's Steam profile and Backpack.tf links.",
"!discord - Get a link to join TF2Autobot and/or the owner's discord server.",
'!more - Show more available commands list.'
].join('\n- ')
);
}

this.bot.sendMessage(
steamID,
`📌 Note 📌${
Expand Down
90 changes: 72 additions & 18 deletions src/classes/Commands/sub-classes/Misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import CommandParser from '../../CommandParser';
import Bot from '../../Bot';
import { Discord, Stock } from '../../Options';
import { pure, timeNow, uptime, testPriceKey } from '../../../lib/tools/export';
import { killstreakersData, sheensData, spellsData } from '../../../lib/data';
import { Paints, StrangeParts } from '@tf2autobot/tf2-schema';

type Misc = 'time' | 'uptime' | 'pure' | 'rate' | 'owner' | 'discord' | 'stock';
type CraftUncraft = 'craftweapon' | 'uncraftweapon';
Expand All @@ -17,6 +19,17 @@ export default class MiscCommands {
this.bot = bot;
}

links(SteamID: SteamID): void {
const botSteamID = this.bot.client.steamID.getSteamID64();

this.bot.sendMessage(
SteamID,
`Steam: <https://steamcommunity.com/profiles/${botSteamID}>` +
`\nBackpack.tf: <https://backpack.tf/u/${botSteamID}>` +
`\nRep.tf: <https://rep.tf/${botSteamID}>`
);
}

miscCommand(steamID: SteamID, command: Misc, message?: string): void {
const opt = this.bot.options.commands[command];
if (!opt.enable) {
Expand Down Expand Up @@ -98,31 +111,60 @@ export default class MiscCommands {
} else {
const itemNameOrSku = CommandParser.removeCommand(removeLinkProtocol(message));
let reply = '';
let isWithSomething = false;

if (itemNameOrSku !== '!sku') {
if (!testPriceKey(itemNameOrSku)) {
// Receive name
const sku = this.bot.schema.getSkuFromName(itemNameOrSku);
if (itemNameOrSku !== '!stock') {
// I don't remember why not `!sku` here.
let sku: string = itemNameOrSku;
if (itemNameOrSku !== '!stock') {
if (!testPriceKey(itemNameOrSku)) {
// Receive name
sku = this.bot.schema.getSkuFromName(itemNameOrSku);

if (sku.includes('null') || sku.includes('undefined')) {
reply = `/pre Generated sku: ${sku}\nPlease check the name. If correct, please let us know. Thank you.`;
isWithSomething = true;
} else {
const assetids = this.bot.inventoryManager.getInventory.findBySKU(sku);
reply = `/pre I currently have ${assetids.length} of ${itemNameOrSku} (${sku}).`;
isWithSomething = true;
return this.bot.sendMessage(
steamID,
`/pre ❌ Generated sku: ${sku}\nPlease check the name. If correct, please let us know. Thank you.`
);
}
}
} else {
// Receive sku
const assetids = this.bot.inventoryManager.getInventory.findBySKU(itemNameOrSku);
const name = this.bot.schema.getName(SKU.fromString(itemNameOrSku), false);

reply = `/pre I currently have ${assetids.length} of ${name} (${itemNameOrSku}).`;
isWithSomething = true;
const itemDicts = this.bot.inventoryManager.getInventory.getItems[sku] ?? [];
const name = this.bot.schema.getName(SKU.fromString(sku), false);

reply = `/pre I currently have ${itemDicts.length} of ${name} (${sku}).`;

const assetids: string[] = [];
if (itemDicts.length > 0) {
const hv: string[] = [];
itemDicts.forEach(item => {
if (item.hv) {
Object.keys(item.hv).forEach(attachment => {
for (const pSku in item.hv[attachment]) {
if (!Object.prototype.hasOwnProperty.call(item.hv[attachment], pSku)) {
continue;
}

const hvName = getAttachmentName(
attachment,
pSku,
this.bot.schema.paints,
this.bot.strangeParts
);

hv.push(hvName);
}
});
}

assetids.push(`${item.id}${hv.length > 0 ? ` (${hv.join(', ')})` : ''}`);
});
}

reply += assetids.length > 0 ? '\n\nAssetids:\n- ' + assetids.join('\n- ') : '';
return this.bot.sendMessage(steamID, reply);
}
}

const inventory = this.bot.inventoryManager.getInventory;
const dict = inventory.getItems;
const items: { amount: number; name: string }[] = [];
Expand Down Expand Up @@ -196,7 +238,7 @@ export default class MiscCommands {
reply += custom
? custom.replace(/%stocklist%/g, stock.join(', \n'))
: `${
isWithSomething ? '\n\n' : steamID.redirectAnswerTo instanceof DiscordMessage ? '/pre2' : '/pre '
steamID.redirectAnswerTo instanceof DiscordMessage ? '/pre2' : '/pre '
}📜 Here's a list of all the items that I have in my inventory:\n${stock.join(', \n')}`;

if (left > 0) {
Expand Down Expand Up @@ -332,3 +374,15 @@ export default class MiscCommands {
return stock;
}
}

function getKeyByValue(object: { [key: string]: any }, value: any): string {
return Object.keys(object).find(key => object[key] === value);
}

function getAttachmentName(attachment: string, pSku: string, paints: Paints, parts: StrangeParts): string {
if (attachment === 's') return getKeyByValue(spellsData, pSku);
else if (attachment === 'sp') return getKeyByValue(parts, pSku);
else if (attachment === 'ke') return getKeyByValue(killstreakersData, pSku);
else if (attachment === 'ks') return getKeyByValue(sheensData, pSku);
else if (attachment === 'p') return getKeyByValue(paints, parseInt(pSku.replace('p', '')));
}
14 changes: 11 additions & 3 deletions src/classes/DiscordBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,27 @@ export default class DiscordBot {
return; // Ignore webhook messages
}

if (!message.content.startsWith('!')) {
return; // Ignore message that not start with !
}

log.info(
`Got new message ${String(message.content)} from ${message.author.tag} (${String(message.author.id)})`
);
if (!this.isDiscordAdmin(message.author.id)) {
return; // obey only admins
}

if (!this.bot.isReady) {
this.sendAnswer(message, '🛑 The bot is still booting up, please wait');
return;
}

try {
if (!this.isDiscordAdmin(message.author.id)) {
// Will return default invalid value
const dummySteamID = new SteamID(null);
dummySteamID.redirectAnswerTo = message;
return await this.bot.handler.onMessage(dummySteamID, message.content);
}

const adminID = this.getAdminBy(message.author.id);
adminID.redirectAnswerTo = message;
await this.bot.handler.onMessage(adminID, message.content);
Expand Down
Loading