forked from cadence/out-of-your-element
refactor custom emoji schema; make reactions work
This commit is contained in:
parent
c7ddf638db
commit
92dee012fc
9 changed files with 70 additions and 34 deletions
|
@ -15,27 +15,40 @@ async function addReaction(event) {
|
|||
const messageID = select("event_message", "message_id", "WHERE event_id = ? AND part = 0").pluck().get(event.content["m.relates_to"].event_id) // 0 = primary
|
||||
if (!messageID) return // Nothing can be done if the parent message was never bridged.
|
||||
|
||||
// no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it
|
||||
const emoji = event.content["m.relates_to"].key // TODO: handle custom text or emoji reactions
|
||||
let discordPreferredEncoding
|
||||
if (emoji.startsWith("mxc://")) {
|
||||
// Custom emoji
|
||||
const row = select("emoji", ["id", "name"], "WHERE mxc_url = ?").get(emoji)
|
||||
if (row) {
|
||||
// Great, we know exactly what this emoji is!
|
||||
discordPreferredEncoding = encodeURIComponent(`${row.name}:${row.id}`)
|
||||
} else {
|
||||
// We don't have this emoji and there's no realistic way to just-in-time upload a new emoji somewhere.
|
||||
// We can't try using a known emoji with the same name because we don't even know what the name is. We only have the mxc url.
|
||||
// Sucks!
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Default emoji
|
||||
// https://github.com/discord/discord-api-docs/issues/2723#issuecomment-807022205 ????????????
|
||||
const encoded = encodeURIComponent(emoji)
|
||||
const encodedTrimmed = encoded.replace(/%EF%B8%8F/g, "")
|
||||
|
||||
let emoji = event.content["m.relates_to"].key // TODO: handle custom text or emoji reactions
|
||||
let encoded = encodeURIComponent(emoji)
|
||||
let encodedTrimmed = encoded.replace(/%EF%B8%8F/g, "")
|
||||
const forceTrimmedList = [
|
||||
"%F0%9F%91%8D", // 👍
|
||||
"%E2%AD%90" // ⭐
|
||||
]
|
||||
|
||||
// https://github.com/discord/discord-api-docs/issues/2723#issuecomment-807022205 ????????????
|
||||
discordPreferredEncoding =
|
||||
( forceTrimmedList.includes(encodedTrimmed) ? encodedTrimmed
|
||||
: encodedTrimmed !== encoded && [...emoji].length === 2 ? encoded
|
||||
: encodedTrimmed)
|
||||
|
||||
const forceTrimmedList = [
|
||||
"%F0%9F%91%8D", // 👍
|
||||
"%E2%AD%90" // ⭐
|
||||
]
|
||||
console.log("add reaction from matrix:", emoji, encoded, encodedTrimmed, "chosen:", discordPreferredEncoding)
|
||||
}
|
||||
|
||||
let discordPreferredEncoding =
|
||||
( forceTrimmedList.includes(encodedTrimmed) ? encodedTrimmed
|
||||
: encodedTrimmed !== encoded && [...emoji].length === 2 ? encoded
|
||||
: encodedTrimmed)
|
||||
|
||||
console.log("add reaction from matrix:", emoji, encoded, encodedTrimmed, "chosen:", discordPreferredEncoding)
|
||||
|
||||
return discord.snow.channel.createReaction(channelID, messageID, discordPreferredEncoding)
|
||||
return discord.snow.channel.createReaction(channelID, messageID, discordPreferredEncoding) // acting as the discord bot itself
|
||||
}
|
||||
|
||||
module.exports.addReaction = addReaction
|
||||
|
|
|
@ -120,9 +120,25 @@ turndownService.addRule("inlineLink", {
|
|||
turndownService.addRule("emoji", {
|
||||
filter: function (node, options) {
|
||||
if (node.nodeName !== "IMG" || !node.hasAttribute("data-mx-emoticon") || !node.getAttribute("src")) return false
|
||||
const row = select("emoji", ["emoji_id", "animated"], "WHERE mxc_url = ?").get(node.getAttribute("src"))
|
||||
let row = select("emoji", ["id", "name", "animated"], "WHERE mxc_url = ?").get(node.getAttribute("src"))
|
||||
if (!row) {
|
||||
// We don't know what this is... but maybe we can guess based on the name?
|
||||
const guessedName = node.getAttribute("title")?.replace?.(/^:|:$/g, "")
|
||||
if (!guessedName) return false
|
||||
for (const guild of discord.guilds.values()) {
|
||||
/** @type {{name: string, id: string, animated: number}[]} */
|
||||
// @ts-ignore
|
||||
const emojis = guild.emojis
|
||||
const match = emojis.find(e => e.name === guessedName) || emojis.find(e => e.name?.toLowerCase() === guessedName.toLowerCase())
|
||||
if (match) {
|
||||
row = match
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!row) return false
|
||||
node.setAttribute("data-emoji-id", row.emoji_id)
|
||||
node.setAttribute("data-emoji-id", row.id)
|
||||
node.setAttribute("data-emoji-name", row.name)
|
||||
node.setAttribute("data-emoji-animated-char", row.animated ? "a" : "")
|
||||
return true
|
||||
},
|
||||
|
@ -133,8 +149,7 @@ turndownService.addRule("emoji", {
|
|||
/** @type {string} */
|
||||
const animatedChar = node.getAttribute("data-emoji-animated-char")
|
||||
/** @type {string} */
|
||||
const title = node.getAttribute("title") || "__"
|
||||
const name = title.replace(/^:|:$/g, "")
|
||||
const name = node.getAttribute("data-emoji-name")
|
||||
return `<${animatedChar}:${name}:${id}>`
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue