UNPKG

@wilcosp/rex

Version:

Rex is an automated command manager for discord js

178 lines (177 loc) 6.81 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.RexSlashCommandSub = void 0; const v10_1 = require("discord-api-types/v10"); const promises_1 = __importDefault(require("fs/promises")); const path_1 = require("path"); const locale_js_1 = require("../helpers/locale.js"); const manager_js_1 = require("../manager.js"); const command_js_1 = require("./command.js"); const commandBase_js_1 = require("./commandBase.js"); class RexSlashCommandSub 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 = { commands, description: this.description, id: this._commandId, name: this.name, type: -1, descriptionLocalizations: this._descriptionLocals ? new Map(this._descriptionLocals) : undefined, nameLocalizations: this._nameLocals ? new Map(this._nameLocals) : undefined, nsfw: this.nsfw, }; 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) { return com; } return com.default; }) .then(com => { if (com instanceof command_js_1.RexSlashCommand) { this._commands.set(com.name, com); } }); } }); } toCommand() { const options = []; for (const [key, com] of this._commands) { options.push(com.toSlashSubCommand()); } return { ...super.toCommand(), options }; } toSlashSubCommand() { const options = []; for (const [key, com] of this._commands) { options.push(com.toSlashSubCommand()); } return { type: v10_1.ApplicationCommandOptionType.SubcommandGroup, name: this.name, description: this._description, options, name_localizations: this._nameLocals ? Object.fromEntries(this._nameLocals) : undefined, description_localizations: this._descriptionLocals ? Object.fromEntries(this._descriptionLocals) : undefined, }; } 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 (!(existing.type == v10_1.ApplicationCommandOptionType.Subcommand)) return false; if (!command.equalToSubSlashCommand(existing)) return false; } return true; } equaltoGroupSlashCommand(existing) { if (this.name != existing.name || this.description != existing.description || !(existing.type == v10_1.ApplicationCommandOptionType.SubcommandGroup)) return false; const nameLocals = existing.name_localizations ?? existing.nameLocalizations; if (nameLocals) { if (!this._nameLocals) return false; if (!(0, locale_js_1.LocalsEqual)(nameLocals, Object.fromEntries(this._nameLocals))) return false; } if (!nameLocals && this._nameLocals) return false; const descLocals = existing.description_localizations ?? existing.descriptionLocalizations; if (descLocals) { if (!this._descriptionLocals) return false; if (!(0, locale_js_1.LocalsEqual)(descLocals, Object.fromEntries(this._descriptionLocals))) return false; } if (!descLocals && this._descriptionLocals) return false; const options = existing.options; if (!options) return false; for (const [name, command] of this._commands) { const existingOption = options.find(com => com.name == name); if (!existingOption) { return false; } if (!(existingOption.type == v10_1.ApplicationCommandOptionType.Subcommand)) return false; if (!command.equalToSubSlashCommand(existingOption)) return false; } return true; } setCommandId(id, { group } = {}) { this._commandinfo = undefined; for (const cmd of this._commands.values()) { cmd.setCommandId(id, { group, sub: this.name }); } return super.setCommandId(id); } run(aCom, commands) { 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.RexSlashCommandSub = RexSlashCommandSub;