From bd32fe6c6d60747da35fa4eabf0a9432787efea4 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 5 Jul 2023 08:41:15 +1200 Subject: [PATCH] reactions working --- d2m/event-dispatcher.js | 10 ++++++---- index.js | 4 ++++ m2d/actions/add-reaction.js | 4 +++- notes.md | 2 +- package-lock.json | 20 ++++++++++---------- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index 99c7792..1686b5f 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -14,10 +14,6 @@ module.exports = { * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message */ onMessageCreate(client, message) { - /** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */ - const channel = client.channels.get(message.channel_id) - const guild = client.guilds.get(channel.guild_id) - if (message.guild_id !== "112760669178241024" && message.guild_id !== "497159726455455754") return // TODO: activate on other servers (requires the space creation flow to be done first) if (message.webhook_id) { const row = db.prepare("SELECT webhook_id FROM webhook WHERE webhook_id = ?").pluck().get(message.webhook_id) if (row) { @@ -25,6 +21,11 @@ module.exports = { return } } + /** @type {import("discord-api-types/v10").APIGuildChannel} */ + const channel = client.channels.get(message.channel_id) + if (!channel.guild_id) return // Nothing we can do in direct messages. + const guild = client.guilds.get(channel.guild_id) + if (message.guild_id !== "112760669178241024" && message.guild_id !== "497159726455455754") return // TODO: activate on other servers (requires the space creation flow to be done first) sendMessage.sendMessage(message, guild) }, @@ -33,6 +34,7 @@ module.exports = { * @param {import("discord-api-types/v10").GatewayMessageReactionAddDispatchData} data */ onReactionAdd(client, data) { + if (data.user_id === client.user.id) return // m2d reactions are added by the discord bot user - do not reflect them back to matrix. if (data.emoji.id !== null) return // TODO: image emoji reactions console.log(data) addReaction.addReaction(data) diff --git a/index.js b/index.js index f4c59c6..233d518 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,10 @@ passthrough.as = as sync.require("./m2d/event-dispatcher") +discord.snow.requestHandler.on("requestError", data => { + console.error("request error", data) +}) + ;(async () => { await discord.cloud.connect() console.log("Discord gateway started") diff --git a/m2d/actions/add-reaction.js b/m2d/actions/add-reaction.js index 342550d..d259ddb 100644 --- a/m2d/actions/add-reaction.js +++ b/m2d/actions/add-reaction.js @@ -17,7 +17,9 @@ async function addReaction(event) { // 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 emoji = event.content["m.relates_to"].key // TODO: handle custom text or emoji reactions + emoji = encodeURIComponent(emoji) + emoji = emoji.replace(/%EF%B8%8F/g, "") return discord.snow.channel.createReaction(channelID, messageID, emoji) } diff --git a/notes.md b/notes.md index 3491682..ec2b9bb 100644 --- a/notes.md +++ b/notes.md @@ -104,7 +104,7 @@ Can use custom transaction ID (?) to send the original timestamps to Matrix. See ## Reaction removed -1. Remove reaction on matrix. +1. Remove reaction on matrix. Just redact the event. ## Member data changed diff --git a/package-lock.json b/package-lock.json index fecb682..4908aa6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -685,11 +685,11 @@ } }, "node_modules/cloudstorm": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/cloudstorm/-/cloudstorm-0.8.0.tgz", - "integrity": "sha512-CT5/RKvSz1I0wmsf0SmZ2Jg9fPvqY67t9e2Y8n92vU0uEK5WmfPUyPOLZoYPMJwmktmsVCj4N6Pvka9gBIsY4g==", + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/cloudstorm/-/cloudstorm-0.8.2.tgz", + "integrity": "sha512-G/P6/+LwXjiS6AmheRG+07DmmsrpHpt21JFMhe+rW8VagFOOKemC2Bcru+Qncl/5jdjZC2gzjKpjfdTjfUm+iw==", "dependencies": { - "snowtransfer": "0.8.0" + "snowtransfer": "^0.8.2" }, "engines": { "node": ">=12.0.0" @@ -1938,9 +1938,9 @@ } }, "node_modules/node-fetch": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", - "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", + "version": "2.6.12", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", + "integrity": "sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==", "dependencies": { "whatwg-url": "^5.0.0" }, @@ -2464,9 +2464,9 @@ } }, "node_modules/snowtransfer": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/snowtransfer/-/snowtransfer-0.8.0.tgz", - "integrity": "sha512-ang6qQsET4VX4u9mdZq6ynJvcm8HQfV6iZOHBh8Y3T0QkJLr6GAjzcv1et7BOXl1HDR/6NhD+j+ZGr8+imTclg==", + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/snowtransfer/-/snowtransfer-0.8.2.tgz", + "integrity": "sha512-fAmaJSpFZqGwAvbrhT3XOWwhbiuHOgxN8pGeKnDDW0f8zdkPmSQT9aekXhFr1WukB94NIALYGcyIXe902p8S4A==", "dependencies": { "discord-api-types": "^0.37.47", "form-data": "^4.0.0",