diff --git a/index.ts b/index.ts index c37f831..a697b1e 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,7 @@ import { Bot } from "./src/Bot.js"; import { Command } from "./src/Command.js"; +import { ParameterResolvable, ParameterType } from "./src/Parameter.js"; import { MessageEmbed } from "discord.js"; export default Bot; -export { Bot, Command, MessageEmbed }; +export { Bot, Command, MessageEmbed, ParameterResolvable, ParameterType }; diff --git a/package.json b/package.json index 1e951fd..e71aa13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commandbot", - "version": "3.0.0-beta1", + "version": "3.0.0-beta2", "description": "A framework that helps you create your own Discord bot easier.", "main": "dist/cjs/index.js", "module": "dist/mjs/index.js", diff --git a/src/Bot.ts b/src/Bot.ts index eed646d..d2da38c 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -42,7 +42,7 @@ export class Bot extends EventEmitter { * @param {ConstructorOptions} options - instance properties * @param {string} options.name - name of your bot * @param {string} options.prefix - prefix used to call commands - * @param {string} [options.argumentSeparator=','] - used to get arguments from message + * @param {string} [options.argumentSeparator=','] - used to get parameters from message * @param {ClientOptions} [options.clientOptions] - client options from Discord.js * @param {string} [options.token] - bot token from Discord Developer Portal */ @@ -110,7 +110,7 @@ export class Bot extends EventEmitter { if (cmdMsg) { this.emit("COMMAND", m, cmdMsg); try { - await cmdMsg.command.start(m, cmdMsg.arguments); + await cmdMsg.command.start(m, cmdMsg.parameters); } catch (e) { if (e instanceof PermissionsError) { this.messages.system.send( diff --git a/src/Command.ts b/src/Command.ts index 95a8ffa..3af3394 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -5,19 +5,23 @@ import { Message, MessageEmbed, } from "discord.js"; -import { MissingArgumentError, PermissionsError } from "./Error.js"; -import { Argument, ArgumentResolvable, ProcessArgument } from "./Arguments.js"; +import { MissingParameterError, PermissionsError } from "./Error.js"; +import { + Parameter, + ParameterResolvable, + ProcessArgument, +} from "./Parameter.js"; //TYPE DEFINITIONS type GetMode = "ALL" | "PREFIX" | "NO_PREFIX"; type PermissionCheckTypes = "ALL" | "ANY"; export interface CommandMessageStructure { command: Command; - arguments: string[]; + parameters: string[]; } interface CommandBuilder { name: string; - arguments?: Argument[]; + parameters?: Parameter[]; aliases?: string[] | string; keywords?: string[] | string; description?: string; @@ -27,7 +31,7 @@ interface CommandBuilder { visible?: boolean; function: ( message?: Message, - cmdArguments?: ArgumentResolvable[] + cmdParams?: ParameterResolvable[] ) => void | string | MessageEmbed | Promise; } interface PhraseOccurrenceData { @@ -38,7 +42,7 @@ interface PhraseOccurrenceData { //CLASSES export class Command { name: string; - arguments: Argument[]; + parameters: Parameter[]; aliases: string[]; keywords: string[]; description: string; @@ -48,7 +52,7 @@ export class Command { visible: boolean; private function: ( message?: Message, - cmdArguments?: ArgumentResolvable[] + cmdParams?: ParameterResolvable[] ) => void | string | MessageEmbed | Promise; /** @@ -67,7 +71,7 @@ export class Command { */ constructor(options: CommandBuilder) { this.name = options.name.split(" ").join("_"); - this.arguments = options.arguments || []; + this.parameters = options.parameters || []; this.aliases = Command.processPhrase(options.aliases); this.keywords = Command.processPhrase(options.keywords); this.description = options.description || "No description"; @@ -84,10 +88,10 @@ export class Command { /** * Starts the command * @param {Message} [message] - a *Message* object used to check caller's permissions. It will get passed to the execution function (specified in *function* property of command's constructor) - * @param {string[]} [cmdArguments] - list of processed arguments passed in a Discord message + * @param {string[]} [cmdParams] - list of processed parameters passed in a Discord message * @returns *Promise* */ - async start(message?: Message, cmdArguments?: string[]): Promise { + async start(message?: Message, cmdParams?: string[]): Promise { const memberPermissions: Readonly = message?.member?.permissions || new Permissions(0); if ( @@ -95,11 +99,11 @@ export class Command { ? memberPermissions.has(this.permissions, true) : memberPermissions.any(this.permissions, true) ) { - let inputArguments: ArgumentResolvable[] = cmdArguments || []; - if (this.arguments.length > 0) { - this.arguments.map((a, i) => { + let inputArguments: ParameterResolvable[] = cmdParams || []; + if (this.parameters.length > 0) { + this.parameters.map((a, i) => { if (!inputArguments[i] && !a.optional) { - throw new MissingArgumentError(a); + throw new MissingParameterError(a); } else if (inputArguments[i]) { inputArguments[i] = ProcessArgument( inputArguments[i] as string, @@ -108,7 +112,7 @@ export class Command { } }); } - const fnResult = await this.function(message, cmdArguments); + const fnResult = await this.function(message, cmdParams); if (typeof fnResult == "string") { await message?.reply(fnResult); } else if (fnResult instanceof MessageEmbed) { @@ -141,7 +145,7 @@ export class Command { private generateUsageFromArguments(): string { let usageTemplate: string = ""; - this.arguments.map((e) => { + this.parameters.map((e) => { usageTemplate += `[${e.name} (${e.type}${ e.optional ? ", optional" : "" })] `; @@ -254,7 +258,7 @@ export class CommandManager { } /** - * Fetches command and arguments from the given *Message* object + * Fetches command and parameters from the given *Message* object * @param {Message} message - *Message* object * @returns *CommandMessagesStructure* | *null* */ @@ -271,20 +275,20 @@ export class CommandManager { const command = this.get(name, prefix ? "PREFIX" : "NO_PREFIX"); if (command) { const argumentsText = content.replace(name, ""); - const argumentsList = argumentsText + const paramsList = argumentsText .split(this.argumentSeparator) .map((a) => { return a.replace(" ", ""); }); if ( - (argumentsList[0] == "" || argumentsList[0] == " ") && - argumentsList.length == 1 + (paramsList[0] == "" || paramsList[0] == " ") && + paramsList.length == 1 ) { - argumentsList.splice(0, 1); + paramsList.splice(0, 1); } return { command: command, - arguments: argumentsList, + parameters: paramsList, }; } else { return null; diff --git a/src/Error.ts b/src/Error.ts index 1c5ddb9..0a20613 100644 --- a/src/Error.ts +++ b/src/Error.ts @@ -1,6 +1,6 @@ import { Command } from "./Command.js"; import { GuildMember } from "discord.js"; -import { Argument, ArgumentType } from "./Arguments.js"; +import { Parameter, ParameterType } from "./Parameter.js"; export class PermissionsError extends Error { private command: Command; @@ -15,26 +15,26 @@ export class PermissionsError extends Error { } } -export class ArgumentTypeError extends TypeError { +export class ParameterTypeError extends TypeError { private stringContent: string; - private type: ArgumentType; - constructor(s: string, type: ArgumentType) { + private type: ParameterType; + constructor(s: string, type: ParameterType) { super(); this.stringContent = s; this.type = type; } toString() { - return `Argument "${this.stringContent}" cannot be converted to ${this.type}`; + return `Parameter "${this.stringContent}" cannot be converted to ${this.type}`; } } -export class MissingArgumentError extends ReferenceError { - private argument: Argument; - constructor(a: Argument) { +export class MissingParameterError extends ReferenceError { + private argument: Parameter; + constructor(a: Parameter) { super(); this.argument = a; } toString() { - return `Your request is missing a "${this.argument.name}" argument which is not optional`; + return `Your request is missing a "${this.argument.name}" parameter which is not optional`; } } diff --git a/src/Arguments.ts b/src/Parameter.ts similarity index 63% rename from src/Arguments.ts rename to src/Parameter.ts index 09a737e..b934068 100644 --- a/src/Arguments.ts +++ b/src/Parameter.ts @@ -1,30 +1,30 @@ -import { ArgumentTypeError } from "./Error.js"; +import { ParameterTypeError } from "./Error.js"; -export type ArgumentType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT"; -export type ArgumentResolvable = string | boolean | number; -export interface Argument { +export type ParameterType = "STRING" | "BOOLEAN" | "INTEGER" | "FLOAT"; +export type ParameterResolvable = string | boolean | number; +export interface Parameter { name: string; optional: boolean; - type: ArgumentType; + type: ParameterType; } export const ProcessArgument = ( a: string, - type: ArgumentType -): ArgumentResolvable => { + type: ParameterType +): ParameterResolvable => { switch (type) { case "STRING": return a; case "INTEGER": const numI = parseInt(a); if (isNaN(numI)) { - throw new ArgumentTypeError(a, type); + throw new ParameterTypeError(a, type); } else { return numI; } case "FLOAT": const numF = parseFloat(a); if (isNaN(numF)) { - throw new ArgumentTypeError(a, type); + throw new ParameterTypeError(a, type); } else { return numF; } @@ -34,7 +34,7 @@ export const ProcessArgument = ( } else if (a.toLowerCase() == "false") { return false; } else { - throw new ArgumentTypeError(a, type); + throw new ParameterTypeError(a, type); } default: throw new TypeError("Invalied type specified");