commandbot
Version:
A framework that helps you create your own Discord bot easier.
236 lines (235 loc) • 14.7 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.SystemMessageManager = void 0;
const discord_js_1 = require("discord.js");
/**
* Stores configuration and generates system messages
*
* System messages - predefined messages sent by the bot in special cases (for example after an error, or when a command doesn't exist)
*
* @remarks You can't customize messages after starting the bot. Changing these properties while the bot is running will have no effect.
*
* @class
*/
class SystemMessageManager {
constructor(client) {
this.client = client;
this.PERMISSION = {
enabled: true,
title: "👮♂️ Insufficient permissions",
bottomText: "You don't have enough permissions to run this command",
accentColor: "#1d1dc4",
displayDetails: false,
showTimestamp: true,
footer: this.client.name,
};
this.ERROR = {
enabled: true,
title: "❌ An error occurred",
bottomText: "Something went wrong while processing your request.",
accentColor: "#ff0000",
displayDetails: false,
showTimestamp: true,
footer: this.client.name,
};
this.NOT_FOUND = {
enabled: false,
title: "🔍 Command not found",
accentColor: "#ff5500",
displayDetails: false,
showTimestamp: true,
footer: this.client.name,
};
this.SUCCESS = {
enabled: true,
title: "✅ Task completed successfully",
accentColor: "#00ff00",
displayDetails: false,
showTimestamp: true,
footer: this.client.name,
deleteTimeout: Infinity,
};
this.deleteTimeout = Infinity;
}
/**
* Generates and sends a system message
* @param {MessageType} type - "ERROR" | "PERMISSION" | "NOT_FOUND" | "SUCCESS"
* @param {?SystemMessageData} [data] - additional data to include in the message
* @param {?Message | Interaction | TextChannel | DMChannel} [interaction] - if specified, the generated message will be sent in this channel
* @returns {Promise<MessageEmbed | Message | void>} A message that got sent or *void*
* @public
* @async
*/
send(type, data, interaction) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return __awaiter(this, void 0, void 0, function* () {
if (this[type]) {
if (this[type].enabled === false)
return;
const embed = new discord_js_1.MessageEmbed();
embed.setTitle(this[type].title);
if (this[type].description || this[type].bottomText)
embed.setDescription((_b = (_a = this[type].description) !== null && _a !== void 0 ? _a : this[type].bottomText) !== null && _b !== void 0 ? _b : "");
embed.setColor(this[type].accentColor || "#000000");
if (this[type].showTimestamp)
embed.setTimestamp();
if (data) {
switch (type) {
case "ERROR":
if (this[type].displayDetails) {
if (data.command) {
embed.addField("Command name:", data.command.name, false);
}
if (data.user) {
embed.addField("User:", data.user.toString(), false);
}
}
if (data.error) {
if (data.error instanceof Error) {
embed.setFooter(data.error.message);
}
else if (typeof data.error == "string") {
embed.setFooter(data.error);
}
}
break;
case "NOT_FOUND":
if (data.phrase) {
embed.setFooter(data.phrase);
}
break;
case "PERMISSION":
if (this[type].displayDetails) {
if (data.user) {
embed.addField("User:", data.user.toString(), false);
}
if (data.command) {
embed.addField("Command name:", data.command.name, false);
if (data.command.isBaseCommandType("PERMISSION")) {
if (data.command.permissions.isCustom) {
embed.addField("Required permissions:", "CUSTOM", false);
}
else {
let permList = "";
data.command.permissions.permissions.toArray(false).map((p) => {
permList += `${p}\n`;
});
permList && embed.addField("Required permissions:", permList, false);
}
}
}
}
break;
case "SUCCESS":
break;
}
}
if (interaction instanceof discord_js_1.TextChannel || interaction instanceof discord_js_1.DMChannel) {
const message = ((_c = data === null || data === void 0 ? void 0 : data.command) === null || _c === void 0 ? void 0 : _c.ephemeral) === "FULL"
? yield ((_d = data.user) === null || _d === void 0 ? void 0 : _d.send({ embeds: [embed] }).catch((e) => console.error(e)))
: yield interaction.send({ embeds: [embed] }).catch((e) => console.error(e));
if (message === null || message === void 0 ? void 0 : message.deletable) {
if (Number.isFinite(this[type].deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield message.delete().catch((e) => console.error(e));
}), this[type].deleteTimeout);
}
else if (this[type].deleteTimeout === undefined && Number.isFinite(this.deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield message.delete().catch((e) => console.error(e));
}), this.deleteTimeout);
}
}
return message !== null && message !== void 0 ? message : embed;
}
else if (interaction instanceof discord_js_1.Message) {
const message = ((_e = data === null || data === void 0 ? void 0 : data.command) === null || _e === void 0 ? void 0 : _e.ephemeral) === "FULL"
? yield ((_f = interaction.member) === null || _f === void 0 ? void 0 : _f.send({ embeds: [embed] }).catch((e) => console.error(e)))
: yield interaction.reply({ embeds: [embed] }).catch(() => __awaiter(this, void 0, void 0, function* () {
var _k, _l;
return ((_k = data === null || data === void 0 ? void 0 : data.command) === null || _k === void 0 ? void 0 : _k.ephemeral) === "FULL"
? yield ((_l = interaction.member) === null || _l === void 0 ? void 0 : _l.send({ embeds: [embed] }).catch((e) => console.error(e)))
: yield interaction.channel.send({ embeds: [embed] }).catch((e) => console.error(e));
}));
if (message === null || message === void 0 ? void 0 : message.deletable) {
if (Number.isFinite(this[type].deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield message.delete().catch((e) => console.error(e));
}), this[type].deleteTimeout);
}
else if (this[type].deleteTimeout === undefined && Number.isFinite(this.deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield message.delete().catch((e) => console.error(e));
}), this.deleteTimeout);
}
}
return message !== null && message !== void 0 ? message : embed;
}
else if (interaction instanceof discord_js_1.Interaction &&
(interaction.isCommand() || interaction.isContextMenu() || interaction.isButton() || interaction.isSelectMenu() || interaction.isSelectMenu())) {
const message = interaction.replied || interaction.deferred
? yield interaction.editReply({ embeds: [embed] }).catch(() => __awaiter(this, void 0, void 0, function* () {
var _m;
return yield ((_m = interaction.channel) === null || _m === void 0 ? void 0 : _m.send({ embeds: [embed] }).catch((e) => console.error(e)));
}))
: yield interaction.reply({ embeds: [embed], ephemeral: (_h = ((_g = data === null || data === void 0 ? void 0 : data.command) === null || _g === void 0 ? void 0 : _g.ephemeral) !== "NONE") !== null && _h !== void 0 ? _h : false }).catch(() => __awaiter(this, void 0, void 0, function* () {
var _o, _p;
return ((_o = data === null || data === void 0 ? void 0 : data.command) === null || _o === void 0 ? void 0 : _o.ephemeral) !== "NONE"
? yield interaction.user.send({ embeds: [embed] }).catch((e) => console.error(e))
: yield ((_p = interaction.channel) === null || _p === void 0 ? void 0 : _p.send({ embeds: [embed] }).catch((e) => console.error(e)));
}));
if (Number.isFinite(this[type].deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield interaction.deleteReply().catch(() => __awaiter(this, void 0, void 0, function* () {
message instanceof discord_js_1.Message && (message === null || message === void 0 ? void 0 : message.deletable) && (yield message.delete().catch((e) => console.error(e)));
}));
}), this[type].deleteTimeout);
}
else if (this[type].deleteTimeout === undefined && Number.isFinite(this.deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield interaction.deleteReply().catch(() => __awaiter(this, void 0, void 0, function* () {
message instanceof discord_js_1.Message && (message === null || message === void 0 ? void 0 : message.deletable) && (yield message.delete().catch((e) => console.error(e)));
}));
}), this.deleteTimeout);
}
return message instanceof discord_js_1.Message ? message : embed;
}
else if (interaction === null || interaction === void 0 ? void 0 : interaction.channel) {
const message = ((_j = data === null || data === void 0 ? void 0 : data.command) === null || _j === void 0 ? void 0 : _j.ephemeral) === "FULL"
? yield interaction.user.send({ embeds: [embed] }).catch((e) => console.error(e))
: yield interaction.channel.send({ embeds: [embed] }).catch((e) => console.error(e));
if (message === null || message === void 0 ? void 0 : message.deletable) {
if (Number.isFinite(this[type].deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield message.delete().catch((e) => console.error(e));
}), this[type].deleteTimeout);
}
else if (this[type].deleteTimeout === undefined && Number.isFinite(this.deleteTimeout)) {
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
yield message.delete().catch((e) => console.error(e));
}), this.deleteTimeout);
}
}
return message !== null && message !== void 0 ? message : embed;
}
else {
return embed;
}
}
else {
console.error("[❌ ERROR] System message: Incorrect parameter");
return;
}
});
}
}
exports.SystemMessageManager = SystemMessageManager;