Add files via upload

This commit is contained in:
Murat
2023-11-29 18:23:54 +03:00
committed by GitHub
parent bdb4e9d949
commit 119d0275bd
79 changed files with 23303 additions and 0 deletions

550
node_modules/eris/lib/command/Command.js generated vendored Normal file
View File

@ -0,0 +1,550 @@
"use strict";
const Base = require("../structures/Base");
/**
* Represents an command framework command
* @prop {Array<String>} aliases An array of command aliases
* @prop {Boolean} argsRequired If arguments are required or not
* @prop {Boolean} caseInsensitive Whether the command label (and aliases) is case insensitive or not
* @prop {Number} cooldown The cooldown between command usage in milliseconds
* @prop {Object} cooldownExclusions A set of factors that limit where cooldowns are active
* @prop {Function | String} cooldownMessage A string or a function that returns a string to show when the command is on cooldown.
* @prop {Number} cooldownReturns Number of times to return a message when the command is used during it's cooldown.
* @prop {Object} defaultSubcommandOptions Default subcommand options.
* @prop {Boolean} deleteCommand Whether to delete the user command message or not
* @prop {String} description A short description of the command
* @prop {Boolean} dmOnly Whether the command is prevented from being used in guilds or not
* @prop {Function | String} errorMessage A string or a function that returns a string to show if the execution of the command handler somehow fails. The function is passed the Message object as a parameter.
* @prop {String} fullDescription A detailed description of the command
* @prop {String} fullLabel The full command label
* @prop {Boolean} guildOnly Whether the command is prevented from being used in Direct Messages or not
* @prop {Boolean} hidden Whether or not the command is hidden from the default help command list.
* @prop {Object} hooks A set of functions to be executed at different times throughout the command's processing
* @prop {Function | String} invalidUsageMessage A string or a function that returns a string to show when a command was improperly used.
* @prop {String} label The command label
* @prop {Command?} parentCommand If this command is also a subcommand, this will refer to its parent Command
* @prop {Function | String} permissionMessage A string or a function that returns a string to show when the user doesn't have permissions to use the command.
* @prop {Object?} reactionButtons An array of objects specifying reaction buttons, or null if no reaction buttons.
* @prop {Number} reactionButtonTimeout Time (in milliseconds) to wait before invalidating the command's reaction buttons
* @prop {Object} requirements A set of factors that limit who can call the command
* @prop {Boolean} restartCooldown Whether or not to restart a command's cooldown every time it's used
* @prop {Object} subcommands Object mapping subcommand labels to Command objects
* @prop {Object} subcommandAliases Object mapping subcommand aliases to their respective subcommand label
* @prop {String} usage Details on how to call the command to show in the default help command
*/
class Command {
/**
* Register a command
* @arg {String} label The command label
* @arg {Function | String | Array<Function | String>} generator A response string, array of functions or strings, or function that generates a string or array of strings when called.
* If a function is passed, the function will be passed a Message object and an array of command arguments. The Message object will have an additional property `prefix`, which is the prefix used in the command.
* `generator(msg, args)`
* @arg {Object} [options] Command options
* @arg {Array<String>} [options.aliases] An array of command aliases
* @arg {Boolean} [options.argsRequired=false] If arguments are required or not
* @arg {Boolean} [options.caseInsensitive=false] Whether the command label (and aliases) is case insensitive or not
* @arg {Number} [options.cooldown] The cooldown between command usage in milliseconds
* @arg {Object} [options.cooldownExclusions={}] A set of factors that limit where cooldowns are active
* @arg {Array<String>} [options.cooldownExclusions.userIDs] An array of user IDs representing users that are not affected by cooldowns.
* @arg {Array<String>} [options.cooldownExclusions.guildIDs] An array of guild IDs representing guilds that are not affected by cooldowns.
* @arg {Array<String>} [options.cooldownExclusions.channelIDs] An array of channel IDs representing channels that are not affected by cooldowns.
* @arg {Function | String | Object} [options.cooldownMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the command is on cooldown. The function is passed the Message object as a parameter.
* @arg {Number} [option.cooldownReturns=0] Number of times to return a message when the command is used during it's cooldown. Once the cooldown expires this is reset. Set this to 0 to always return a message.
* @arg {Object} [options.defaultSubcommandOptions={}] Default subcommand options. This object takes the same options as a normal Command
* @arg {Boolean} [options.deleteCommand=false] Whether to delete the user command message or not
* @arg {String} [options.description="No description"] A short description of the command to show in the default help command
* @arg {Boolean} [options.dmOnly=false] Whether to prevent the command from being used in guilds or not
* @arg {Function | String | Object} [options.errorMessage] A string or `createMessage()` content object, or a function that returns either of those, to show if the execution of the command handler somehow fails. The function is passed the Message object as a parameter.
* @arg {String} [options.fullDescription="No full description"] A detailed description of the command to show in the default help command
* @arg {Boolean} [options.guildOnly=false] Whether to prevent the command from being used in Direct Messages or not
* @arg {Boolean} [options.hidden=false] Whether or not the command should be hidden from the default help command list.
* @arg {Object} [options.hooks] A set of functions to be executed at different times throughout the command's processing
* @arg {Function} [options.hooks.preCommand] A function that is executed before any permission or cooldown checks is made. The function is passed the command message and arguments as parameters.
* @arg {Function} [options.hooks.postCheck] A function that is executed after all checks have cleared, but before the command is executed. The function is passed the command message, arguments, and if command checks were passed as parameters.
* @arg {Function} [options.hooks.postExecution] A function that is executed after the command is executed, regardless of the final failed state of the command. The function is passed the command message, arguments, and if execution succeeded as parameters.
* @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
* @arg {Function | String | Object} [options.invalidUsageMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when a command was improperly used. The function is passed the Message object as a parameter.
* @arg {Function | String | Object} [options.permissionMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the user doesn't have permissions to use the command. The function is passed the Message object as a parameter.
* @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>), filter: Function}>} [options.reactionButtons] An array of objects specifying reaction buttons
* `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
* `type` specifies the type of the reaction button, either "edit" or "cancel"
* `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
* `filter` specifies a function (`function(msg, emoji, userID)`) that filters message reactions. If the function returns false, the reaction is not treated as a valid reaction button response
* @arg {Number} [options.reactionButtonTimeout=60000] Time (in milliseconds) to wait before invalidating the command's reaction buttons
* @arg {Object} [options.requirements] A set of factors that limit who can call the command
* @arg {Function | Array<String>} [options.requirements.userIDs] An array or a function that returns an array of user IDs representing users that can call the command. The function is passed the Message object as a parameter.
* @arg {Function | Object} [options.requirements.permissions] An object or a function that returns an object containing permission keys the user must match to use the command. The function is passed the Message object as a parameter.
* i.e.:
* ```
* {
* "administrator": false,
* "manageMessages": true
* }
* ```
* In the above example, the user must not have administrator permissions, but must have manageMessages to use the command
* @arg {Function | Array<String>} [options.requirements.roleIDs] An array or a function that returns an array of role IDs that would allow a user to use the command. The function is passed the Message object as a parameter.
* @arg {Function | Array<String>} [options.requirements.roleNames] An array or a function that returns an array of role names that would allow a user to use the command. The function is passed the Message object as a parameter.
* @arg {Function} [options.requirements.custom] A function that accepts a message and returns true if the command should be run
* @arg {Boolean} [option.restartCooldown=false] Whether or not to restart a command's cooldown every time it's used.
* @arg {String} [options.usage] Details on how to call the command to show in the default help command
*/
constructor(label, generator, options, parentCommand) {
this.parentCommand = parentCommand;
this.label = label;
this.description = options.description || "No description";
this.fullDescription = options.fullDescription || "No full description";
this.usage = options.usage || "";
this.aliases = options.aliases || [];
this.caseInsensitive = !!options.caseInsensitive;
this.hooks = options.hooks || {};
this.requirements = options.requirements || {};
if(!this.requirements.userIDs) {
this.requirements.userIDs = [];
}
if(!this.requirements.permissions) {
this.requirements.permissions = {};
}
this.deleteCommand = !!options.deleteCommand;
this.argsRequired = !!options.argsRequired;
this.guildOnly = !!options.guildOnly;
this.dmOnly = !!options.dmOnly;
this.cooldown = options.cooldown || 0;
this.cooldownExclusions = options.cooldownExclusions || {};
if(!this.cooldownExclusions.userIDs) {
this.cooldownExclusions.userIDs = [];
}
if(!this.cooldownExclusions.guildIDs) {
this.cooldownExclusions.guildIDs = [];
}
if(!this.cooldownExclusions.channelIDs) {
this.cooldownExclusions.channelIDs = [];
}
this.restartCooldown = !!options.restartCooldown;
this.cooldownReturns = options.cooldownReturns || 0;
this.cooldownMessage = options.cooldownMessage || false;
this.invalidUsageMessage = options.invalidUsageMessage || false;
this.permissionMessage = options.permissionMessage || false;
this.errorMessage = options.errorMessage || "";
this.reactionButtons = options.reactionButtons ? options.reactionButtons.map((button, index) => {
if(typeof button.response === "string") {
button.execute = () => button.response;
return button;
} else if(Array.isArray(button.response)) {
button.responses = button.response.map((item, otherIndex) => {
if(typeof item === "string") {
return () => item;
} else if(typeof item === "function") {
return item;
} else {
throw new Error(`Invalid reaction button response generator (index ${index}:${otherIndex})`);
}
});
button.execute = () => button.responses[Math.floor(Math.random() * button.responses.length)]();
return button;
} else if(typeof button.response === "function") {
button.execute = button.response;
return button;
} else if(button.type === "cancel") {
return button;
} else {
throw new Error(`Invalid reaction button response generator (index ${index})`);
}
}) : null;
this.reactionButtonTimeout = options.reactionButtonTimeout || 60000;
if(this.cooldown !== 0) {
this.usersOnCooldown = new Set();
if(this.restartCooldown) {
this.cooldownTimeouts = {};
}
if(this.cooldownReturns) {
this.cooldownAmounts = {};
}
}
if(typeof generator === "string") {
this.response = generator;
this.execute = () => this.response;
} else if(Array.isArray(generator)) {
this.responses = generator.map((item, index) => {
if(typeof item === "string") {
return () => item;
} else if(typeof item === "function") {
return item;
} else {
throw new Error(`Invalid command response generator (index ${index})`);
}
});
this.execute = () => this.responses[Math.floor(Math.random() * this.responses.length)]();
} else if(typeof generator === "function") {
this.execute = generator;
} else {
throw new Error("Invalid command response generator");
}
this.defaultSubcommandOptions = options.defaultSubcommandOptions || {};
this.subcommands = {};
this.subcommandAliases = {};
this.hidden = !!options.hidden;
}
get fullLabel() {
return `${this.parentCommand ? this.parentCommand.fullLabel + " " : ""}${this.label}`;
}
cooldownCheck(msg) {
// Verify the msg isn't excluded from cooldown checks
if(this.cooldownExclusionCheck(msg)) {
return true;
}
const userID = msg.author.id;
if(this.usersOnCooldown.has(userID)) {
if(this.cooldownReturns) {
this.cooldownAmounts[userID]++;
}
if(this.restartCooldown) {
clearTimeout(this.cooldownTimeouts[userID]);
this.cooldownTimeouts[userID] = setTimeout(() => {
this.usersOnCooldown.delete(userID);
}, this.cooldown);
}
return false;
}
if(this.cooldownReturns) {
this.cooldownAmounts[userID] = 0;
}
this.usersOnCooldown.add(userID);
if(this.restartCooldown) {
this.cooldownTimeouts[userID] = setTimeout(() => {
this.usersOnCooldown.delete(userID);
}, this.cooldown);
} else {
setTimeout(() => {
this.usersOnCooldown.delete(userID);
}, this.cooldown);
}
return true;
}
cooldownExclusionCheck(msg) {
return this.cooldownExclusions.channelIDs.includes(msg.channel.id) || this.cooldownExclusions.userIDs.includes(msg.author.id) || (msg.channel.guild && this.cooldownExclusions.guildIDs.includes(msg.channel.guild.id));
}
async executeCommand(msg, args) {
if(this.hooks.postCheck) {
const response = await this.hooks.postCheck(msg, args, true);
if(response) {
msg = response.msg || msg;
args = response.args || args;
}
}
const ret = this.execute(msg, args);
if(this.hooks.postExecution) {
this.hooks.postExecution(msg, args, true);
}
return ret;
}
async permissionCheck(msg) {
if(this.requirements.custom) {
if(typeof this.requirements.custom !== "function") {
throw new Error("Custom requirement is not a function");
}
if(!(await this.requirements.custom(msg))) {
return false;
}
}
if(this.requirements.userIDs) {
const userIDs = typeof this.requirements.userIDs === "function" ? await this.requirements.userIDs(msg) : this.requirements.userIDs;
if(!Array.isArray(userIDs)) {
throw new Error("User IDs requirement is not an array");
}
if(userIDs.length > 0 && !userIDs.includes(msg.author.id)) {
return false;
}
}
if(msg.channel.guild) {
if(this.dmOnly) {
return false;
}
if(this.requirements.permissions) {
const requiredPermissions = Object.keys(typeof this.requirements.permissions === "function" ? await this.requirements.permissions(msg) : this.requirements.permissions);
if(requiredPermissions.length > 0) {
const permissions = msg.channel.permissionsOf(msg.author.id);
for(const permission of requiredPermissions) {
if(!permissions.has(permission)) {
return false;
}
}
}
}
const roleIDs = msg.member.roles || [];
if(this.requirements.roleIDs) {
const requiredRoleIDs = typeof this.requirements.roleIDs === "function" ? await this.requirements.roleIDs(msg) : this.requirements.roleIDs;
if(!Array.isArray(requiredRoleIDs)) {
throw new Error("Role IDs requirement is not an array");
}
for(const roleID of requiredRoleIDs) {
if(!roleIDs.includes(roleID)) {
return false;
}
}
}
if(this.requirements.roleNames) {
const roleNames = roleIDs.map((roleID) => msg.channel.guild.roles.get(roleID).name);
const requiredRoleNames = typeof this.requirements.roleNames === "function" ? await this.requirements.roleNames(msg) : this.requirements.roleNames;
if(!Array.isArray(roleNames)) {
throw new Error("Role names requirement is not an array");
}
for(const roleName of requiredRoleNames) {
if(!roleNames.includes(roleName)) {
return false;
}
}
}
} else if(this.guildOnly) {
return false;
}
return true;
}
async process(args, msg) {
const shouldDelete = this.deleteCommand && msg.channel.guild && msg.channel.permissionsOf(msg._client.user.id).has("manageMessages");
if(this.hooks.preCommand) {
const response = await this.hooks.preCommand(msg, args);
if(response) {
msg = response.msg || msg;
args = response.args || args;
}
}
let reply;
if(this.cooldown !== 0 && !this.cooldownCheck(msg)) {
if(this.hooks.postCheck) {
const response = await this.hooks.postCheck(msg, args, true);
if(response) {
msg = response.msg || msg;
args = response.args || args;
}
}
if(this.cooldownMessage && (!this.cooldownReturns || this.cooldownAmounts[msg.author.id] <= this.cooldownReturns)) {
reply = typeof this.cooldownMessage === "function" ? await this.cooldownMessage(msg) : this.cooldownMessage;
if(reply) {
msg.channel.createMessage(reply);
}
}
return;
}
if(!await this.permissionCheck(msg)) {
if(this.hooks.postCheck) {
const response = await this.hooks.postCheck(msg, args, false);
if(response) {
msg = response.msg || msg;
args = response.args || args;
}
}
if(shouldDelete) {
msg.delete();
}
reply = typeof this.permissionMessage === "function" ? await this.permissionMessage(msg) : this.permissionMessage;
if(reply) {
msg.channel.createMessage(reply);
}
return;
}
if(args.length === 0) {
if(shouldDelete) {
msg.delete();
}
if(this.argsRequired) {
if(this.hooks.postCheck) {
const response = await this.hooks.postCheck(msg, args, true);
if(response) {
msg = response.msg || msg;
args = response.args || args;
}
}
reply = typeof this.invalidUsageMessage === "function" ? await this.invalidUsageMessage(msg) : this.invalidUsageMessage;
if(reply) {
if(typeof reply === "string") {
reply = {
content: reply
};
}
if(reply.content) {
reply.content = reply.content.replace(/%prefix%/g, msg.prefix).replace(/%label%/g, this.fullLabel);
}
msg.channel.createMessage(reply);
}
return;
}
return this.executeCommand(msg, args);
}
const label = this.subcommandAliases[args[0]] || args[0];
let subcommand;
if((subcommand = this.subcommands[label]) !== undefined || ((subcommand = this.subcommands[label.toLowerCase()]) !== undefined && subcommand.caseInsensitive)) {
msg.command = subcommand; // eslint-disable-line require-atomic-updates
return subcommand.process(args.slice(1), msg);
} else {
if(shouldDelete) {
msg.delete();
}
return this.executeCommand(msg, args);
}
}
/**
* Register a subcommand
* @arg {String} label The subcommand label
* @arg {Function | String | Array<Function | String>} generator A response string, array of functions or strings, or function that generates a string or array of strings when called.
* If a function is passed, the function will be passed a Message object and an array of subcommand arguments. The Message object will have an additional property `prefix`, which is the prefix used in the subcommand.
* `generator(msg, args)`
* @arg {Object} [options] Command options
* @arg {Array<String>} [options.aliases] An array of command aliases
* @arg {Boolean} [options.argsRequired=false] If arguments are required or not
* @arg {Boolean} [options.caseInsensitive=false] Whether the command label (and aliases) is case insensitive or not
* @arg {Number} [options.cooldown] The cooldown between command usage in milliseconds
* @arg {Object} [options.cooldownExclusions={}] A set of factors that limit where cooldowns are active
* @arg {Array<String>} [options.cooldownExclusions.userIDs] An array of user IDs representing users that are not affected by cooldowns.
* @arg {Array<String>} [options.cooldownExclusions.guildIDs] An array of guild IDs representing guilds that are not affected by cooldowns.
* @arg {Array<String>} [options.cooldownExclusions.channelIDs] An array of channel IDs representing channels that are not affected by cooldowns.
* @arg {Function | String | Object} [options.cooldownMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the command is on cooldown
* @arg {Number} [option.cooldownReturns=0] Number of times to return a message when the command is used during it's cooldown. Once the cooldown expires this is reset. Set this to 0 to always return a message.
* @arg {Object} [options.defaultSubcommandOptions={}] Default subcommand options. This object takes the same options as a normal Command
* @arg {Boolean} [options.deleteCommand=false] Whether to delete the user command message or not
* @arg {String} [options.description="No description"] A short description of the command to show in the default help command
* @arg {Boolean} [options.dmOnly=false] Whether to prevent the command from being used in guilds or not
* @arg {Function | String | Object} [options.errorMessage] A string or `createMessage()` content object, or a function that returns either of those, to show if the execution of the command handler somehow fails.
* @arg {String} [options.fullDescription="No full description"] A detailed description of the command to show in the default help command
* @arg {Boolean} [options.guildOnly=false] Whether to prevent the command from being used in Direct Messages or not
* @arg {Boolean} [options.hidden=false] Whether or not the command should be hidden from the default help command list
* @arg {Object} [options.hooks] A set of functions to be executed at different times throughout the command's processing
* @arg {Function} [options.hooks.preCommand] A function that is executed before any permission or cooldown checks is made. The function is passed the command message and arguments as parameters.
* @arg {Function} [options.hooks.postCheck] A function that is executed after all checks have cleared, but before the command is executed. The function is passed the command message, arguments, and if command checks were passed as parameters.
* @arg {Function} [options.hooks.postExecution] A function that is executed after the command is executed, regardless of the final failed state of the command. The function is passed the command message, arguments, and if execution succeeded as parameters.
* @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
* @arg {Function | String | Object} [options.invalidUsageMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when a command was improperly used
* @arg {Function | String | Object} [options.permissionMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the user doesn't have permissions to use the command
* @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>)}>} [options.reactionButtons] An array of objects specifying reaction buttons
* `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
* `type` specifies the type of the reaction button, either "edit" or "cancel"
* `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
* `filter` specifies a function (`function(msg, emoji, userID)`) that filters message reactions. If the function returns false, the reaction is not treated as a valid reaction button response
* @arg {Number} [options.reactionButtonTimeout=60000] Time (in milliseconds) to wait before invalidating the command's reaction buttons
* @arg {Object} [options.requirements] A set of factors that limit who can call the command
* @arg {Function | Array<String>} [options.requirements.userIDs] An array or a function that returns an array of user IDs representing users that can call the command
* @arg {Function | Object} [options.requirements.permissions] An object or a function that returns an object containing permission keys the user must match to use the command
* i.e.:
* ```
* {
* "administrator": false,
* "manageMessages": true
* }
* ```
* In the above example, the user must not have administrator permissions, but must have manageMessages to use the command
* @arg {Function | Array<String>} [options.requirements.roleIDs] An array or a function that returns an array of role IDs that would allow a user to use the command
* @arg {Function | Array<String>} [options.requirements.roleNames] An array or a function that returns an array of role names that would allow a user to use the command
* @arg {Function} [options.requirements.custom] A function that accepts a message and returns true if the command should be run
* @arg {Boolean} [option.restartCooldown=false] Whether or not to restart a command's cooldown every time it's used.
* @arg {String} [options.usage] Details on how to call the command to show in the default help command
* @returns {Command}
*/
registerSubcommand(label, generator, options = {}) {
if(label.includes(" ")) {
throw new Error("Subcommand label may not have spaces");
}
if(this.subcommands[label]) {
throw new Error("You have already registered a subcommand for " + label);
}
options.defaultSubcommandOptions = options.defaultSubcommandOptions || {};
for(const key in this.defaultSubcommandOptions) {
if(this.defaultSubcommandOptions.hasOwnProperty(key) && options[key] === undefined) {
options[key] = this.defaultSubcommandOptions[key];
options.defaultSubcommandOptions[key] = this.defaultSubcommandOptions[key];
}
}
label = options.caseInsensitive === true ? label.toLowerCase() : label;
this.subcommands[label] = new Command(label, generator, options, this);
if(options.aliases) {
options.aliases.forEach((alias) => {
this.subcommandAliases[alias] = label;
});
}
return this.subcommands[label];
}
/**
* Register an alias for a subcommand
* @arg {String} alias The alias
* @arg {String} label The original subcommand label
*/
registerSubcommandAlias(alias, label) {
if(!this.subcommands[label]) {
throw new Error("No subcommand registered for " + label);
}
if(this.subcommandAliases[alias]) {
throw new Error(`Alias ${label} already registered`);
}
this.subcommandAliases[alias] = label;
this.subcommands[label].aliases.push(alias);
}
/**
* Unregister a subcommand
* @arg {String} label The subcommand label
*/
unregisterSubcommand(label) {
const original = this.subcommandAliases[label];
if(original) {
this.subcommands[original].aliases.splice(this.subcommands[original].aliases.indexOf(label), 1);
delete this.subcommandAliases[label];
} else {
delete this.subcommands[label];
}
}
toString() {
return `[Command ${this.label}]`;
}
toJSON(props = []) {
return Base.prototype.toJSON.call(this, [
"parentCommand",
"label",
"description",
"fullDescription",
"usage",
"aliases",
"caseInsensitive",
"hooks",
"requirements",
"deleteCommand",
"argsRequired",
"guildOnly",
"dmOnly",
"cooldown",
"cooldownExclusions",
"restartCooldown",
"cooldownReturns",
"cooldownMessage",
"invalidUsageMessage",
"permissionMessage",
"errorMessage",
"reactionButtons",
"reactionButtonTimeout",
"execute",
"defaultSubcommandOptions",
"subcommands",
"subcommandAliases",
"hidden",
...props
]);
}
}
module.exports = Command;

