From a2e3f06e8e65949cb52dd8049d364ae6b2a80d40 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 31 Aug 2024 17:02:46 +1200 Subject: [PATCH] Unbridge rooms when their channel is deleted --- d2m/actions/create-room.js | 20 ++++++++++++++------ d2m/discord-packets.js | 3 +++ d2m/event-dispatcher.js | 13 +++++++++++++ discord/register-interactions.js | 2 +- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/d2m/actions/create-room.js b/d2m/actions/create-room.js index a6b61cd3..51eef6cd 100644 --- a/d2m/actions/create-room.js +++ b/d2m/actions/create-room.js @@ -370,11 +370,16 @@ async function _unbridgeRoom(channelID) { /** @ts-ignore @type {DiscordTypes.APIGuildChannel} */ const channel = discord.channels.get(channelID) assert.ok(channel) - return unbridgeDeletedChannel(channel.id, channel.guild_id) + assert.ok(channel.guild_id) + return unbridgeDeletedChannel(channel, channel.guild_id) } -async function unbridgeDeletedChannel(channelID, guildID) { - const roomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get() +/** + * @param {DiscordTypes.APIGuildChannel} channel + * @param {string} guildID + */ +async function unbridgeDeletedChannel(channel, guildID) { + const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get() assert.ok(roomID) const spaceID = select("guild_space", "space_id", {guild_id: guildID}).pluck().get() assert.ok(spaceID) @@ -384,7 +389,11 @@ async function unbridgeDeletedChannel(channelID, guildID) { await api.sendState(spaceID, "m.space.child", roomID, {}) // remove declaration that the room is bridged - await api.sendState(roomID, "uk.half-shot.bridge", `moe.cadence.ooye://discord/${guildID}/${channelID}`, {}) + await api.sendState(roomID, "uk.half-shot.bridge", `moe.cadence.ooye://discord/${guildID}/${channel.id}`, {}) + if ("topic" in channel) { + // previously the Matrix topic would say the channel ID. we should remove that + await api.sendState(roomID, "m.room.topic", "", {topic: channel.topic || ""}) + } // send a notification in the room await api.sendEvent(roomID, "m.room.message", { @@ -396,8 +405,7 @@ async function unbridgeDeletedChannel(channelID, guildID) { await api.leaveRoom(roomID) // delete room from database - const {changes} = db.prepare("DELETE FROM channel_room WHERE room_id = ? AND channel_id = ?").run(roomID, channelID) - assert.equal(changes, 1) + db.prepare("DELETE FROM channel_room WHERE room_id = ? AND channel_id = ?").run(roomID, channel.id) } /** diff --git a/d2m/discord-packets.js b/d2m/discord-packets.js index ed47faea..f619f2b6 100644 --- a/d2m/discord-packets.js +++ b/d2m/discord-packets.js @@ -147,6 +147,9 @@ const utils = { } else if (message.t === "CHANNEL_PINS_UPDATE") { await eventDispatcher.onChannelPinsUpdate(client, message.d) + } else if (message.t === "CHANNEL_DELETE") { + await eventDispatcher.onChannelDelete(client, message.d) + } else if (message.t === "THREAD_CREATE") { // @ts-ignore await eventDispatcher.onThreadCreate(client, message.d) diff --git a/d2m/event-dispatcher.js b/d2m/event-dispatcher.js index 57cb72cd..235248c8 100644 --- a/d2m/event-dispatcher.js +++ b/d2m/event-dispatcher.js @@ -230,6 +230,19 @@ module.exports = { await updatePins.updatePins(data.channel_id, roomID, convertedTimestamp) }, + /** + * @param {import("./discord-client")} client + * @param {DiscordTypes.GatewayChannelDeleteDispatchData} channel + */ + async onChannelDelete(client, channel) { + const guildID = channel["guild_id"] + if (!guildID) return // channel must have been a DM channel or something + const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get() + if (!roomID) return // channel wasn't being bridged in the first place + // @ts-ignore + await createRoom.unbridgeDeletedChannel(channel, guildID) + }, + /** * @param {import("./discord-client")} client * @param {DiscordTypes.GatewayMessageCreateDispatchData} message diff --git a/discord/register-interactions.js b/discord/register-interactions.js index a6e43323..553c6db9 100644 --- a/discord/register-interactions.js +++ b/discord/register-interactions.js @@ -17,7 +17,7 @@ discord.snow.interaction.bulkOverwriteApplicationCommands(id, [{ name: "Permissions", contexts: [DiscordTypes.InteractionContextType.Guild], type: DiscordTypes.ApplicationCommandType.Message, - default_member_permissions: String(DiscordTypes.PermissionFlagsBits.KickMembers) + default_member_permissions: String(DiscordTypes.PermissionFlagsBits.KickMembers | DiscordTypes.PermissionFlagsBits.ManageRoles) }, { name: "invite", contexts: [DiscordTypes.InteractionContextType.Guild],