commandbot
Version:
A framework that helps you create your own Discord bot easier.
109 lines (108 loc) • 6.06 kB
JavaScript
;
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.FunctionCommand = void 0;
const discord_js_1 = require("discord.js");
const errors_js_1 = require("../../errors.js");
const Command_js_1 = require("./Command.js");
/**
* Function (executable) command
* @class
* @extends {Command}
*/
class FunctionCommand extends Command_js_1.Command {
/**
* Executable command constructor
* @constructor
* @param {CommandManager} manager - command manager attached to this command
* @param {CommandType} type - command type
* @param {FunctionCommandInit} options - command initailization options
*/
constructor(manager, type, options) {
var _a, _b, _c;
super(manager, type, {
name: options.name,
default_permission: options.default_permission,
});
this._function =
(_a = options.function) !== null && _a !== void 0 ? _a : ((input) => {
if (this.manager.help)
return this.manager.help.generateMessage(input.interaction, this.name);
else
return;
});
this.announceSuccess = (_b = options.announceSuccess) !== null && _b !== void 0 ? _b : true;
this.ephemeral = (_c = options.ephemeral) !== null && _c !== void 0 ? _c : "NONE";
}
/**
* Invoke the command
* @param {InputManager} input - input data manager
* @returns {Promise<void>} A *Promise* that resolves after the function command is completed
* @public
* @async
*/
start(input) {
return __awaiter(this, void 0, void 0, function* () {
if (input.interaction instanceof discord_js_1.Interaction && !input.interaction.isCommand() && !input.interaction.isContextMenu())
throw new TypeError(`Interaction not recognized`);
if (input.interaction instanceof discord_js_1.Interaction)
yield input.interaction.deferReply({ ephemeral: this.ephemeral !== "NONE" });
yield this.handleReply(input.interaction, yield this._function(input));
});
}
/**
* Reply handler
* @param {Message | Interaction} interaction - Discord interaction object
* @param {void | string | MessageEmbed | ReplyMessageOptions} result - result of command function execution
* @return {Promise<void>}
* @private
* @async
*/
handleReply(interaction, result) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
if (interaction instanceof discord_js_1.Interaction && !interaction.isCommand() && !interaction.isContextMenu())
throw new TypeError(`Interaction not recognized`);
if (result instanceof Object &&
("content" in result || "embeds" in result || "files" in result || "components" in result || "sticker" in result)) {
if (interaction instanceof discord_js_1.Message)
this.ephemeral === "FULL" ? yield ((_a = interaction.member) === null || _a === void 0 ? void 0 : _a.send(result)) : yield interaction.reply(result);
else if (interaction instanceof discord_js_1.Interaction)
yield interaction.editReply(result);
}
else if (typeof result == "string") {
if (interaction instanceof discord_js_1.Message)
this.ephemeral === "FULL" ? yield ((_b = interaction === null || interaction === void 0 ? void 0 : interaction.member) === null || _b === void 0 ? void 0 : _b.send({ content: result })) : yield (interaction === null || interaction === void 0 ? void 0 : interaction.reply({ content: result }));
else if (interaction instanceof discord_js_1.Interaction)
yield interaction.editReply({
content: result,
});
}
else if (result instanceof discord_js_1.MessageEmbed) {
if (interaction instanceof discord_js_1.Message)
this.ephemeral === "FULL" ? yield ((_c = interaction.member) === null || _c === void 0 ? void 0 : _c.send({ embeds: [result] })) : yield (interaction === null || interaction === void 0 ? void 0 : interaction.reply({ embeds: [result] }));
else if (interaction instanceof discord_js_1.Interaction)
yield interaction.editReply({ embeds: [result] });
}
else if (this.announceSuccess && (interaction instanceof discord_js_1.Interaction ? !interaction.replied : true)) {
throw new errors_js_1.OperationSuccess(this);
}
else if (interaction instanceof discord_js_1.Interaction && !interaction.replied) {
this.ephemeral !== "NONE"
? yield interaction.editReply({
embeds: [new discord_js_1.MessageEmbed().setTitle(this.manager.client.messages.SUCCESS.title).setColor((_d = this.manager.client.messages.SUCCESS.accentColor) !== null && _d !== void 0 ? _d : "#00ff00")],
})
: yield interaction.deleteReply();
}
});
}
}
exports.FunctionCommand = FunctionCommand;