sheweny
Version:
The powerful framework for create discord bots
256 lines • 8.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Command = void 0;
const index_js_1 = require("./index.js");
const index_js_2 = require("../helpers/index.js");
const constants_js_1 = require("../constants/constants.js");
/**
* Represents an Command structure
* @extends {BaseStructure}
*/
class Command extends index_js_1.BaseStructure {
/**
* Constructor for build a Command
* @param {ShewenyClient} client Client framework
* @param {CommandData} data Data for build a Command
*/
constructor(client, data) {
super(client, data.enabled);
/**
* If a command is reserved for bot admins
* @type {boolean}
*/
Object.defineProperty(this, "adminsOnly", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Register Slash Command or not
* @type {boolean}
*/
Object.defineProperty(this, "registerApplicationCommand", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Aliases of the Message command
* @type {string[] | undefined}
*/
Object.defineProperty(this, "aliases", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Args of a Message command
* @type {MessageCommandOptionData | undefined}
*/
Object.defineProperty(this, "args", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Category of a command
* @type {string}
*/
Object.defineProperty(this, "category", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Only channel where a command can be executed
* @type {"GUILD" | "DM" | undefined}
*/
Object.defineProperty(this, "channel", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The permissions required for the client
* @type {PermissionResolvable[]}
*/
Object.defineProperty(this, "clientPermissions", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Cooldown of a command in seconds
* @type {number}
*/
Object.defineProperty(this, "cooldown", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Description of a command
* @type {string | undefined}
*/
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Localized descriptions of the command
* @type {LocalizationMap}
*/
Object.defineProperty(this, "descriptionLocalizations", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Examples of a command
* @type {string}
*/
Object.defineProperty(this, "examples", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Name of a command
* @type {string}
*/
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Localized names of the command
* @type {LocalizationMap}
*/
Object.defineProperty(this, "nameLocalizations", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Options of a Application command
* @type {ApplicationCommandOptionData[] | undefined}
*/
Object.defineProperty(this, "options", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Type of a command
* @type {CommandType}
*/
Object.defineProperty(this, "type", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* Usage of a command
* @type {string | string[] | undefined}
*/
Object.defineProperty(this, "usage", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
/**
* The permissions required to be executed by the user
* @type {PermissionResolvable[]}
*/
Object.defineProperty(this, "userPermissions", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
const defaultData = client.managers.commands?.default || {};
const type = data.type || defaultData.type || constants_js_1.COMMAND_TYPE.cmdMsg;
this.adminsOnly = (data.adminsOnly || defaultData.adminOnly) ?? false;
this.registerApplicationCommand = (data.registerApplicationCommand || defaultData.registerApplicationCommand) ?? true;
this.aliases = this.isType(type, constants_js_1.COMMAND_TYPE.cmdMsg) ? data.aliases : [];
this.args = this.isType(type, constants_js_1.COMMAND_TYPE.cmdMsg) ? data.args : undefined;
this.category = (data.category || defaultData.category) ?? '';
this.channel = data.channel || defaultData.channel;
this.clientPermissions = (data.clientPermissions || defaultData.clientPermissions) ?? [];
this.cooldown = (data.cooldown || defaultData.cooldown) ?? 0;
this.description = (data.description || defaultData.description) ?? '';
this.descriptionLocalizations = !this.isType(type, constants_js_1.COMMAND_TYPE.cmdMsg)
? data.descriptionLocalizations || undefined
: undefined;
this.examples = data.examples || defaultData.examples;
this.name = data.name;
this.nameLocalizations = !this.isType(type, constants_js_1.COMMAND_TYPE.cmdMsg)
? data.nameLocalizations || undefined
: undefined;
this.options = this.isType(type, constants_js_1.COMMAND_TYPE.cmdSlash) ? data.options : undefined;
this.type = type;
this.usage = data.usage || defaultData.usage;
this.userPermissions = (data.userPermissions || defaultData.userPermissions) ?? [];
}
/**
* Check the type of a command
* @param type - Type of a command
* @param types - Types allowed
* @returns {boolean}
*/
isType(type, ...types) {
if (types.includes(type))
return true;
return false;
}
/**
* Register a command in collections
* @returns {Collection<string, ApplicationCommand>} The Application Commands collection
*/
async register() {
if (!this.path)
return new index_js_2.ShewenyError(this.client, 'PATH_NOT_DEFINE', 'Command', this.name);
const CommandImported = (await Promise.resolve().then(() => __importStar(require(this.path)))).default;
const cmd = new CommandImported(this.client);
return cmd;
}
/**
* Reload a command
* @returns {Promise<Collection<string, Command> | ShewenyError>} The Application Commands collection
*/
async reload() {
this.unregister();
return this.register();
}
/**
* Unregister a command from collections
* @returns {boolean}
*/
unregister() {
this.client.collections.commands?.delete(this.name);
if (!this.path)
return false;
delete require.cache[require.resolve(this.path)];
return true;
}
}
exports.Command = Command;
//# sourceMappingURL=Command.js.map