UNPKG

commandbot

Version:

A framework that helps you create your own Discord bot easier.

167 lines (166 loc) 8.04 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubCommand = void 0; const generateUsageFromArguments_js_1 = require("../utils/generateUsageFromArguments.js"); const Parameter_js_1 = require("../structures/Parameter.js"); const PermissionCommand_js_1 = require("./base/PermissionCommand.js"); const SubCommandGroup_js_1 = require("./SubCommandGroup.js"); const commandsTypes_js_1 = require("./commandsTypes.js"); /** * Representation of SUB_COMMAND Discord interaction * @class */ class SubCommand extends PermissionCommand_js_1.PermissionCommand { /** * Subcommand constructor (SUB_COMMAND parameter in Discord API) * @constructor * @param {SubCommandGroup | ChatCommand} parent - command parent * @param {SubCommandInit} options - initialization options */ constructor(parent, options) { var _a, _b; super(parent instanceof SubCommandGroup_js_1.SubCommandGroup ? parent.parent.manager : parent.manager, "CHAT", { name: options.name, announceSuccess: options.announceSuccess, permissions: options.permissions, function: options.function, ephemeral: options.ephemeral, }); this.parent = parent; this.description = (_a = options.description) !== null && _a !== void 0 ? _a : "No description"; if (options.parameters == "no_input" || !options.parameters) { this.parameters = []; } else if (options.parameters == "simple") { this.parameters = [new Parameter_js_1.DefaultParameter(this)]; } else { this.parameters = options.parameters.map((ps) => new Parameter_js_1.Parameter(this, ps)); } this.aliases = options.aliases ? (Array.isArray(options.aliases) ? options.aliases : [options.aliases]) : undefined; this.usage = (_b = options.usage) !== null && _b !== void 0 ? _b : (0, generateUsageFromArguments_js_1.generateUsageFromArguments)(this); if (this.parent.children.find((ch) => ch.name === this.name)) { throw new Error(`Parent "${this.parent.name}" already has a subcommand or group named "${this.name}"`); } if (!commandsTypes_js_1.CommandRegExps.chatName.test(this.name)) { throw new Error(`"${this.name}" is not a valid command name (regexp: ${commandsTypes_js_1.CommandRegExps.chatName})`); } if (this.description && !commandsTypes_js_1.CommandRegExps.chatDescription.test(this.description)) { throw new Error(`The description of "${this.name}" doesn't match a regular expression ${commandsTypes_js_1.CommandRegExps.chatDescription}`); } if (this.aliases) { if (Array.isArray(this.aliases)) { this.aliases.map((a) => { if (!commandsTypes_js_1.CommandRegExps.chatName.test(a)) { throw new Error(`"${a}" is not a valid alias name (regexp: ${commandsTypes_js_1.CommandRegExps.chatName})`); } }); } else { if (!commandsTypes_js_1.CommandRegExps.chatName.test(this.aliases)) { throw new Error(`"${this.aliases}" is not a valid alias name (regexp: ${commandsTypes_js_1.CommandRegExps.chatName})`); } } } if (this.aliases && this.aliases.length > 0 && this.aliases.find((a) => this.manager.get(a, this.type))) { throw new Error(`One of aliases from "${this.name}" command is already a registered name in the manager and cannot be reused.`); } } /** * Invoke the command * @param {InputManager} input - input data * @returns {Promise<void>} * @async */ start(input) { const _super = Object.create(null, { start: { get: () => super.start } }); return __awaiter(this, void 0, void 0, function* () { if (this.parent instanceof SubCommandGroup_js_1.SubCommandGroup ? !this.parent.parent.dm && !input.interaction.guild : !this.parent.dm && !input.interaction.guild) throw new Error(`Command "${this.name}" is only available inside a guild.`); if (this.parent instanceof SubCommandGroup_js_1.SubCommandGroup ? this.parent.parent.guilds && this.parent.parent.guilds.length > 0 && !this.parent.parent.guilds.find((id) => { var _a; return id === ((_a = input.interaction.guild) === null || _a === void 0 ? void 0 : _a.id); }) : this.parent.guilds && this.parent.guilds.length > 0 && !this.parent.guilds.find((id) => { var _a; return id === ((_a = input.interaction.guild) === null || _a === void 0 ? void 0 : _a.id); })) throw new Error(`Command "${this.name}" is not available.`); yield _super.start.call(this, input); }); } /** * @returns {ChatCommandObject} Discord API object * @public */ toObject() { const obj = Object.assign(Object.assign({}, super.toObject()), { type: 1, description: this.description }); let options = []; if (this.parameters) { options = this.parameters .map((p) => { let type = 3; switch (p.type) { case "boolean": type = 5; break; case "user": type = 6; break; case "channel": type = 7; break; case "role": type = 8; break; case "mentionable": type = 9; break; case "number": type = 10; break; case "target": throw new Error(`"target" parameter cannot be used in chat commands`); default: type = 3; break; } const choices = []; if (p.choices) { p.choices.map((c) => { choices.push({ name: c, value: c }); }); } const optionObj = { name: p.name, description: p.description, required: !p.optional, type: p.choices ? 3 : type, choices: choices.length > 0 ? choices : undefined, }; return optionObj; }) .sort((a, b) => { if (a.required && !b.required) { return -1; } else if (a.required && b.required) { return 0; } else if (!a.required && b.required) { return 1; } return 0; }); obj.options = options; } return obj; } } exports.SubCommand = SubCommand;