Unbridge rooms when their channel is deleted

This commit is contained in:
Cadence Ember 2024-08-31 17:02:46 +12:00
parent 7afe3e7591
commit a2e3f06e8e
4 changed files with 31 additions and 7 deletions

View file

@ -370,11 +370,16 @@ async function _unbridgeRoom(channelID) {
/** @ts-ignore @type {DiscordTypes.APIGuildChannel} */ /** @ts-ignore @type {DiscordTypes.APIGuildChannel} */
const channel = discord.channels.get(channelID) const channel = discord.channels.get(channelID)
assert.ok(channel) 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) assert.ok(roomID)
const spaceID = select("guild_space", "space_id", {guild_id: guildID}).pluck().get() const spaceID = select("guild_space", "space_id", {guild_id: guildID}).pluck().get()
assert.ok(spaceID) assert.ok(spaceID)
@ -384,7 +389,11 @@ async function unbridgeDeletedChannel(channelID, guildID) {
await api.sendState(spaceID, "m.space.child", roomID, {}) await api.sendState(spaceID, "m.space.child", roomID, {})
// remove declaration that the room is bridged // 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 // send a notification in the room
await api.sendEvent(roomID, "m.room.message", { await api.sendEvent(roomID, "m.room.message", {
@ -396,8 +405,7 @@ async function unbridgeDeletedChannel(channelID, guildID) {
await api.leaveRoom(roomID) await api.leaveRoom(roomID)
// delete room from database // delete room from database
const {changes} = db.prepare("DELETE FROM channel_room WHERE room_id = ? AND channel_id = ?").run(roomID, channelID) db.prepare("DELETE FROM channel_room WHERE room_id = ? AND channel_id = ?").run(roomID, channel.id)
assert.equal(changes, 1)
} }
/** /**

View file

@ -147,6 +147,9 @@ const utils = {
} else if (message.t === "CHANNEL_PINS_UPDATE") { } else if (message.t === "CHANNEL_PINS_UPDATE") {
await eventDispatcher.onChannelPinsUpdate(client, message.d) await eventDispatcher.onChannelPinsUpdate(client, message.d)
} else if (message.t === "CHANNEL_DELETE") {
await eventDispatcher.onChannelDelete(client, message.d)
} else if (message.t === "THREAD_CREATE") { } else if (message.t === "THREAD_CREATE") {
// @ts-ignore // @ts-ignore
await eventDispatcher.onThreadCreate(client, message.d) await eventDispatcher.onThreadCreate(client, message.d)

View file

@ -230,6 +230,19 @@ module.exports = {
await updatePins.updatePins(data.channel_id, roomID, convertedTimestamp) 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 {import("./discord-client")} client
* @param {DiscordTypes.GatewayMessageCreateDispatchData} message * @param {DiscordTypes.GatewayMessageCreateDispatchData} message

View file

@ -17,7 +17,7 @@ discord.snow.interaction.bulkOverwriteApplicationCommands(id, [{
name: "Permissions", name: "Permissions",
contexts: [DiscordTypes.InteractionContextType.Guild], contexts: [DiscordTypes.InteractionContextType.Guild],
type: DiscordTypes.ApplicationCommandType.Message, type: DiscordTypes.ApplicationCommandType.Message,
default_member_permissions: String(DiscordTypes.PermissionFlagsBits.KickMembers) default_member_permissions: String(DiscordTypes.PermissionFlagsBits.KickMembers | DiscordTypes.PermissionFlagsBits.ManageRoles)
}, { }, {
name: "invite", name: "invite",
contexts: [DiscordTypes.InteractionContextType.Guild], contexts: [DiscordTypes.InteractionContextType.Guild],