From 4151979e108d5803c9ad3aaec8f27a17178744f0 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 27 Sep 2023 17:06:02 +1300 Subject: [PATCH] /emoji can now upload Matrix-exclusive emojis --- matrix/matrix-command-handler.js | 40 +++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/matrix/matrix-command-handler.js b/matrix/matrix-command-handler.js index 908b20bb..e2d7f872 100644 --- a/matrix/matrix-command-handler.js +++ b/matrix/matrix-command-handler.js @@ -202,27 +202,45 @@ const commands = [{ .addLine(`Ⓜ️ This room isn't bridged to Discord. ${matrixOnlyConclusion}`, `Ⓜ️ This room isn't bridged to Discord. ${matrixOnlyConclusion}`, matrixOnlyReason === "NOT_BRIDGED") .addLine(`Ⓜ️ *Discord ran out of space for emojis. ${matrixOnlyConclusion}`, `Ⓜ️ Discord ran out of space for emojis. ${matrixOnlyConclusion}`, matrixOnlyReason === "CAPACITY") .addLine(`Ⓜ️ *If you were a Discord user, you wouldn't have permission to create emojis. ${matrixOnlyConclusion}`, `Ⓜ️ If you were a Discord user, you wouldn't have permission to create emojis. ${matrixOnlyConclusion}`, matrixOnlyReason === "CAPACITY") - .addLine("[Preview not available in plain text.]", `Preview: `) + .addLine("[Preview not available in plain text.]", `Preview: :${name}:`) .addLine("Hit ✅ to add it.") .get() }) addButton(event.room_id, sent, "✅", event.sender).then(async () => { - const publicUrl = mxUtils.getPublicUrlForMxc(mxc) - // @ts-ignore - const resizeInput = await fetch(publicUrl, {agent: false}).then(res => res.arrayBuffer()) - const resizeOutput = await sharp(resizeInput) - .resize(EMOJI_SIZE, EMOJI_SIZE, {fit: "inside", withoutEnlargement: true, background: {r: 0, g: 0, b: 0, alpha: 0}}) - .png() - .toBuffer({resolveWithObject: true}) if (matrixOnlyReason) { - // Edit some state keys + // Edit some state + const type = "im.ponies.room_emotes" + const key = "moe.cadence.ooye.pack.matrix" + let pack + try { + pack = await api.getStateEvent(event.room_id, type, key) + } catch (e) { + pack = { + pack: { + display_name: "Non-Discord Emojis", + usage: ["emoticon", "sticker"] + } + } + } + if (!("images" in pack)) pack.images = {} + pack.images[name] = { + url: mxc // Directly use the same file that the Matrix user uploaded. Don't need to worry about dimensions/filesize because clients already request their preferred resized version from the homeserver. + } api.sendEvent(event.room_id, "m.room.message", { ...ctx, - msgtype: "m.text", - body: "Sorry, adding Matrix-only emojis not supported yet!!" + ...new MatrixStringBuilder() + .addLine(`Created :${name}:`, `:${name}:`) + .get() }) } else { // Upload it to Discord and have the bridge sync it back to Matrix again + const publicUrl = mxUtils.getPublicUrlForMxc(mxc) + // @ts-ignore + const resizeInput = await fetch(publicUrl, {agent: false}).then(res => res.arrayBuffer()) + const resizeOutput = await sharp(resizeInput) + .resize(EMOJI_SIZE, EMOJI_SIZE, {fit: "inside", withoutEnlargement: true, background: {r: 0, g: 0, b: 0, alpha: 0}}) + .png() + .toBuffer({resolveWithObject: true}) console.log(`uploading emoji ${resizeOutput.data.length} bytes to :${name}:`) const emoji = await discord.snow.guildAssets.createEmoji(guildID, {name, image: "data:image/png;base64," + resizeOutput.data.toString("base64")}) api.sendEvent(event.room_id, "m.room.message", {