455
node_modules/eris/lib/command/CommandClient.js generated vendored Normal file
View File

@ -0,0 +1,455 @@
"use strict";
const Client = require("../Client");
const Command = require("./Command");
const Message = require("../structures/Message");
/**
* Represents an Eris client with the command framework
* @extends Client
* @prop {Object} commands Object mapping command labels to Command objects
* @prop {Object} commandAliases Object mapping command label aliases to command labels
* @prop {Object} commandOptions Command options
* @prop {Object} guildPrefixes Object mapping guild IDs to guild specific prefix or arrays of guild-specific prefixes
*/
class CommandClient extends Client {
/**
* Create a CommandClient
* @arg {String} token Bot token
* @arg {Object} options Eris options (same as Client)
* @arg {Object} [commandOptions] Command options
* @arg {Function} [argsSplitter] The function used to split args. The function is given a string with the contents of the command message (without the prefix) and should return an array of strings. By default, args are split by consecutive whitespace
* @arg {Boolean} [commandOptions.defaultHelpCommand=true] Whether to register the default help command or not
* @arg {String} [commandOptions.description="An Eris-based Discord bot"] The description to show in the default help command
* @arg {Boolean} [commandOptions.ignoreBots=true] Whether to ignore bot accounts or not
* @arg {Boolean} [commandOptions.ignoreSelf=true] Whether to ignore the bot's own account or not
* @arg {String} [commandOptions.name="<Bot username>"] The bot name to show in the default help command
* @arg {String} [commandOptions.owner="an unknown user"] The owner to show in the default help command
* @arg {String | Array<String>} [commandOptions.prefix="@mention "] The bot prefix. Can be either an array of prefixes or a single prefix. "@mention" will be automatically replaced with the bot's actual mention
* @arg {Object} [commandOptions.defaultCommandOptions={}] Default command options. This object takes the same options as a normal Command
*/
constructor(token, options, commandOptions) {
super(token, options);
this.commandOptions = Object.assign({
argsSplitter: (str) => str.split(/\s+/g),
defaultHelpCommand: true,
description: "An Eris-based Discord bot",
ignoreBots: true,
ignoreSelf: true,
name: null,
owner: "an unknown user",
prefix: "@mention ",
defaultCommandOptions: {}
}, commandOptions);
this.guildPrefixes = {};
this.commands = {};
this.commandAliases = {};
this.activeMessages = {};
this.once("shardPreReady", () => {
this.preReady = true;
if(!this.commandOptions.name) {
this.commandOptions.name = `**${this.user.username}**`;
}
if(Array.isArray(this.commandOptions.prefix)) {
for(let i = 0; i < this.commandOptions.prefix.length; ++i) {
this.commandOptions.prefix[i] = this.commandOptions.prefix[i].replace(/@mention/g, this.user.mention);
}
} else {
this.commandOptions.prefix = this.commandOptions.prefix.replace(/@mention/g, this.user.mention);
}
for(const key of Object.keys(this.guildPrefixes)) {
if(Array.isArray(this.guildPrefixes[key])) {
for(let i = 0; i < this.guildPrefixes[key].length; ++i) {
this.guildPrefixes[key][i] = this.guildPrefixes[key][i].replace(/@mention/g, this.user.mention);
}
} else {
this.guildPrefixes[key] = this.guildPrefixes[key].replace(/@mention/g, this.user.mention);
}
}
});
this.on("messageCreate", this.onMessageCreate);
this.on("messageReactionAdd", this.onMessageReactionEvent);
this.on("messageReactionRemove", this.onMessageReactionEvent);
if(this.commandOptions.defaultHelpCommand) {
this.registerCommand("help", async (msg, args) => {
let result = "";
if(args.length > 0) {
let cur = this.commands[this.commandAliases[args[0]] || args[0]];
if(!cur) {
return "Command not found";
}
let {label} = cur;
for(let i = 1; i < args.length; ++i) {
cur = cur.subcommands[cur.subcommandAliases[args[i]] || args[i]];
if(!cur) {
return "Command not found";
}
label += ` ${cur.label}`;
}
result += `**${msg.prefix}${label}** ${cur.usage}\n${cur.fullDescription}`;
if(cur.aliases.length > 0) {
result += `\n\n**Aliases:** ${cur.aliases.join(", ")}`;
}
const subcommands = Object.keys(cur.subcommands);
if(subcommands.length > 0) {
result += "\n\n**Subcommands:**";
for(const subLabel of subcommands) {
if(cur.subcommands.hasOwnProperty(subLabel) && await cur.subcommands[subLabel].permissionCheck(msg)) {
result += `\n **${subLabel}** - ${cur.subcommands[subLabel].description}`;
}
}
}
} else {
result += `${this.commandOptions.name} - ${this.commandOptions.description}\n`;
if(this.commandOptions.owner) {
result += `by ${this.commandOptions.owner}\n`;
}
result += "\n**Commands:**\n";
for(const label in this.commands) {
if(this.commands.hasOwnProperty(label) && this.commands[label] && await this.commands[label].permissionCheck(msg) && !this.commands[label].hidden) {
result += ` **${msg.prefix}${label}** - ${this.commands[label].description}\n`;
}
}
result += `\nType ${msg.prefix}help <command> for more info on a command.`;
}
return result;
}, {
description: "This help text",
fullDescription: "This command is used to view information of different bot commands, including this one."
});
if(!this.commandOptions.defaultCommandOptions.invalidUsageMessage) {
this.commandOptions.defaultCommandOptions.invalidUsageMessage = "Invalid usage. Do `%prefix%help %label%` to view proper usage.";
}
} else if(!this.commandOptions.defaultCommandOptions.invalidUsageMessage) {
this.commandOptions.defaultCommandOptions.invalidUsageMessage = "Invalid usage.";
}
}
checkPrefix(msg) {
let prefixes = this.commandOptions.prefix;
if(msg.channel.guild !== undefined && this.guildPrefixes[msg.channel.guild.id] !== undefined) {
prefixes = this.guildPrefixes[msg.channel.guild.id];
}
if(typeof prefixes === "string") {
return msg.content.replace(/<@!/g, "<@").startsWith(prefixes) && prefixes;
} else if(Array.isArray(prefixes)) {
return prefixes.find((prefix) => msg.content.replace(/<@!/g, "<@").startsWith(prefix));
}
throw new Error(`Unsupported prefix format | ${prefixes}`);
}
/**
* Checks the command client for a command based on the provided message
* @arg {Message} msg The message object from the message create event
*/
async onMessageCreate(msg) {
if(!this.ready) {
return;
}
if(!msg.author) {
this.emit("warn", `Message ${msg.id} has author=${msg.author} | Channel ${msg.channel.id}, timestamp ${Date.now()}`);
return;
}
msg.command = false;
if((!this.commandOptions.ignoreSelf || msg.author.id !== this.user.id) && (!this.commandOptions.ignoreBots || !msg.author.bot) && (msg.prefix = this.checkPrefix(msg))) {
const args = this.commandOptions.argsSplitter(msg.content.replace(/<@!/g, "<@").substring(msg.prefix.length).trim());
const label = args.shift();
const command = this.resolveCommand(label);
if(command !== undefined) {
msg.command = command;
try {
let resp = await msg.command.process(args, msg);
if(resp != null) {
if(!(resp instanceof Message)) {
resp = await this.createMessage(msg.channel.id, resp); // eslint-disable-line require-atomic-updates
}
if(msg.command.reactionButtons) {
msg.command.reactionButtons.forEach((button) => resp.addReaction(button.emoji));
this.activeMessages[resp.id] = {
args: args,
command: msg.command,
timeout: setTimeout(() => {
this.unwatchMessage(resp.id, resp.channel.id);
}, msg.command.reactionButtonTimeout)
};
}
}
if(msg.command.hooks.postCommand) {
msg.command.hooks.postCommand(msg, args, resp);
}
} catch(err) {
this.emit("error", err);
if(msg.command.hooks.postExecution) {
msg.command.hooks.postExecution(msg, args, false);
}
let newMsg;
if(msg.command.errorMessage) {
try {
if(typeof msg.command.errorMessage === "function") {
const reply = await msg.command.errorMessage(msg, err);
if(reply !== undefined) {
newMsg = await this.createMessage(msg.channel.id, reply);
}
} else {
newMsg = await this.createMessage(msg.channel.id, msg.command.errorMessage);
}
} catch(err) {
this.emit("error", err);
}
}
if(msg.command.hooks.postCommand) {
msg.command.hooks.postCommand(msg, args, newMsg);
}
}
}
}
}
async onMessageReactionEvent(msg, emoji, reactor) {
const userID = typeof reactor === "object" ? reactor.id : reactor;
if(!this.ready || userID === this.user.id || !(msg.content || msg.embeds || msg.attachments)) {
return;
}
emoji = emoji.id ? `${emoji.name}:${emoji.id}` : emoji.name;
const activeMessage = this.activeMessages[msg.id];
if(activeMessage && activeMessage.command.reactionButtons) {
const action = activeMessage.command.reactionButtons.find((button) => button.emoji === emoji);
if(!action) {
return;
}
if(action.filter && !action.filter(msg, emoji, userID)) {
return;
}
switch(action.type) {
case "cancel": {
this.unwatchMessage(msg.id, msg.channel.guild && msg.channel.id);
try {
const resp = await action.execute(msg, activeMessage.args, userID);
if(resp != null) {
await this.editMessage(msg.channel.id, msg.id, resp);
}
} catch(err) {} // eslint-disable-line no-empty
break;
}
case "edit":
default: {
try {
const resp = await action.execute(msg, activeMessage.args, userID);
if(resp != null) {
await this.editMessage(msg.channel.id, msg.id, resp);
}
} catch(err) {} // eslint-disable-line no-empty
break;
}
}
}
}
/**
* Register a command
* @arg {String} label The command label
* @arg {Function | String | Array<Function | String>} generator A response string, array of functions or strings, or function that generates a string or array of strings when called.
* If a function is passed, the function will be passed a Message object and an array of command arguments. The Message object will have an additional property `prefix`, which is the prefix used in the command.
* `generator(msg, args)`
* @arg {Object} [options] Command options
* @arg {Array<String>} [options.aliases] An array of command aliases
* @arg {Boolean} [options.argsRequired=false] If arguments are required or not
* @arg {Boolean} [options.caseInsensitive=false] Whether the command label (and aliases) is case insensitive or not
* @arg {Number} [options.cooldown] The cooldown between command usage in milliseconds
* @arg {Object} [options.cooldownExclusions={}] A set of factors that limit where cooldowns are active
* @arg {Array<String>} [options.cooldownExclusions.userIDs] An array of user IDs representing users that are not affected by cooldowns.
* @arg {Array<String>} [options.cooldownExclusions.guildIDs] An array of guild IDs representing guilds that are not affected by cooldowns.
* @arg {Array<String>} [options.cooldownExclusions.channelIDs] An array of channel IDs representing channels that are not affected by cooldowns.
* @arg {Function | String} [options.cooldownMessage] A string or a function that returns a string to show when the command is on cooldown
* @arg {Number} [option.cooldownReturns=0] Number of times to return a message when the command is used during it's cooldown. Once the cooldown expires this is reset. Set this to 0 to always return a message.
* @arg {Object} [options.defaultSubcommandOptions={}] Default subcommand options. This object takes the same options as a normal Command
* @arg {Boolean} [options.deleteCommand=false] Whether to delete the user command message or not
* @arg {String} [options.description="No description"] A short description of the command to show in the default help command
* @arg {Boolean} [options.dmOnly=false] Whether to prevent the command from being used in guilds or not
* @arg {Function | String} [options.errorMessage] A string or a function that returns a string to show if the execution of the command handler somehow fails. The function is passed the command message and the error as parameters.
* @arg {String} [options.fullDescription="No full description"] A detailed description of the command to show in the default help command
* @arg {Boolean} [options.guildOnly=false] Whether to prevent the command from being used in Direct Messages or not
* @arg {Boolean} [options.hidden=false] Whether or not the command should be hidden from the default help command list
* @arg {Object} [options.hooks] A set of functions to be executed at different times throughout the command's processing
* @arg {Function} [options.hooks.preCommand] A function that is executed before any permission or cooldown checks is made. The function is passed the command message and arguments as parameters.
* @arg {Function} [options.hooks.postCheck] A function that is executed after all checks have cleared, but before the command is executed. The function is passed the command message, arguments, and if command checks were passed as parameters.
* @arg {Function} [options.hooks.postExecution] A function that is executed after the command is executed, regardless of the final failed state of the command. The function is passed the command message, arguments, and if execution succeeded as parameters.
* @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
* @arg {Function | String} [options.invalidUsageMessage] A string or a function that returns a string to show when a command was improperly used
* @arg {Function | String} [options.permissionMessage] A string or a function that returns a string to show when the user doesn't have permissions to use the command
* @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>)}>} [options.reactionButtons] An array of objects specifying reaction buttons
* `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
* `type` specifies the type of the reaction button, either "edit" or "cancel"
* `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
* `filter` specifies a function (`function(msg, emoji, userID)`) that filters message reactions. If the function returns false, the reaction is not treated as a valid reaction button response
* @arg {Number} [options.reactionButtonTimeout=60000] Time (in milliseconds) to wait before invalidating the command's reaction buttons
* @arg {Object} [options.requirements] A set of factors that limit who can call the command
* @arg {Function | Array<String>} [options.requirements.userIDs] An array or a function that returns an array of user IDs representing users that can call the command
* @arg {Function | Object} [options.requirements.permissions] An object or a function that returns an object containing permission keys the user must match to use the command
* i.e.:
* ```
* {
* "administrator": false,
* "manageMessages": true
* }
* ```
* In the above example, the user must not have administrator permissions, but must have manageMessages to use the command
* @arg {Function | Array<String>} [options.requirements.roleIDs] An array or a function that returns an array of role IDs that would allow a user to use the command
* @arg {Function | Array<String>} [options.requirements.roleNames] An array or a function that returns an array of role names that would allow a user to use the command
* @arg {Function} [options.requirements.custom] A function that accepts a message and returns true if the command should be run
* @arg {Boolean} [option.restartCooldown=false] Whether or not to restart a command's cooldown every time it's used.
* @arg {String} [options.usage] Details on how to call the command to show in the default help command
* @returns {Command}
*/
registerCommand(label, generator, options = {}) {
if(label.includes(" ")) {
throw new Error("Command label may not have spaces");
}
let lowercaseCommand = label.toLowerCase();
if(this.commands[label] || (this.commands[lowercaseCommand] && this.commands[lowercaseCommand].caseInsensitive)) {
throw new Error(`You have already registered a command for ${label}`);
}
// Aliases are not deleted when deleting commands
let command = this.commandAliases[label]; // Just to make the following if statement less messy
lowercaseCommand = this.commandAliases[label.toLowerCase()];
if(this.commands[command] || (this.commands[lowercaseCommand] && this.commands[lowercaseCommand].caseInsensitive)) {
throw new Error(`Alias ${label} already registered`);
}
options.defaultSubcommandOptions = options.defaultSubcommandOptions || {};
for(const key in this.commandOptions.defaultCommandOptions) {
if(this.commandOptions.defaultCommandOptions.hasOwnProperty(key) && options[key] === undefined) {
options[key] = this.commandOptions.defaultCommandOptions[key];
options.defaultSubcommandOptions[key] = this.commandOptions.defaultCommandOptions[key];
}
}
label = options.caseInsensitive === true ? label.toLowerCase() : label;
if(this.commands[label]) {
throw new Error(`You have already registered a command for ${label}`);
}
command = this.commandAliases[label];
if(this.commands[command]) {
throw new Error(`Alias ${command} already registered`);
}
if(options.aliases) {
options.aliases.forEach((alias) => {
lowercaseCommand = alias.toLowerCase();
if(this.commands[alias] || (this.commands[lowercaseCommand] && this.commands[lowercaseCommand].caseInsensitive)) {
throw new Error(`You have already registered a command for alias ${alias}`);
}
command = this.commandAliases[alias];
lowercaseCommand = this.commandAliases[alias.toLowerCase()];
if(this.commands[command] || (this.commands[lowercaseCommand] && this.commands[lowercaseCommand].caseInsensitive)) {
throw new Error(`Alias ${alias} already registered`);
}
alias = options.caseInsensitive === true ? alias.toLowerCase() : alias;
if(this.commands[alias]) {
throw new Error(`You have already registered a command for alias ${alias}`);
}
command = this.commandAliases[alias];
if(this.commands[command]) {
throw new Error(`Alias ${alias} already registered`);
}
this.commandAliases[alias] = label;
});
}
this.commands[label] = new Command(label, generator, options);
return this.commands[label];
}
/**
* Register an alias for a command
* @arg {String} alias The alias
* @arg {String} label The original command label
*/
registerCommandAlias(alias, label) {
let caseInsensitiveLabel = false;
if(!this.commands[label] && !(this.commands[(label = label.toLowerCase())] && (caseInsensitiveLabel = this.commands[label.toLowerCase()].caseInsensitive))) {
throw new Error(`No command registered for ${label}`);
}
alias = caseInsensitiveLabel === true ? alias.toLowerCase() : alias;
if(this.commandAliases[alias]) {
throw new Error(`Alias ${alias} already registered`);
}
this.commandAliases[alias] = label;
this.commands[label].aliases.push(alias);
}
/**
* Register a prefix override for a specific guild
* @arg {String} guildID The ID of the guild to override prefixes for
* @arg {String | Array} prefix The bot prefix. Can be either an array of prefixes or a single prefix. "@mention" will be automatically replaced with the bot's actual mention
*/
registerGuildPrefix(guildID, prefix) {
if(!this.preReady) {
this.guildPrefixes[guildID] = prefix;
} else if(Array.isArray(prefix)) {
for(let i = 0; i < prefix.length; ++i) {
prefix[i] = prefix[i].replace(/@mention/g, this.user.mention);
}
this.guildPrefixes[guildID] = prefix;
} else {
this.guildPrefixes[guildID] = prefix.replace(/@mention/g, this.user.mention);
}
}
resolveCommand(label) {
label = this.commandAliases[label] || label;
let command = this.commands[label];
if(command) {
return command;
}
label = label.toLowerCase();
label = this.commandAliases[label] || label;
command = this.commands[label];
if(command && command.caseInsensitive) {
return command;
}
}
/**
* Unregister a command
* @arg {String} label The command label
*/
unregisterCommand(label) {
const original = this.commandAliases[label];
if(original) {
this.commands[original].aliases.splice(this.commands[original].aliases.indexOf(label), 1);
delete this.commandAliases[label];
} else {
delete this.commands[label];
}
}
unwatchMessage(id, channelID) {
delete this.activeMessages[id];
if(channelID) {
this.removeMessageReactions(channelID, id).catch(() => {});
}
}
toString() {
return `[CommandClient ${this.user.id}]`;
}
toJSON(props = []) {
return super.toJSON([
"commandOptions",
"guildPrefixes",
"commands",
"commandAliases",
"activeMessages",
...props
]);
}
}
module.exports = CommandClient;