Silly fix for emoji sprite sheet

This commit is contained in:
Cadence Ember 2024-02-19 01:23:27 +13:00
parent 77b7772062
commit 6e41f85996

View file

@ -209,14 +209,19 @@ function convertEmoji(mxcUrl, nameForGuess, allowSpriteSheetIndicator, allowLink
// Get the known emoji from the database. // Get the known emoji from the database.
let row let row
if (mxcUrl) row = select("emoji", ["emoji_id", "name", "animated"], {mxc_url: mxcUrl}).get() if (mxcUrl) row = select("emoji", ["emoji_id", "name", "animated"], {mxc_url: mxcUrl}).get()
// Now we have to search all servers to see if we're able to send this emoji.
if (row) {
const found = [...discord.guilds.values()].find(g => g.emojis.find(e => e.id === row.id))
if (!found) row = null
}
// Or, if we don't have an emoji right now, we search for the name instead.
if (!row && nameForGuess) { if (!row && nameForGuess) {
// We don't know the emoji, but we could guess a suitable emoji based on the name
const nameForGuessLower = nameForGuess.toLowerCase() const nameForGuessLower = nameForGuess.toLowerCase()
for (const guild of discord.guilds.values()) { for (const guild of discord.guilds.values()) {
/** @type {{name: string, id: string, animated: number}[]} */ /** @type {{name: string, id: string, animated: number}[]} */
// @ts-ignore // @ts-ignore
const emojis = guild.emojis const emojis = guild.emojis
const found = emojis.find(e => e.name?.toLowerCase() === nameForGuessLower) const found = emojis.find(e => e.id === row?.id || e.name?.toLowerCase() === nameForGuessLower)
if (found) { if (found) {
row = { row = {
animated: found.animated, animated: found.animated,
@ -643,19 +648,6 @@ async function eventToMessage(event, guild, di) {
// input = input.replace(/ /g, " ") // input = input.replace(/ /g, " ")
// There is also a corresponding test to uncomment, named "event2message: whitespace is retained" // There is also a corresponding test to uncomment, named "event2message: whitespace is retained"
// SPRITE SHEET EMOJIS FEATURE: Emojis at the end of the message that we don't know about will be reuploaded as a sprite sheet.
// First we need to determine which emojis are at the end.
endOfMessageEmojis = []
let match
let last = input.length
while ((match = input.slice(0, last).match(/<img [^>]*>\s*$/))) {
if (!match[0].includes("data-mx-emoticon")) break
const mxcUrl = match[0].match(/\bsrc="(mxc:\/\/[^"]+)"/)
if (mxcUrl) endOfMessageEmojis.unshift(mxcUrl[1])
assert(typeof match.index === "number", "Your JavaScript implementation does not comply with TC39: https://tc39.es/ecma262/multipage/text-processing.html#sec-regexpbuiltinexec")
last = match.index
}
// Handling written @mentions: we need to look for candidate Discord members to join to the room // Handling written @mentions: we need to look for candidate Discord members to join to the room
// This shouldn't apply to code blocks, links, or inside attributes. So editing the HTML tree instead of regular expressions is a sensible choice here. // This shouldn't apply to code blocks, links, or inside attributes. So editing the HTML tree instead of regular expressions is a sensible choice here.
// We're using the domino parser because Turndown uses the same and can reuse this tree. // We're using the domino parser because Turndown uses the same and can reuse this tree.
@ -701,6 +693,19 @@ async function eventToMessage(event, guild, di) {
} }
await forEachNode(root) await forEachNode(root)
// SPRITE SHEET EMOJIS FEATURE: Emojis at the end of the message that we don't know about will be reuploaded as a sprite sheet.
// First we need to determine which emojis are at the end.
endOfMessageEmojis = []
let match
let last = input.length
while ((match = input.slice(0, last).match(/<img [^>]*>\s*$/))) {
if (!match[0].includes("data-mx-emoticon")) break
const mxcUrl = match[0].match(/\bsrc="(mxc:\/\/[^"]+)"/)
if (mxcUrl) endOfMessageEmojis.unshift(mxcUrl[1])
assert(typeof match.index === "number", "Your JavaScript implementation does not comply with TC39: https://tc39.es/ecma262/multipage/text-processing.html#sec-regexpbuiltinexec")
last = match.index
}
// @ts-ignore bad type from turndown // @ts-ignore bad type from turndown
content = turndownService.turndown(root) content = turndownService.turndown(root)