From 5c899b1129c7cd4ab94bc93ea7abfc5ca52ff3d1 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Mon, 21 Jun 2021 19:19:57 +0200 Subject: [PATCH] keyword + alias support --- src/Bot.ts | 31 +++++++++++------------ src/Command.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 22 deletions(-) diff --git a/src/Bot.ts b/src/Bot.ts index ee1f4b3..83b5218 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -77,27 +77,24 @@ class Bot { this.client.on('message', async m => { this.on.message(m); const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix); - if(cmdMsg) { + if(cmdMsg?.command) { this.on.command(m, cmdMsg); - const calledCommand: Command | null = this.commands.get(cmdMsg.name); - if(calledCommand) { - try { - await calledCommand.start(m, cmdMsg.arguments); - } - catch (e) { - if(e instanceof PermissionsError) { - this.messages.system.send('PERMISSION', { user: m.member || undefined, command: calledCommand }, m.channel); - } - else { - this.messages.system.send('ERROR', { command: calledCommand, user: m.member || undefined, error: e }, m.channel); - } - return; - } + try { + await cmdMsg.command.start(m, cmdMsg.arguments); } - else { - this.messages.system.send('NOT_FOUND', { phrase: m.content, user: m.member || undefined }, m.channel); + catch (e) { + if(e instanceof PermissionsError) { + this.messages.system.send('PERMISSION', { user: m.member || undefined, command: cmdMsg.command }, m.channel); + } + else { + this.messages.system.send('ERROR', { command: cmdMsg.command, user: m.member || undefined, error: e }, m.channel); + } + return; } } + else if(cmdMsg) { + this.messages.system.send('NOT_FOUND', { phrase: m.content, user: m.member || undefined }, m.channel); + } }); } else { diff --git a/src/Command.ts b/src/Command.ts index 52a4e4c..0a72cea 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -14,8 +14,10 @@ interface CommandBuilder { } interface CommandMessageStructure { name: string, - arguments: string[] + arguments: string[], + command: Command | null } +type GetMode = 'ALL' | 'PREFIX' | 'NO_PREFIX'; //ERROR CLASSES class PermissionsError { @@ -89,11 +91,43 @@ class CommandsManager { constructor() { this.list = []; } - get(name: string) : Command | null { + get(phrase: string, mode?: GetMode) : Command | null { + if(!mode) mode = 'ALL'; let command : Command | null = null; this.list.map((c) => { - if(c.name == name) { - command = c; + switch (mode) { + case 'PREFIX': + if(c.name == phrase) { + command = c; + } + c.aliases.map(a => { + if(a == phrase) { + command = c; + } + }); + break; + case 'NO_PREFIX': + c.keywords.map(k => { + if(k == phrase) { + command = c; + } + }); + break; + case 'ALL': + if(c.name == phrase) { + command = c; + } + c.aliases.map(a => { + if(a == phrase) { + command = c; + } + }); + c.keywords.map(k => { + if(k == phrase) { + command = c; + } + }); + break; } }); return command; @@ -131,6 +165,7 @@ class CommandsManager { if(!message.author.bot && message.content.startsWith(prefix)) { let msgContent = message.content.replace(prefix, ''); const cmdName = msgContent.split(' ')[0]; + const cmd: Command | null = this.get(cmdName, 'PREFIX'); msgContent = msgContent.replace(cmdName, ''); let cmdArguments: string[] = msgContent.split(','); cmdArguments = cmdArguments.map((a) => { @@ -141,7 +176,30 @@ class CommandsManager { } return { name: cmdName, - arguments: cmdArguments + arguments: cmdArguments, + command: cmd + } + } + else if(!message.author.bot) { + const cmdName = message.content.split(' ')[0]; + const cmd: Command | null = this.get(cmdName, 'NO_PREFIX'); + if(cmd) { + const cmdArgumentsStr = message.content.replace(cmdName, ''); + let cmdArguments: string[] = cmdArgumentsStr.split(','); + cmdArguments = cmdArguments.map((a) => { + return a.replace(' ', ''); + }); + if((cmdArguments[0] == '' || cmdArguments[0] == ' ') && cmdArguments.length == 1) { + cmdArguments = []; + } + return { + name: cmdName, + arguments: cmdArguments, + command: cmd + } + } + else { + return null; } } else {