From 5375e7bd84aef18136b8f160927d84623094ea75 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Mon, 28 Jun 2021 03:25:36 +0200 Subject: [PATCH] improved phrase checking --- README.md | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/Command.ts | 48 +++++++++++++++++++++++++++++++++++------------ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9a2c3d6..1e263e4 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # CommandBot -**1.1.0** +**1.1.1** Discord.js framework that helps you build your own Discord bot. diff --git a/package-lock.json b/package-lock.json index 4035a98..e49fdd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "commandbot", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "commandbot", - "version": "1.1.0", + "version": "1.1.1", "license": "MIT", "dependencies": { "discord.js": "^12.5.3" diff --git a/package.json b/package.json index c301d76..9b14c7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "commandbot", - "version": "1.1.0", + "version": "1.1.1", "description": "A framework that helps you create your own Discord bot easier.", "type": "module", "main": "dist/index.js", diff --git a/src/Command.ts b/src/Command.ts index 2e066e4..449c25c 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -18,6 +18,10 @@ interface CommandMessageStructure { arguments: string[], command: Command | null } +interface PhraseOccurrenceData { + command: Command, + type: 'NAME' | 'ALIAS' +} type GetMode = 'ALL' | 'PREFIX' | 'NO_PREFIX'; type PermissionCheckTypes = 'ALL' | 'ANY'; @@ -151,20 +155,22 @@ class CommandManager { if(!(command instanceof Command)) { throw new TypeError('Inavlid argument type'); } - this.list.map((c) => { - if(c.name == command.name) { - throw new Error(`Command with name "${c.name}" already exists!`); + const nameOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(command.name); + if(nameOccurrence) { + throw new Error(`The name "${command.name}" has already been registered as ${nameOccurrence.type} in the "${nameOccurrence.command.name}" command.`); + } + command.aliases.map(a => { + const aliasOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(a); + if(aliasOccurrence) { + console.warn(`WARN! The name "${a}" is already registered as ${aliasOccurrence.type} in the "${aliasOccurrence.command.name}" command. It will be removed from the "${command.name}".`); + const iToRemove = command.aliases.indexOf(a); + command.aliases.splice(iToRemove, 1); } - command.aliases.map((a) => { - if(c.aliases.indexOf(a) != -1) { - console.warn(`WARN! Alias with name "${a}" already exists in "${c.name}" command. It will be removed from the "${command.name}" command`); - const iToRemove = command.aliases.indexOf(a); - command.aliases.splice(iToRemove, 1); - } - }); - command.keywords.map((k) => { + }); + this.list.map(c => { + command.keywords.map(k => { if(c.keywords.indexOf(k) != -1) { - console.warn(`WARN! Keyword "${k}" already exists in "${c.name}" command. It will be removed from the "${command.name}" command`); + console.warn(`WARN! The name "${k}" is already a registered KEYWORD for the "${c.name}" command. It will be removed from the "${command.name}" command`); const iToRemove = command.keywords.indexOf(k); command.keywords.splice(iToRemove, 1); } @@ -223,6 +229,24 @@ class CommandManager { return null; } } + private findPhraseOccurrence(phrase?: string) : PhraseOccurrenceData | null { + let returnValue: PhraseOccurrenceData | null = null; + this.list.map(c => { + if(phrase == c.name) { + returnValue = { + command: c, + type: 'NAME' + } + } + else if(c.aliases.indexOf(phrase || '') != -1) { + returnValue = { + command: c, + type: 'ALIAS' + } + } + }); + return returnValue; + } } //EXPORTS