From 053b835e89ea7148172ee1e04f46164af712fddf Mon Sep 17 00:00:00 2001 From: Mijyuoon Date: Wed, 7 Apr 2021 17:01:44 +0300 Subject: [PATCH] Improve emote resolving for .emote and .react commands (#33) --- src/commands/utilities/emote.ts | 7 +- src/commands/utilities/react.ts | 19 +-- .../utilities/subcommands/emote-utils.ts | 127 ++++++++++++++---- 3 files changed, 113 insertions(+), 40 deletions(-) diff --git a/src/commands/utilities/emote.ts b/src/commands/utilities/emote.ts index 912774c..d62a3d0 100644 --- a/src/commands/utilities/emote.ts +++ b/src/commands/utilities/emote.ts @@ -1,5 +1,5 @@ import Command from "../../core/command"; -import {queryClosestEmoteByName} from "./subcommands/emote-utils"; +import {processEmoteQueryFormatted} from "./subcommands/emote-utils"; import {botHasPermission} from "../../core/lib"; import {Permissions} from "discord.js"; @@ -10,9 +10,8 @@ export default new Command({ description: "The emote(s) to send.", usage: "", async run({guild, channel, message, args}) { - let output = ""; - for (const query of args) output += queryClosestEmoteByName(query).toString(); - channel.send(output); + const output = processEmoteQueryFormatted(args); + if (output.length > 0) channel.send(output); } }) }); diff --git a/src/commands/utilities/react.ts b/src/commands/utilities/react.ts index 49648aa..13fabd3 100644 --- a/src/commands/utilities/react.ts +++ b/src/commands/utilities/react.ts @@ -1,7 +1,7 @@ import Command from "../../core/command"; import {CommonLibrary} from "../../core/lib"; import {Message, Channel, TextChannel} from "discord.js"; -import {queryClosestEmoteByName} from "./subcommands/emote-utils"; +import {processEmoteQueryArray} from "./subcommands/emote-utils"; export default new Command({ description: @@ -10,17 +10,11 @@ export default new Command({ async run($: CommonLibrary): Promise { let target: Message | undefined; let distance = 1; - - // allows reactions by using an in-line reply - if($.message.reference){ - const messageID = $.message.reference.messageID; - try{ - target = await $.channel.messages.fetch(messageID!) - }catch{ - return $.channel.send("Unknown error occurred!") - } + + if ($.message.reference) { + // If the command message is a reply to another message, use that as the react target. + target = await $.channel.messages.fetch($.message.reference.messageID!); } - // handles reacts by message id/distance else if ($.args.length >= 2) { const last = $.args[$.args.length - 1]; // Because this is optional, do not .pop() unless you're sure it's a message link indicator. @@ -106,9 +100,8 @@ export default new Command({ ).last(); } - for (const search of $.args) { + for (const emote of processEmoteQueryArray($.args)) { // Even though the bot will always grab *some* emote, the user can choose not to keep that emote there if it isn't what they want - const emote = queryClosestEmoteByName(search); const reaction = await target!.react(emote); // This part is called with a promise because you don't want to wait 5 seconds between each reaction. diff --git a/src/commands/utilities/subcommands/emote-utils.ts b/src/commands/utilities/subcommands/emote-utils.ts index 0e18652..16ca2eb 100644 --- a/src/commands/utilities/subcommands/emote-utils.ts +++ b/src/commands/utilities/subcommands/emote-utils.ts @@ -1,33 +1,114 @@ +import {GuildEmoji} from "discord.js"; import {client} from "../../../index"; -// Calculate and match the list of emotes against the queried emote, then sort the IDs based on calculated priority. -export function queryClosestEmoteByName(query: string) { - const priorityTable: {[id: string]: number} = {}; +// Levenshtein distance coefficients for all transformation types. +// TODO: Investigate what values result in the most optimal matching strategy. +const directMatchWeight = 0.0; +const uppercaseWeight = 0.2; +const lowercaseWeight = 0.5; +const substitutionWeight = 1.0; +const deletionWeight = 1.5; +const insertionWeight = 1.5; - for (const emote of client.emojis.cache.values()) priorityTable[emote.id] = compareEmoteNames(emote.name, query); +// Maximum Levenshtein distance for an emote to be considered a suitable match candidate. +const maxAcceptedDistance = 3.0; - const resultingIDs = Object.keys(priorityTable).sort((a, b) => priorityTable[b] - priorityTable[a]); - return client.emojis.cache.get(resultingIDs[0])!; -} +// Algorithm taken from https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows +// Modified for separate handling of uppercasing and lowercasing transformations. +function levenshtein(s: string, t: string): number { + const m = s.length; + const n = t.length; -// Compare an emote's name against a query to see how alike the two are. The higher the number, the closer they are. Takes into account length and capitalization. -function compareEmoteNames(emote: string, query: string) { - let likeness = -Math.abs(emote.length - query.length); - const isQueryLonger = query.length > emote.length; + let v0 = new Array(n + 1); + let v1 = new Array(n + 1); - // Loop through all indexes that the two strings share then compare each letter. - for (let i = 0; i < (isQueryLonger ? emote.length : query.length); i++) { - const c = emote[i]; - const q = query[i]; + let i, j; - // If they're the exact same character - if (c === q) likeness += 1.5; - // If the emote is uppercase but the query is lowercase - else if (c === q.toUpperCase()) likeness += 1; - // If the emote is lowercase but the query is uppercase - else if (c === q.toLowerCase()) likeness += 0.5; - // Otherwise, if they're different characters, don't add anything (this isn't a spellchecker) + for (i = 0; i <= n; i++) v0[i] = i; + + for (i = 0; i < m; i++) { + v1[0] = i + 1; + + for (j = 0; j < n; j++) { + let r; + + if (s[i] === t[j]) r = directMatchWeight; + else if (s[i] === t[j].toUpperCase()) r = uppercaseWeight; + else if (s[i] === t[j].toLowerCase()) r = lowercaseWeight; + else r = substitutionWeight; + + v1[j + 1] = Math.min( + v0[j + 1] + deletionWeight, + v1[j] + insertionWeight, + v0[j] + r); + } + + const tmp = v1; + v1 = v0, v0 = tmp; } - return likeness; + return v0[n]; } + +function searchSimilarEmotes(query: string): GuildEmoji[] { + const emoteCandidates: {emote: GuildEmoji, dist: number}[] = []; + + for (const emote of client.emojis.cache.values()) { + const dist = levenshtein(emote.name, query); + if (dist <= maxAcceptedDistance) { + emoteCandidates.push({ emote, dist }); + } + } + + emoteCandidates.sort((b, a) => b.dist - a.dist); + return emoteCandidates.map(em => em.emote); +} + +const unicodeEmojiRegex = /^(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])[\ufe00-\ufe0f]?$/; +const discordEmoteMentionRegex = /^$/; +const emoteNameWithSelectorRegex = /^(.+)~(\d+)$/; + +function processEmoteQuery(query: string[], isFormatted: boolean): string[] { + return query.map(emote => { + emote = emote.trim(); + + // If the query directly matches a Unicode emoji or a Discord custom emote mention, pass it as-is. + if (discordEmoteMentionRegex.test(emote) + || unicodeEmojiRegex.test(emote)) return emote; + + // If formatted mode is enabled, parse whitespace and newline elements. + if (isFormatted) { + if (emote == "-") return " "; + if (emote == "+") return "\n"; + } + + // Selector number used for disambiguating multiple emotes with same name. + let selector = 0; + + // If the query has emoteName~123 format, extract the actual name and the selector number. + const queryWithSelector = emote.match(emoteNameWithSelectorRegex); + if (queryWithSelector) { + emote = queryWithSelector[1]; + selector = +queryWithSelector[2]; + } + + // Try to match an emote name directly if the selector is for the closest match. + if (selector == 0) { + const directMatchEmote = client.emojis.cache.find(em => em.name === emote); + if (directMatchEmote) return directMatchEmote.toString(); + } + + // Find all similar emote candidates within certian threshold and select Nth top one according to the selector. + const similarEmotes = searchSimilarEmotes(emote); + if (similarEmotes.length > 0) { + selector = Math.min(selector, similarEmotes.length); + return similarEmotes[selector].toString(); + } + + // Return some "missing/invalid emote" indicator. + return "❓"; + }); +} + +export const processEmoteQueryArray = (query: string[]): string[] => processEmoteQuery(query, false); +export const processEmoteQueryFormatted = (query: string[]): string => processEmoteQuery(query, true).join(""); \ No newline at end of file