From 7790d98c66dfda1a7a3af56945be03621cb20218 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 10 Oct 2023 11:23:51 +1300 Subject: [PATCH] Fix bridging interaction responses and edits --- d2m/actions/send-message.js | 4 +- d2m/converters/edit-to-changes.js | 2 +- d2m/discord-packets.js | 1 - discord/utils.js | 10 ++ test/data.js | 187 ++++++++++++++++++++++++++++++ 5 files changed, 201 insertions(+), 3 deletions(-) diff --git a/d2m/actions/send-message.js b/d2m/actions/send-message.js index ab5b6c6..843ee20 100644 --- a/d2m/actions/send-message.js +++ b/d2m/actions/send-message.js @@ -12,6 +12,8 @@ const api = sync.require("../../matrix/api") const registerUser = sync.require("./register-user") /** @type {import("../actions/create-room")} */ const createRoom = sync.require("../actions/create-room") +/** @type {import("../../discord/utils")} */ +const dUtils = sync.require("../../discord/utils") /** * @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message @@ -21,7 +23,7 @@ async function sendMessage(message, guild) { const roomID = await createRoom.ensureRoom(message.channel_id) let senderMxid = null - if (!message.webhook_id) { + if (!dUtils.isWebhookMessage(message)) { if (message.member) { // available on a gateway message create event senderMxid = await registerUser.syncUser(message.author, message.member, message.guild_id, roomID) } else { // well, good enough... diff --git a/d2m/converters/edit-to-changes.js b/d2m/converters/edit-to-changes.js index 6d4bbdd..9d1fc0b 100644 --- a/d2m/converters/edit-to-changes.js +++ b/d2m/converters/edit-to-changes.js @@ -24,7 +24,7 @@ async function editToChanges(message, guild, api) { const roomID = select("channel_room", "room_id", {channel_id: message.channel_id}).pluck().get() assert(roomID) /** @type {string?} Null if we don't have a sender in the room, which will happen if it's a webhook's message. The bridge bot will do the edit instead. */ - const senderMxid = from("sim").join("sim_member", "mxid").where({user_id: message.author.id}).pluck("mxid").get() || null + const senderMxid = from("sim").join("sim_member", "mxid").where({user_id: message.author.id, room_id: roomID}).pluck("mxid").get() || null const oldEventRows = select("event_message", ["event_id", "event_type", "event_subtype", "part"], {message_id: message.id}).all() diff --git a/d2m/discord-packets.js b/d2m/discord-packets.js index 0782702..dfba8b0 100644 --- a/d2m/discord-packets.js +++ b/d2m/discord-packets.js @@ -141,7 +141,6 @@ const utils = { await eventDispatcher.onChannelOrThreadUpdate(client, message.d, true) } else if (message.t === "MESSAGE_CREATE") { - console.log(message.d) await eventDispatcher.onMessageCreate(client, message.d) } else if (message.t === "MESSAGE_UPDATE") { diff --git a/discord/utils.js b/discord/utils.js index 2a34008..8856aa7 100644 --- a/discord/utils.js +++ b/discord/utils.js @@ -47,4 +47,14 @@ function getPermissions(userRoles, guildRoles, userID, channelOverwrites) { return allowed } +/** + * Command interaction responses have a webhook_id for some reason, but still have real author info of a real bot user in the server. + * @param {DiscordTypes.APIMessage} message + */ +function isWebhookMessage(message) { + const isInteractionResponse = message.type === 20 + return message.webhook_id && !isInteractionResponse +} + module.exports.getPermissions = getPermissions +module.exports.isWebhookMessage = isWebhookMessage diff --git a/test/data.js b/test/data.js index d6abc9d..c36f252 100644 --- a/test/data.js +++ b/test/data.js @@ -2007,6 +2007,193 @@ module.exports = { }, attachments: [], guild_id: "1100319549670301727" + }, + bridge_echo_webhook: { + webhook_id: "1160692755144654970", + type: 0, + tts: false, + timestamp: "2023-10-09T21:15:58.866000+00:00", + pinned: false, + mentions: [], + mention_roles: [], + mention_everyone: false, + id: "1161049444674973706", + flags: 0, + embeds: [], + edited_timestamp: null, + content: "ready when you are", + components: [], + channel_id: "497161350934560778", + author: { + username: "cadence [they]", + id: "1160692755144654970", + discriminator: "0000", + bot: true, + avatar: "af0ead3b92cf6e448fdad80b4e7fc9e5" + }, + attachments: [], + application_id: "684280192553844747", + guild_id: "497159726455455754" + }, + crosspost_announcement: { + id: "1152745817678028840", + type: 0, + content: "All text based commands are now inactive on Chewey Bot\nTo continue using commands you'll need to use them as slash commands", + channel_id: "500454381414514688", + author: { + id: "748007224353226832", + username: "Chewey Bot Official Server #announcements", + avatar: "427b2893c574b90f1c6bb54da2c609cb", + discriminator: "0000", + public_flags: 0, + flags: 0, + bot: true + }, + attachments: [], + embeds: [], + mentions: [], + mention_roles: [], + pinned: false, + mention_everyone: false, + tts: false, + timestamp: "2023-09-16T23:20:19.916000+00:00", + edited_timestamp: null, + flags: 2, + components: [], + webhook_id: "748007224353226832", + message_reference: { + channel_id: "372274661439832065", + message_id: "1152745799596384263", + guild_id: "372271956562542592" + } + }, + }, + interaction_message: { + thinking_interaction_without_bot_user: { + webhook_id: "1109360903096369153", + type: 20, + tts: false, + timestamp: "2023-10-09T21:16:11.673000+00:00", + pinned: false, + nonce: "1161049469261709312", + mentions: [], + mention_roles: [], + mention_everyone: false, + interaction: { + user: { + username: "papiophidian", + public_flags: 4194880, + id: "320067006521147393", + global_name: "PapiOphidian", + discriminator: "0", + avatar_decoration_data: null, + avatar: "5fc4ad85c1ea876709e9a7d3374a78a1" + }, + type: 2, + name: "stats", + member: { + roles: [], + premium_since: null, + pending: false, + nick: "Brad", + mute: false, + joined_at: "2018-10-03T21:35:50.974000+00:00", + flags: 0, + deaf: false, + communication_disabled_until: null, + avatar: null + }, + id: "1161049497724534825" + }, + id: "1161049498391425196", + flags: 128, + embeds: [], + edited_timestamp: null, + content: "", + components: [], + channel_id: "497161350934560778", + author: { + username: "Amanda 🎵", + public_flags: 524288, + id: "1109360903096369153", + global_name: null, + discriminator: "2192", + bot: true, + avatar_decoration_data: null, + avatar: "e4a45abe5f8ee44f0b59b79a08bdb2ac" + }, + attachments: [], + application_id: "1109360903096369153", + guild_id: "497159726455455754" + }, + thinking_interaction: { + webhook_id: "1109360903096369153", + type: 20, + tts: false, + timestamp: "2023-10-09T21:18:45.002000+00:00", + pinned: false, + nonce: "1161050112089128960", + mentions: [], + mention_roles: [], + mention_everyone: false, + member: { + roles: [ "604073998749270046" ], + premium_since: null, + pending: false, + nick: null, + mute: false, + joined_at: "2023-10-09T21:18:30.600000+00:00", + flags: 1, + deaf: false, + communication_disabled_until: null, + avatar: null + }, + interaction: { + user: { + username: "papiophidian", + public_flags: 4194880, + id: "320067006521147393", + global_name: "PapiOphidian", + discriminator: "0", + avatar_decoration_data: null, + avatar: "5fc4ad85c1ea876709e9a7d3374a78a1" + }, + type: 2, + name: "stats", + member: { + roles: [], + premium_since: null, + pending: false, + nick: "Brad", + mute: false, + joined_at: "2018-10-03T21:35:50.974000+00:00", + flags: 0, + deaf: false, + communication_disabled_until: null, + avatar: null + }, + id: "1161050140640018472" + }, + id: "1161050141499863120", + flags: 128, + embeds: [], + edited_timestamp: null, + content: "", + components: [], + channel_id: "497161350934560778", + author: { + username: "Amanda 🎵", + public_flags: 524288, + id: "1109360903096369153", + global_name: null, + discriminator: "2192", + bot: true, + avatar_decoration_data: null, + avatar: "e4a45abe5f8ee44f0b59b79a08bdb2ac" + }, + attachments: [], + application_id: "1109360903096369153", + guild_id: "497159726455455754" } } }