@globalart/nestcord
Version:
A module for creating Discord bots using NestJS, based on Discord.js
92 lines (91 loc) • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlashCommandDiscovery = void 0;
const discord_js_1 = require("discord.js");
const command_discovery_1 = require("../command.discovery");
const options_1 = require("./options");
/**
* Represents a slash command discovery.
*/
class SlashCommandDiscovery extends command_discovery_1.CommandDiscovery {
constructor() {
super(...arguments);
this.subcommands = new discord_js_1.Collection();
}
/**
* Returns the command description.
*/
getDescription() {
return this.meta.description;
}
/**
* Sets the command description.
* @param command The command discovery.
*/
setSubcommand(command) {
this.subcommands.set(command.getName(), command);
}
/**
* Ensures a subcommand exists.
* @param command
*/
ensureSubcommand(command) {
return this.subcommands.ensure(command.getName(), () => command);
}
/**
* Returns the subcommand.
* @param name
*/
getSubcommand(name) {
return this.subcommands.get(name);
}
/**
* Returns the subcommands.
*/
getSubcommands() {
return this.subcommands;
}
/**
* Returns raw options from metadata.
*/
getRawOptions() {
return this.reflector.get(options_1.OPTIONS_METADATA, this.getHandler()) || {};
}
/**
* Returns the options.
*/
getOptions() {
if (this.subcommands.size >= 1) {
return [...this.subcommands.values()].map((subcommand) => subcommand.toJSON());
}
return Object.values(this.getRawOptions());
}
/**
* Executes the command.
* @param interaction
* @param depth
*/
execute(interaction, depth = 1) {
var _a;
if (this.subcommands.size >= 1) {
const commandName = depth === 2
? interaction.options.getSubcommand(true)
: interaction.options.getSubcommandGroup(false) || interaction.options.getSubcommand(true);
return (_a = this.subcommands.get(commandName)) === null || _a === void 0 ? void 0 : _a.execute(interaction, depth + 1);
}
return super.execute([interaction]);
}
/**
* Returns whether the discovery is a slash command.
*/
isSlashCommand() {
return true;
}
/**
* Returns the JSON representation of the discovery.
*/
toJSON() {
return Object.assign(Object.assign({}, this.meta), { options: this.getOptions() });
}
}
exports.SlashCommandDiscovery = SlashCommandDiscovery;