From 77feb1f73509634a7c83ff922c894e8b06ab7952 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Tue, 19 Apr 2022 11:52:11 +0200 Subject: [PATCH] message deletions revolt->discord --- bridge/src/discord/events.ts | 4 ++++ bridge/src/revolt/events.ts | 29 +++++++++++++++++++++++++++++ bridge/src/types/BridgedMessage.ts | 6 ++++++ 3 files changed, 39 insertions(+) diff --git a/bridge/src/discord/events.ts b/bridge/src/discord/events.ts index 0398de6..293c3f9 100644 --- a/bridge/src/discord/events.ts +++ b/bridge/src/discord/events.ts @@ -78,6 +78,10 @@ client.on('messageCreate', async message => { }, $set: { 'revolt.nonce': nonce, + channels: { + discord: message.channelId, + revolt: bridgeCfg.revolt, + } } }, { upsert: true } diff --git a/bridge/src/revolt/events.ts b/bridge/src/revolt/events.ts index 8bc97ab..f92b112 100644 --- a/bridge/src/revolt/events.ts +++ b/bridge/src/revolt/events.ts @@ -5,6 +5,31 @@ import GenericEmbed from "../types/GenericEmbed"; import { SendableEmbed } from "revolt-api"; import { clipText, discordFetchMessage } from "../util"; +client.on('message/delete', async id => { + try { + logger.debug(`[D] Revolt: ${id}`); + + const bridgedMsg = await BRIDGED_MESSAGES.findOne({ "revolt.messageId": id }); + if (!bridgedMsg?.discord.messageId) return logger.debug(`Revolt: Message has not been bridged; ignoring delete`); + if (!bridgedMsg.channels?.discord) return logger.debug(`Revolt: Channel for deleted message is unknown`); + + const bridgeCfg = await BRIDGE_CONFIG.findOne({ revolt: bridgedMsg.channels.revolt }); + if (!bridgeCfg?.discordWebhook) return logger.debug(`Revolt: No Discord webhook stored`); + if (!bridgeCfg.discord || bridgeCfg.discord != bridgedMsg.channels.discord) { + return logger.debug(`Revolt: Discord channel is no longer linked; ignoring delete`); + } + + const targetMsg = await discordFetchMessage(bridgedMsg.discord.messageId, bridgeCfg.discord); + if (!targetMsg) return logger.debug(`Revolt: Could not fetch message from Discord`); + + const client = new WebhookClient({ id: bridgeCfg.discordWebhook.id, token: bridgeCfg.discordWebhook.token }); + await client.deleteMessage(bridgedMsg.discord.messageId); + client.destroy(); + } catch(e) { + console.error(e); + } +}); + client.on('message/update', async message => { if (message.content && typeof message.content != 'string') return; if (message.author_id == client.user?._id) return; @@ -56,6 +81,10 @@ client.on('message', async message => { messageId: message._id, nonce: message.nonce, }, + channels: { + revolt: message.channel_id, + discord: bridgeCfg.discord, + } }, $setOnInsert: { discord: {}, diff --git a/bridge/src/types/BridgedMessage.ts b/bridge/src/types/BridgedMessage.ts index f5e60ae..698d542 100644 --- a/bridge/src/types/BridgedMessage.ts +++ b/bridge/src/types/BridgedMessage.ts @@ -9,4 +9,10 @@ export default class { messageId?: string; nonce?: string; } + + // Required to sync message deletions + channels?: { + discord: string; + revolt: string; + } }