Skip to content

Commit

Permalink
feat: importing cart commands (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolfx committed Jan 17, 2023
1 parent d342506 commit 46cf31a
Showing 1 changed file with 101 additions and 5 deletions.
106 changes: 101 additions & 5 deletions src/classes/Commands/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -46,14 +52,29 @@ export interface ICommand {
/**
* When the command is called
*/
execute: (steamID: SteamID, message: string) => Promise<void> | void;
execute: (steamID: SteamID, message: string, command?: any) => Promise<void> | 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<Inventory> = {};

private readonly commands: Map<string, ICommand>;

private readonly commandsPointAliases: Map<string, string>;
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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.`
);
}
}
}

0 comments on commit 46cf31a

Please sign in to comment.