commandbot
Version:
A framework that helps you create your own Discord bot easier.
144 lines (143 loc) • 5.05 kB
JavaScript
import { CommandRegExps } from "../commandsTypes.js";
import { CommandPermissions } from "../../structures/CommandPermissions.js";
/**
* Bot core command object
* @class
*/
export class Command {
/**
* Manager in which this command is registered
* @type {CommandManager}
* @public
* @readonly
*/
manager;
/**
* Command name
* @type {string}
* @public
* @readonly
*/
name;
/**
* CommandBot's internal command type
* @type {CommandType}
* @public
* @readonly
*/
type;
/**
* Discord API default_permission
* @type {boolean}
* @public
* @readonly
*/
default_permission;
/**
* 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) {
this.manager = manager;
this.name = options.name;
this.type = type;
this.default_permission = options.default_permission ?? true;
if (!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;
case "PERMISSIONGUILD":
return (this.isBaseCommandType("FUNCTION") &&
"dm" in this &&
typeof this.dm === "boolean" &&
"permissions" in this &&
this.permissions instanceof 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;
}
}
}