UNPKG

@guildedts/framework

Version:

A framework for creating a Guilded bot.

90 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Command = void 0; const collection_1 = require("@discordjs/collection"); const guilded_ts_1 = require("guilded.ts"); /** * Represents a command. * @example * class Ping extends Command { * name = 'ping'; * * execute(message) { * message.reply('Pong!'); * } * } */ class Command { client; /** The name of the command. */ name; /** The aliases of the command. */ aliases = []; /** The description of the command. */ description; /** The options of the command. */ arguments = []; /** The cooldown of the command. */ cooldown; /** The cooldowns of the command. */ cooldowns = new collection_1.Collection(); /** @param client The client the command belongs to. */ constructor(client) { this.client = client; this.cooldown = this.cooldown ?? client.config.commandCooldown; } /** * Get the usage of the command. * @param serverId The ID of the server the command is used in. * @returns The usage of the command. * @example command.getUsage(); */ getUsage(serverId) { const prefix = serverId ? this.client.prefixes.get(serverId) || this.client.config.prefix : this.client.config.prefix; return `${prefix}${this.name} ${this.arguments .map((arg) => new arg(this).usage) .join(' ')}`; } /** * Validate the command. * @param message The message that triggered the command. * @param args The arguments of the command. * @returns The validated arguments. * @example command.validate(message, ['hello', 'world']); // { content: 'hello world' } */ async validate(message, args) { const cooldown = this.cooldowns.get(message.createdBy); const now = Date.now(); if (cooldown && now < cooldown) throw new Error(`You must wait ${(cooldown - now) / 1000} seconds before using the ${(0, guilded_ts_1.inlineCode)(this.name)} command again`); else return this.validateArguments(args); } /** * Validate the command arguments. * @param args The arguments to validate. * @returns The validated arguments. * @example command.validateArguments(['hello', 'world']); // { content: 'hello world' } */ async validateArguments(args) { const mappedArgs = {}; for (const [index, argument] of this.arguments .map((Argument) => new Argument(this)) .entries()) mappedArgs[argument.name] = await argument.validate(!this.arguments[index + 1] ? args.splice(index).join(' ') : args[index]); return mappedArgs; } /** * Set a cooldown for a user. * @param userId The ID of the user to set the cooldown for. * @example command.setCooldown('abc'); */ setCooldown(userId) { if (this.cooldown > 0) this.cooldowns.set(userId, Date.now() + this.cooldown); } } exports.Command = Command; //# sourceMappingURL=Command.js.map