From 7846d6c19bff904fd4b915ccebf5392b31cffbad Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Wed, 29 Sep 2021 20:25:50 +0200 Subject: [PATCH] feat: add managed role mention prefix support (#289) --- .../command-handler/CoreMessageParser.ts | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/listeners/command-handler/CoreMessageParser.ts b/src/listeners/command-handler/CoreMessageParser.ts index 788c58125..8c7eb7d79 100644 --- a/src/listeners/command-handler/CoreMessageParser.ts +++ b/src/listeners/command-handler/CoreMessageParser.ts @@ -16,7 +16,7 @@ export class CoreListener extends Listener { if (!canRun) return; let prefix = null; - const mentionPrefix = this.getMentionPrefix(message.content); + const mentionPrefix = this.getMentionPrefix(message); const { client } = this.container; const { regexPrefix } = client.options; if (mentionPrefix) { @@ -48,26 +48,24 @@ export class CoreListener extends Listener { return channel.permissionsFor(me).has(this.requiredPermissions, false); } - private getMentionPrefix(content: string): string | null { - const { id } = this.container.client; + private getMentionPrefix(message: Message): string | null { + // If the content is shorter than 20 characters, or does not start with `<@` then skip early: + if (message.content.length < 20 || !message.content.startsWith('<@')) return null; - // If no client ID was specified, return null: - if (!id) return null; - - // If the content is shorter than `<@{n}>` or doesn't start with `<@`, skip early: - if (content.length < 20 || !content.startsWith('<@')) return null; + // Calculate the offset and the ID that is being provided + const [offset, id] = + message.content[2] === '&' + ? [3, message.guild?.roles.botRoleFor(message.guild.me!)?.id] + : [message.content[2] === '!' ? 3 : 2, this.container.client.id]; - // Retrieve whether the mention is a nickname mention (`<@!{n}>`) or not (`<@{n}>`). - const nickname = content[2] === '!'; - const idOffset = (nickname ? 3 : 2) as number; - const idLength = id.length; + if (!id) return null; // If the mention doesn't end with `>`, skip early: - if (content[idOffset + idLength] !== '>') return null; + if (message.content[offset + id.length] !== '>') return null; - // Check whether or not the ID is the same as the client ID: - const mentionId = content.substr(idOffset, idLength); - if (mentionId === id) return content.substr(0, idOffset + idLength + 1); + // Check whether or not the ID is the same as the managed role ID: + const mentionId = message.content.substr(offset, id.length); + if (mentionId === id) return message.content.substr(0, offset + id.length + 1); return null; }