From 46cf31a93fa3439ad8a24f6de3c015abc2d4ed6e Mon Sep 17 00:00:00 2001 From: Tolfx Date: Tue, 17 Jan 2023 15:44:33 +0100 Subject: [PATCH] feat: importing cart commands (#1475) --- src/classes/Commands/CommandHandler.ts | 106 +++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 5 deletions(-) diff --git a/src/classes/Commands/CommandHandler.ts b/src/classes/Commands/CommandHandler.ts index ee6c97bc9..2cf380071 100644 --- a/src/classes/Commands/CommandHandler.ts +++ b/src/classes/Commands/CommandHandler.ts @@ -2,9 +2,15 @@ import SteamID from 'steamid'; import Bot from '../Bot'; import IPricer from '../IPricer'; import log from '../../lib/logger'; +import CommandParser from '../CommandParser'; +import CartQueue from '../Carts/CartQueue'; +import Cart from '../Carts/Cart'; +import { UnknownDictionary } from '../../types/common'; +import Inventory from '../Inventory'; +// Import all commands import InformationCommands from './commands/information'; -import CommandParser from '../CommandParser'; +import CartCommands from './commands/cart'; export interface ICommand { /** @@ -46,14 +52,29 @@ export interface ICommand { /** * When the command is called */ - execute: (steamID: SteamID, message: string) => Promise | void; + execute: (steamID: SteamID, message: string, command?: any) => Promise | void; } +export type Instant = 'buy' | 'b' | 'sell' | 's'; +export type CraftUncraft = 'craftweapon' | 'uncraftweapon'; +export type Misc = 'time' | 'uptime' | 'pure' | 'rate' | 'owner' | 'discord' | 'stock'; +export type BlockUnblock = 'block' | 'unblock'; +export type NameAvatar = 'name' | 'avatar'; +export type TF2GC = 'expand' | 'use' | 'delete'; +export type ActionOnTrade = 'accept' | 'accepttrade' | 'decline' | 'declinetrade'; +export type ForceAction = 'faccept' | 'fdecline'; + function hasAliases(command: ICommand): command is ICommand & { aliases: string[] } { return command.aliases !== undefined; } export default class CommandHandler { + isDonating = false; + + adminInventoryReset: NodeJS.Timeout; + + adminInventory: UnknownDictionary = {}; + private readonly commands: Map; private readonly commandsPointAliases: Map; @@ -69,9 +90,9 @@ export default class CommandHandler { // We will also include aliases and point them to the command // We want to initialize all our commands - const commands = [...InformationCommands]; + const commands = [...InformationCommands, ...CartCommands]; for (const command of commands) { - const cmd = new command(this.bot, this.pricer); + const cmd = new command(this.bot, this.pricer, this); this.commands.set(cmd.name, cmd); if (hasAliases(cmd)) { for (const alias of cmd.aliases) { @@ -88,6 +109,12 @@ export default class CommandHandler { 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(`Received command ${command} from ${steamID.getSteamID64()}`); if (command === null) { @@ -113,6 +140,75 @@ export default class CommandHandler { return this.bot.sendMessage(steamID, '❌ Command not available.'); } - await cmd.execute(steamID, message); + await cmd.execute(steamID, message, command); + } + + get cartQueue(): CartQueue { + return this.bot.handler.cartQueue; + } + + get weaponsAsCurrency(): { enable: boolean; withUncraft: boolean } { + return { + enable: this.bot.options.miscSettings.weaponsAsCurrency.enable, + withUncraft: this.bot.options.miscSettings.weaponsAsCurrency.withUncraft + }; + } + + addCartToQueue(cart: Cart, isDonating: boolean, isBuyingPremium: boolean): void { + const activeOfferID = this.bot.trades.getActiveOffer(cart.partner); + + const custom = this.bot.options.commands.addToQueue; + + if (activeOfferID !== null) { + return this.bot.sendMessage( + cart.partner, + custom.alreadyHaveActiveOffer + ? custom.alreadyHaveActiveOffer.replace( + /%tradeurl%/g, + `https://steamcommunity.com/tradeoffer/${activeOfferID}/` + ) + : `❌ You already have an active offer! Please finish it before requesting a new one: https://steamcommunity.com/tradeoffer/${activeOfferID}/` + ); + } + + const currentPosition = this.cartQueue.getPosition(cart.partner); + + if (currentPosition !== -1) { + if (currentPosition === 0) { + this.bot.sendMessage( + cart.partner, + custom.alreadyInQueueProcessingOffer + ? custom.alreadyInQueueProcessingOffer + : '⚠️ You are already in the queue! Please wait while I process your offer.' + ); + } else { + this.bot.sendMessage( + cart.partner, + custom.alreadyInQueueWaitingTurn + ? custom.alreadyInQueueWaitingTurn + .replace(/%isOrAre%/g, currentPosition !== 1 ? 'are' : 'is') + .replace(/%currentPosition%/g, String(currentPosition)) + : '⚠️ You are already in the queue! Please wait your turn, there ' + + (currentPosition !== 1 ? 'are' : 'is') + + ` ${currentPosition} in front of you.` + ); + } + return; + } + + const position = this.cartQueue.enqueue(cart, isDonating, isBuyingPremium); + + if (position !== 0) { + this.bot.sendMessage( + cart.partner, + custom.addedToQueueWaitingTurn + ? custom.addedToQueueWaitingTurn + .replace(/%isOrAre%/g, position !== 1 ? 'are' : 'is') + .replace(/%position%/g, String(position)) + : '✅ You have been added to the queue! Please wait your turn, there ' + + (position !== 1 ? 'are' : 'is') + + ` ${position} in front of you.` + ); + } } }