@wilcosp/rex
Version:
Rex is an automated command manager for discord js
140 lines (139 loc) • 5.07 kB
JavaScript
/*!
* 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/.
*/
import { ApplicationCommandOptionType, } from "discord-api-types/v10";
import fs from "fs/promises";
import { join as pjoin } from "path";
import { importer } from "../manager.js";
import { RexSlashCommand } from "./command.js";
import { RexSlashCommandBase } from "./commandBase.js";
import { RexSlashCommandSub } from "./sub.js";
export class RexSlashCommandGroup extends 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 = pjoin(dir, this.folder);
return fs.readdir(path).then(async (files) => {
for (const file of files.filter(f => f.endsWith(".js") || f.endsWith(".mjs") || f.endsWith("cjs"))) {
const filepath = pjoin(path, file);
await fs
.stat(filepath)
.then(stats => importer(`${pjoin(path, file)}?t=${Math.floor(stats.mtimeMs)}`))
.then(com => {
if (com instanceof RexSlashCommand || com instanceof RexSlashCommandSub) {
return com;
}
return com.default;
})
.then(async (com) => {
if (com instanceof RexSlashCommand) {
this._commands.set(com.name, com);
}
if (com instanceof 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 RexSlashCommand) {
if (!(existing.type == ApplicationCommandOptionType.Subcommand))
return false;
if (!command.equalToSubSlashCommand(existing))
return false;
}
else if (command instanceof RexSlashCommandSub) {
if (!(existing.type == 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;
}
}