Skip to content

Commit

Permalink
feat: created inventory commands (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolfx committed Jan 18, 2023
1 parent cce6ee4 commit f6596f1
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/classes/Commands/commands/inventory/Autokeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SteamID from 'steamid';
import CommandHandler, { ICommand } from '../../CommandHandler';
import Bot from '../../../Bot';
import IPricer from '../../../IPricer';

export default class AutokeysCommand implements ICommand {
name = 'autokeys';

description = "Get info on the bot's current autokeys settings 🔑";

isAdminCommand = true;

constructor(
public readonly bot: Bot,
public readonly pricer: IPricer,
public readonly commandHandler: CommandHandler
) {
this.bot = bot;
this.pricer = pricer;
this.commandHandler = commandHandler;
}

// I don't like this approach, and better to remove this aliases and turn them into specific commands
// In their own classes
execute = (steamID: SteamID, message: string, command) => {
this.commandHandler.manager.autokeysCommand(steamID);
};
}
37 changes: 37 additions & 0 deletions src/classes/Commands/commands/inventory/Crafting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import SteamID from 'steamid';
import CommandHandler, { CraftUncraft, ICommand } from '../../CommandHandler';
import Bot from '../../../Bot';
import IPricer from '../../../IPricer';

export default class CraftingCommand implements ICommand {
name = 'craftweapon';

aliases = ['craftweapons', 'uncraftweapon', 'uncraftweapons'];

description = '';

isAdminCommand = true;

constructor(
public readonly bot: Bot,
public readonly pricer: IPricer,
public readonly commandHandler: CommandHandler
) {
this.bot = bot;
this.pricer = pricer;
this.commandHandler = commandHandler;
}

// I don't like this approach, and better to remove this aliases and turn them into specific commands
// In their own classes
execute = (steamID: SteamID, message: string, command) => {
void this.commandHandler.misc.weaponCommand(
steamID,
command === 'craftweapons'
? 'craftweapon'
: command === 'uncraftweapons'
? 'uncraftweapon'
: (command as CraftUncraft)
);
};
}
141 changes: 141 additions & 0 deletions src/classes/Commands/commands/inventory/Deposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import SteamID from 'steamid';
import SKU from '@tf2autobot/tf2-sku';
import CommandHandler, { ICommand } from '../../CommandHandler';
import Bot from '../../../Bot';
import IPricer from '../../../IPricer';
import Cart from '../../../Carts/Cart';
import AdminCart from '../../../Carts/AdminCart';
import CommandParser from '../../../CommandParser';
import { getItemFromParams, removeLinkProtocol } from '../../functions/utils';
import { fixItem } from '../../../../lib/items';
import Inventory from '../../../Inventory';
import log from '../../../../lib/logger';
import pluralize from 'pluralize';

export default class DepositCommand implements ICommand {
name = 'deposit';

aliases = ['d'];

description = '';

isAdminCommand = true;

constructor(
public readonly bot: Bot,
public readonly pricer: IPricer,
public readonly commandHandler: CommandHandler
) {
this.bot = bot;
this.pricer = pricer;
this.commandHandler = commandHandler;
}

// I don't like this approach, and better to remove this aliases and turn them into specific commands
// In their own classes
execute = async (steamID: SteamID, message: string) => {
const prefix = this.bot.getPrefix(steamID);
const currentCart = Cart.getCart(steamID);
if (currentCart !== null && !(currentCart instanceof AdminCart)) {
return this.bot.sendMessage(
steamID,
'❌ You already have an active cart, please finalize it before making a new one. 🛒'
);
}

const params = CommandParser.parseParams(CommandParser.removeCommand(removeLinkProtocol(message)));
if (params.sku === undefined) {
const item = getItemFromParams(steamID, params, this.bot);
if (item === null) {
return;
}

params.sku = SKU.fromObject(item);
} else {
params.sku = SKU.fromObject(fixItem(SKU.fromString(params.sku as string), this.bot.schema));
}

const sku = params.sku as string;

const amount = typeof params.amount === 'number' ? params.amount : 1;
if (!Number.isInteger(amount)) {
return this.bot.sendMessage(steamID, `❌ amount should only be an integer.`);
}

const itemName = this.bot.schema.getName(SKU.fromString(sku), false);

const steamid = steamID.getSteamID64();

const adminInventory =
this.commandHandler.adminInventory[steamid] ||
new Inventory(steamID, this.bot, 'their', this.bot.boundInventoryGetter);

if (this.commandHandler.adminInventory[steamid] === undefined) {
try {
log.debug('fetching admin inventory');
await adminInventory.fetch();
this.commandHandler.adminInventory[steamid] = adminInventory;

clearTimeout(this.commandHandler.adminInventoryReset);
this.commandHandler.adminInventoryReset = setTimeout(() => {
delete this.commandHandler.adminInventory[steamid];
}, 5 * 60 * 1000);
} catch (err) {
log.error('Error fetching inventory: ', err);
return this.bot.sendMessage(
steamID,
`❌ Error fetching inventory, steam might down. Please try again later. ` +
`If you have private profile/inventory, please set to public and try again.`
);
}
}

const dict = adminInventory.getItems;

if (dict[params.sku as string] === undefined) {
clearTimeout(this.commandHandler.adminInventoryReset);
delete this.commandHandler.adminInventory[steamid];
return this.bot.sendMessage(steamID, `❌ You don't have any ${itemName}.`);
}

const currentAmount = dict[params.sku as string].length;
if (currentAmount < amount) {
clearTimeout(this.commandHandler.adminInventoryReset);
delete this.commandHandler.adminInventory[steamid];
return this.bot.sendMessage(steamID, `❌ You only have ${pluralize(itemName, currentAmount, true)}.`);
}

const cart =
AdminCart.getCart(steamID) ||
new AdminCart(
steamID,
this.bot,
this.commandHandler.weaponsAsCurrency.enable ? this.bot.craftWeapons : [],
this.commandHandler.weaponsAsCurrency.enable && this.commandHandler.weaponsAsCurrency.withUncraft
? this.bot.uncraftWeapons
: []
);

if (amount > 0) {
const cartAmount = cart.getTheirCount(sku);

if (cartAmount > currentAmount || cartAmount + amount > currentAmount) {
return this.bot.sendMessage(
steamID,
`❌ You can't add ${pluralize(itemName, amount, true)} ` +
`because you already have ${cartAmount} in cart and you only have ${currentAmount}.`
);
}
}

cart.addTheirItem(sku, amount);
Cart.addCart(cart);

this.bot.sendMessage(
steamID,
`✅ ${pluralize(itemName, Math.abs(amount), true)} has been ` +
(amount >= 0 ? 'added to' : 'removed from') +
` your cart. Type "${prefix}cart" to view your cart summary or "${prefix}checkout" to checkout. 🛒`
);
};
}
28 changes: 28 additions & 0 deletions src/classes/Commands/commands/inventory/Paints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SteamID from 'steamid';
import CommandHandler, { ICommand } from '../../CommandHandler';
import Bot from '../../../Bot';
import IPricer from '../../../IPricer';

export default class PaintsCommand implements ICommand {
name = 'paints';

description = 'Get a list of paints partial sku 🎨';

isAdminCommand = true;

constructor(
public readonly bot: Bot,
public readonly pricer: IPricer,
public readonly commandHandler: CommandHandler
) {
this.bot = bot;
this.pricer = pricer;
this.commandHandler = commandHandler;
}

// I don't like this approach, and better to remove this aliases and turn them into specific commands
// In their own classes
execute = (steamID: SteamID, message: string, command) => {
this.commandHandler.misc.paintsCommand(steamID);
};
}
71 changes: 71 additions & 0 deletions src/classes/Commands/commands/inventory/Sku.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import SteamID from 'steamid';
import SKU from '@tf2autobot/tf2-sku';
import CommandHandler, { ICommand } from '../../CommandHandler';
import Bot from '../../../Bot';
import IPricer from '../../../IPricer';
import CommandParser from '../../../CommandParser';
import { removeLinkProtocol } from '../../functions/utils';
import testPriceKey from '../../../../lib/tools/testPriceKey';

export default class SkuCommand implements ICommand {
name = 'sku';

description = 'Get the sku of an item.';

usage = `<Full Item Name|Item's sku>`;

constructor(
public readonly bot: Bot,
public readonly pricer: IPricer,
public readonly commandHandler: CommandHandler
) {
this.bot = bot;
this.pricer = pricer;
this.commandHandler = commandHandler;
}

execute = (steamID: SteamID, message: string) => {
const itemNamesOrSkus = CommandParser.removeCommand(removeLinkProtocol(message));

if (itemNamesOrSkus === '!sku') {
return this.bot.sendMessage(steamID, `❌ Missing item name or item sku!`);
}

const itemsOrSkus = itemNamesOrSkus.split('\n');

if (itemsOrSkus.length === 1) {
if (!testPriceKey(itemNamesOrSkus)) {
// Receive name
const sku = this.bot.schema.getSkuFromName(itemNamesOrSkus);

if (sku.includes('null') || sku.includes('undefined')) {
return this.bot.sendMessage(
steamID,
`Generated sku: ${sku}\nPlease check the name. If correct, please let us know. Thank you.`
);
}

this.bot.sendMessage(steamID, `• ${sku}\nhttps://autobot.tf/items/${sku}`);
} else {
// Receive sku
const name = this.bot.schema.getName(SKU.fromString(itemNamesOrSkus), false);
this.bot.sendMessage(steamID, `• ${name}\nhttps://autobot.tf/items/${itemNamesOrSkus}`);
}
} else {
const results: { source: string; generated: string }[] = [];
itemsOrSkus.forEach(item => {
if (!testPriceKey(item)) {
// Receive name
results.push({ source: item, generated: this.bot.schema.getSkuFromName(item) });
} else {
results.push({ source: item, generated: this.bot.schema.getName(SKU.fromString(item), false) });
}
});

this.bot.sendMessage(
steamID,
`• ${results.map(item => `${item.source} => ${item.generated}`).join('\n• ')}`
);
}
};
}
7 changes: 7 additions & 0 deletions src/classes/Commands/commands/inventory/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AutokeysCommand from './Autokeys';
import CraftingCommand from './Crafting';
import DepositCommand from './Deposit';
import PaintsCommand from './Paints';
import SkuCommand from './Sku';

export default [SkuCommand, PaintsCommand, AutokeysCommand, DepositCommand, CraftingCommand];

0 comments on commit f6596f1

Please sign in to comment.