UNPKG

@wilcosp/rex

Version:

Rex is an automated command manager for discord js

147 lines (146 loc) 5.54 kB
"use strict"; /*! * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RexSlashCommandGroup = void 0; const v10_1 = require("discord-api-types/v10"); const promises_1 = __importDefault(require("fs/promises")); const path_1 = require("path"); const manager_js_1 = require("../manager.js"); const command_js_1 = require("./command.js"); const commandBase_js_1 = require("./commandBase.js"); const sub_js_1 = require("./sub.js"); class RexSlashCommandGroup extends commandBase_js_1.RexSlashCommandBase { constructor(name, description, folder) { super(name, description); this.folder = folder; this._commands = new Map(); } get commandInfo() { const ref = this._commandinfo?.deref(); if (ref) { return ref; } const commands = new Map(); for (const cmd of this._commands.values()) { commands.set(cmd.name, cmd.commandInfo); } const info = { name: this.name, commands: commands, description: this._description, id: this._commandId, type: -2, descriptionLocalizations: this._descriptionLocals ? new Map(this._descriptionLocals) : undefined, nameLocalizations: this._nameLocals ? new Map(this._nameLocals) : undefined, }; this._commandinfo = new WeakRef(info); return info; } load(dir) { const path = (0, path_1.join)(dir, this.folder); return promises_1.default.readdir(path).then(async (files) => { for (const file of files.filter(f => f.endsWith(".js") || f.endsWith(".mjs") || f.endsWith("cjs"))) { const filepath = (0, path_1.join)(path, file); await promises_1.default .stat(filepath) .then(stats => (0, manager_js_1.importer)(`${(0, path_1.join)(path, file)}?t=${Math.floor(stats.mtimeMs)}`)) .then(com => { if (com instanceof command_js_1.RexSlashCommand || com instanceof sub_js_1.RexSlashCommandSub) { return com; } return com.default; }) .then(async (com) => { if (com instanceof command_js_1.RexSlashCommand) { this._commands.set(com.name, com); } if (com instanceof sub_js_1.RexSlashCommandSub) { await com.load(path); this._commands.set(com.name, com); } }); } }); } toCommand() { const options = []; for (const [key, com] of this._commands) { options.push(com.toSlashSubCommand()); } return { ...super.toCommand(), options }; } equalToCommand(aCom) { if (!super.equalToCommand(aCom)) { return false; } const options = aCom.options; if (!options) { return false; } if (Object.keys(options).length != this._commands.size) return false; for (const [name, command] of this._commands) { const existing = options.find(com => com.name == name); if (!existing) { return false; } if (command instanceof command_js_1.RexSlashCommand) { if (!(existing.type == v10_1.ApplicationCommandOptionType.Subcommand)) return false; if (!command.equalToSubSlashCommand(existing)) return false; } else if (command instanceof sub_js_1.RexSlashCommandSub) { if (!(existing.type == v10_1.ApplicationCommandOptionType.SubcommandGroup)) return false; if (!command.equaltoGroupSlashCommand(existing)) return false; } } return true; } setCommandId(id) { this._commandinfo = undefined; for (const cmd of this._commands.values()) { cmd.setCommandId(id, { group: this.name }); } return super.setCommandId(id); } run(aCom, commands) { const groupCom = aCom.options.getSubcommandGroup(false); if (groupCom) { const groupCommand = this._commands.get(groupCom); if (groupCommand) { return groupCommand.run(aCom, commands); } return; } const subCom = aCom.options.getSubcommand(); if (subCom) { const subcommand = this._commands.get(subCom); if (subcommand) { return subcommand.run(aCom, commands); } } } runAutoComplete(auto, commands) { const subCom = auto.options.getSubcommand(); if (subCom) { const subcommand = this._commands.get(subCom); if (subcommand) { return subcommand.runAutoComplete(auto, commands); } } } setExecute() { return this; } } exports.RexSlashCommandGroup = RexSlashCommandGroup;