diff --git a/src/Bot.ts b/src/Bot.ts index 46d5ab0..66a4716 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -1,8 +1,7 @@ //IMPORTS import { Client, ClientOptions, Message } from 'discord.js'; -import { Command, CommandsManager } from './Commands.js'; +import { Command, CommandsManager, CommandMessageStructure } from './Commands.js'; import { HelpMessage, HelpMessageParams } from './Help.js'; -import { CommandMessage } from './CommandMessage.js'; import * as http from 'http'; //TYPE DEFINITIONS @@ -31,7 +30,7 @@ class Bot { } on: { message: (m: Message) => any, - command: (m: CommandMessage) => any + command: (m: Message, cmdInfo?: CommandMessageStructure) => any } constructor(options: ConstructorOptions) { @@ -54,7 +53,7 @@ class Bot { } this.on = { message: (m: Message) => {}, - command: (m: CommandMessage) => { console.log(m.command); } + command: (m: Message, cmdInfo?: CommandMessageStructure) => { console.log(cmdInfo); } } } async start(port?: number, token?: string) : Promise { @@ -76,12 +75,18 @@ class Bot { console.log('BOT IS READY!\n'); this.client.on('message', (m) => { this.on.message(m); - const cmdMsg: CommandMessage = new CommandMessage(m, this.config.prefix); - if(cmdMsg.command) { - this.on.command(cmdMsg); - const calledCommand = this.commands.get(cmdMsg.command.name); + const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix); + if(cmdMsg) { + this.on.command(m, cmdMsg); + const calledCommand: Command | null = this.commands.get(cmdMsg.name); if(calledCommand) { - calledCommand.start(cmdMsg); + try { + calledCommand.start(m, cmdMsg.arguments); + } + catch (e) { + console.error(`ERROR! ${e.toString()}`); + return; + } } } }); diff --git a/src/CommandMessage.ts b/src/CommandMessage.ts deleted file mode 100644 index 3405363..0000000 --- a/src/CommandMessage.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Message } from 'discord.js'; - -interface CommandMessageStructure { - name: string, - arguments: string[] -} - -class CommandMessage extends Message { - command: CommandMessageStructure | null - - constructor(message: Message, prefix: string) { - super(message.client, message, message.channel); - if(!message.author.bot && message.content.startsWith(prefix)) { - let msgContent = message.content.replace(prefix, ''); - const cmdName = msgContent.split(' ')[0]; - msgContent = msgContent.replace(cmdName, ''); - let cmdArguments: string[] = msgContent.split(','); - cmdArguments = cmdArguments.map((a) => { - return a.replace(' ', ''); - }); - this.command = { - name: cmdName, - arguments: cmdArguments - } - } - else { - this.command = null; - } - } -} - -export { CommandMessage } \ No newline at end of file diff --git a/src/Commands.ts b/src/Commands.ts index 0f97702..26386f8 100644 --- a/src/Commands.ts +++ b/src/Commands.ts @@ -10,7 +10,11 @@ interface CommandBuilder { usage?: string, permissions?: PermissionResolvable, visible?: boolean - function: (message?: Message) => void | string | MessageEmbed; + function: (message?: Message, cmdArguments?: string[]) => void | string | MessageEmbed; +} +interface CommandMessageStructure { + name: string, + arguments: string[] } //ERROR CLASSES @@ -35,28 +39,28 @@ class Command { usage: string; permissions: Permissions; visible: boolean; - private function: (message?: Message) => void | string | MessageEmbed; + private function: (message?: Message, cmdArguments?: string[]) => void | string | MessageEmbed; constructor(options: CommandBuilder) { this.name = options.name.split(' ').join('_'); - this.aliases = Command.ProcessPhrase(options.aliases); - this.keywords = Command.ProcessPhrase(options.keywords); + this.aliases = Command.processPhrase(options.aliases); + this.keywords = Command.processPhrase(options.keywords); this.description = options.description || "No description"; this.usage = options.usage || ""; this.permissions = new Permissions(options.permissions || 0); this.visible = options.visible || true; this.function = options.function; } - async start(message?: Message) { + async start(message?: Message, cmdArguments?: string[]) { const memberPermissions : Readonly = message?.member?.permissions || new Permissions(0); - if(this.permissions.any(memberPermissions, true)) { - await this.function(message); + if(memberPermissions.any(this.permissions, true)) { + await this.function(message, cmdArguments); } else { throw new PermissionsError(this, message?.member); } } - static ProcessPhrase(phrase?: string | string[]) : string[] { + static processPhrase(phrase?: string | string[]) : string[] { if(Array.isArray(phrase)) { const buff = phrase.map((p) => { return p.split(' ').join('_'); @@ -79,8 +83,8 @@ class CommandsManager { constructor() { this.list = []; } - get(name: string) : Command | undefined { - let command : Command | undefined = undefined; + get(name: string) : Command | null { + let command : Command | null = null; this.list.map((c) => { if(c.name == name) { command = c; @@ -117,7 +121,25 @@ class CommandsManager { return false; } } + fetch(message: Message, prefix: string) : CommandMessageStructure | null { + if(!message.author.bot && message.content.startsWith(prefix)) { + let msgContent = message.content.replace(prefix, ''); + const cmdName = msgContent.split(' ')[0]; + msgContent = msgContent.replace(cmdName, ''); + let cmdArguments: string[] = msgContent.split(','); + cmdArguments = cmdArguments.map((a) => { + return a.replace(' ', ''); + }); + return { + name: cmdName, + arguments: cmdArguments + } + } + else { + return null; + } + } } //EXPORTS -export { Command, CommandsManager } \ No newline at end of file +export { Command, CommandsManager, CommandMessageStructure } \ No newline at end of file diff --git a/src/Help.ts b/src/Help.ts index a0f9217..865b733 100644 --- a/src/Help.ts +++ b/src/Help.ts @@ -16,7 +16,7 @@ class HelpMessage extends Command { usage: params.usage, permissions: 0, description: params.description, - function: (m?: Message) => { + function: () => { const helpMsg = new MessageEmbed() if(helpMsg != null) { helpMsg.setColor(params.color);