commandbot
Version:
A framework that helps you create your own Discord bot easier.
121 lines (120 loc) • 4.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Command = void 0;
const commandsTypes_js_1 = require("../commandsTypes.js");
const CommandPermissions_js_1 = require("../../structures/CommandPermissions.js");
/**
* Bot core command object
* @class
*/
class Command {
/**
* Command core constructor
* @constructor
* @param {CommandManager} manager - manager bound to this command
* @param {CommandType} type - command internal type
* @param {APICommandInit} options - initialization options
*/
constructor(manager, type, options) {
var _a;
this.manager = manager;
this.name = options.name;
this.type = type;
this.default_permission = (_a = options.default_permission) !== null && _a !== void 0 ? _a : true;
if (!commandsTypes_js_1.CommandRegExps.baseName.test(this.name)) {
throw new Error(`"${this.name}" is not a valid command name`);
}
if (this.manager.get(this.name, this.type)) {
throw new Error(`A command with name "${this.name}" is already registered in the manager.`);
}
}
/**
* Converts a command instance to an {@link APICommandObject}
* @returns {APICommandObject} An object that is accepted by the Discord API
* @public
*/
toObject() {
return {
name: this.name,
default_permission: this.default_permission,
};
}
/**
* Check base command type
* @param {BaseCommandType} type - base command type
* @returns {boolean} Whether this command can be used as the given type
* @public
*/
isBaseCommandType(type) {
switch (type) {
case "BASE":
return ("name" in this &&
"type" in this &&
"default_permission" in this &&
typeof this.name === "string" &&
(this.type === "CHAT" || this.type === "CONTEXT"));
case "FUNCTION":
return ("announceSuccess" in this &&
"start" in this &&
typeof this.announceSuccess === "boolean" &&
this.start instanceof Function);
case "GUILD":
return this.isBaseCommandType("FUNCTION") && "dm" in this && typeof this.dm === "boolean";
case "PERMISSION":
return this.isBaseCommandType("FUNCTION") && "permissions" in this && this.permissions instanceof CommandPermissions_js_1.CommandPermissions;
case "PERMISSIONGUILD":
return (this.isBaseCommandType("FUNCTION") &&
"dm" in this &&
typeof this.dm === "boolean" &&
"permissions" in this &&
this.permissions instanceof CommandPermissions_js_1.CommandPermissions);
default:
return false;
}
}
/**
* Checks command type
* @param {CommandType} type - command type
* @returns {boolean} Whether this command can be used as the given type
* @public
*/
isCommandType(type) {
switch (type) {
case "CHAT":
return (this.type === "CHAT" &&
"parameters" in this &&
Array.isArray(this.parameters) &&
"description" in this &&
typeof this.description === "string" &&
"visible" in this &&
typeof this.visible === "boolean" &&
"slash" in this &&
typeof this.slash === "boolean");
case "CONTEXT":
return this.type === "CONTEXT" && this.isBaseCommandType("PERMISSIONGUILD");
default:
return false;
}
}
/**
* Check child command type
* @param {ChildCommandType} type - child command type
* @returns {boolean} Whether this command can be used as a child command of the given type
* @public
*/
isChildCommandType(type) {
switch (type) {
case "COMMAND":
return (this.type === "CHAT" &&
"description" in this &&
typeof this.description === "string" &&
"parameters" in this &&
Array.isArray(this.parameters));
case "GROUP":
return this.type === "CHAT" && "description" in this && !("parameters" in this) && !("visible" in this) && !("slash" in this);
default:
return false;
}
}
}
exports.Command = Command;