From 049fc2232668bdc5af184aa3eacc92111e6d16ac Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 21 Sep 2023 01:39:09 +1200 Subject: [PATCH] fix the order of fetching emojis for messages --- d2m/converters/message-to-event.js | 41 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/d2m/converters/message-to-event.js b/d2m/converters/message-to-event.js index d9b8940..15eaf34 100644 --- a/d2m/converters/message-to-event.js +++ b/d2m/converters/message-to-event.js @@ -156,11 +156,30 @@ async function messageToEvent(message, guild, options = {}, di) { } }) + // Handling emojis that we don't know about. The emoji has to be present in the DB for it to be picked up in the emoji markdown converter. + // So we scan the message ahead of time for all its emojis and ensure they are in the DB. + const emojiMatches = [...content.matchAll(/<(a?):([^:>]{2,20}):([0-9]+)>/g)] + const emojiDownloads = [] + for (const match of emojiMatches) { + const id = match[3] + const name = match[2] + const animated = +!!match[1] + const row = select("emoji", "id", "WHERE id = ?").pluck().get(id) + if (!row) { + // The custom emoji is not registered. We will register it and then add it. + emojiDownloads.push( + file.uploadDiscordFileToMxc(file.emoji(id, animated)).then(mxc => { + db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(id, name, animated, mxc) + }) + ) + } + } + await Promise.all(emojiDownloads) + let html = markdown.toHTML(content, { discordCallback: getDiscordParseCallbacks(message, true) }, null, null) - // TODO: add a string return type to my discord-markdown library let body = markdown.toHTML(content, { discordCallback: getDiscordParseCallbacks(message, false), discordOnly: true, @@ -209,26 +228,6 @@ async function messageToEvent(message, guild, options = {}, di) { } } - // Handling emojis that we don't know about. The emoji has to be present in the DB for it to be picked up in the emoji markdown converter. - // So we scan the message ahead of time for all its emojis and ensure they are in the DB. - const emojiMatches = [...content.matchAll(/<(a?):([^:>]{2,20}):([0-9]+)>/g)] - const emojiDownloads = [] - for (const match of emojiMatches) { - const id = match[3] - const name = match[2] - const animated = +!!match[1] - const row = select("emoji", "id", "WHERE id = ?").pluck().get(id) - if (!row) { - // The custom emoji is not registered. We will register it and then add it. - emojiDownloads.push( - file.uploadDiscordFileToMxc(file.emoji(id, animated)).then(mxc => { - db.prepare("INSERT OR IGNORE INTO emoji (id, name, animated, mxc_url) VALUES (?, ?, ?, ?)").run(id, name, animated, mxc) - }) - ) - } - } - await Promise.all(emojiDownloads) - // Star * prefix for fallback edits if (options.includeEditFallbackStar) { body = "* " + body