From 8da5ad0ca640834eee802fe5d5ebbf5cdb03b51a Mon Sep 17 00:00:00 2001 From: WatDuhHekBro <44940783+WatDuhHekBro@users.noreply.github.com> Date: Sun, 24 Jan 2021 19:12:43 -0600 Subject: [PATCH] Addressed issue with inline replies using prefix --- src/core/structures.ts | 13 ++++++++++++- src/events/message.ts | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/core/structures.ts b/src/core/structures.ts index b37e169..37b2194 100644 --- a/src/core/structures.ts +++ b/src/core/structures.ts @@ -110,7 +110,18 @@ if (process.argv[2] === "dev") { } export function getPrefix(guild: DiscordGuild | null): string { - return Storage.getGuild(guild?.id || "N/A").prefix ?? Config.prefix; + let prefix = Config.prefix; + + if (guild) { + const possibleGuildPrefix = Storage.getGuild(guild.id).prefix; + + // Here, lossy comparison works in our favor because you wouldn't want an empty string to trigger the prefix. + if (possibleGuildPrefix) { + prefix = possibleGuildPrefix; + } + } + + return prefix; } export interface EmoteRegistryDumpEntry { diff --git a/src/events/message.ts b/src/events/message.ts index 00e6458..107c8cf 100644 --- a/src/events/message.ts +++ b/src/events/message.ts @@ -22,16 +22,41 @@ export default new Event<"message">({ replyEventListeners.get(`${reference.channelID}-${reference.messageID}`)?.(message); } - const prefix = getPrefix(message.guild); + let prefix = getPrefix(message.guild); + const originalPrefix = prefix; + let exitEarly = !message.content.startsWith(prefix); + const clientUser = message.client.user; + let usesBotSpecificPrefix = false; - if (!message.content.startsWith(prefix) && !message.reference) { - if (message.client.user && message.mentions.has(message.client.user)) - message.channel.send(`${message.author.toString()}, my prefix on this guild is \`${prefix}\`.`); - return; + // If the client user exists, check if it starts with the bot-specific prefix. + if (clientUser) { + // If the prefix starts with the bot-specific prefix, go off that instead (these two options must mutually exclude each other). + // The pattern here has an optional space at the end to capture that and make it not mess with the header and args. + const matches = message.content.match(new RegExp(`^<@!?${clientUser.id}> ?`)); + + if (matches) { + prefix = matches[0]; + exitEarly = false; + usesBotSpecificPrefix = true; + } } + // If it doesn't start with the current normal prefix or the bot-specific unique prefix, exit the thread of execution early. + // Inline replies should still be captured here because if it doesn't exit early, two characters for a two-length prefix would still trigger commands. + if (exitEarly) return; + const [header, ...args] = message.content.substring(prefix.length).split(/ +/); + // If the message is just the prefix itself, move onto this block. + if (header === "" && args.length === 0) { + // I moved the bot-specific prefix to a separate conditional block to separate the logic. + // And because it listens for the mention as a prefix instead of a free-form mention, inline replies (probably) shouldn't ever trigger this unintentionally. + if (usesBotSpecificPrefix) { + message.channel.send(`${message.author.toString()}, my prefix on this guild is \`${originalPrefix}\`.`); + return; + } + } + if (!commands.has(header)) return; if ( @@ -66,7 +91,7 @@ export default new Event<"message">({ for (let param of args) { if (command.endpoint) { if (command.subcommands.size > 0 || command.user || command.number || command.any) - $.warn(`An endpoint cannot have subcommands! Check ${prefix}${header} again.`); + $.warn(`An endpoint cannot have subcommands! Check ${originalPrefix}${header} again.`); isEndpoint = true; break; }