From 98d1eb318ebd239b358ffff0d4f9312322104c83 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Tue, 19 Apr 2022 11:22:16 +0200 Subject: [PATCH] message edits discord->revolt --- bridge/src/discord/client.ts | 3 +++ bridge/src/discord/events.ts | 29 ++++++++++++++++++++++++++++- bridge/src/revolt/events.ts | 4 +++- bridge/src/util.ts | 4 +++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/bridge/src/discord/client.ts b/bridge/src/discord/client.ts index e958e8d..bc16536 100644 --- a/bridge/src/discord/client.ts +++ b/bridge/src/discord/client.ts @@ -9,6 +9,9 @@ const client = new Discord.Client({ 'GUILD_MEMBERS', 'GUILD_WEBHOOKS', ], + partials: [ + 'MESSAGE', // Allows us to receive message updates for uncached messages + ], allowedMentions: { parse: [ ] }, // how the hell does this work }); diff --git a/bridge/src/discord/events.ts b/bridge/src/discord/events.ts index 8aa320f..0398de6 100644 --- a/bridge/src/discord/events.ts +++ b/bridge/src/discord/events.ts @@ -6,8 +6,35 @@ import axios from 'axios'; import { ulid } from "ulid"; import GenericEmbed from "../types/GenericEmbed"; import FormData from 'form-data'; +import { revoltFetchMessage } from "../util"; -const MAX_BRIDGED_FILE_SIZE = 1_048_576; // 1 MiB +const MAX_BRIDGED_FILE_SIZE = 8_000_000; // 8 MB + +client.on('messageUpdate', async (oldMsg, newMsg) => { + if (oldMsg.content && newMsg.content == oldMsg.content) return; // Let's not worry about embeds here for now + + try { + logger.debug(`[E] Discord: ${newMsg.content}`); + + const [ bridgeCfg, bridgedMsg ] = await Promise.all([ + BRIDGE_CONFIG.findOne({ discord: newMsg.channel.id }), + BRIDGED_MESSAGES.findOne({ "discord.messageId": newMsg.id }), + ]); + + if (!bridgedMsg) return logger.debug(`Discord: Message has not been bridged; ignoring edit`); + if (!bridgeCfg?.revolt) return logger.debug(`Discord: No Revolt channel associated`); + if (newMsg.webhookId && newMsg.webhookId == bridgeCfg.discordWebhook?.id) { + return logger.debug(`Discord: Message was sent by bridge; ignoring edit`); + } + + const targetMsg = await revoltFetchMessage(bridgedMsg.revolt.messageId, revoltClient.channels.get(bridgeCfg.revolt)); + if (!targetMsg) return logger.debug(`Discord: Could not fetch message from Revolt`); + + await targetMsg.edit({ content: newMsg.content || undefined }); + } catch(e) { + console.error(e); + } +}); client.on('messageCreate', async message => { try { diff --git a/bridge/src/revolt/events.ts b/bridge/src/revolt/events.ts index 72a71e2..8bc97ab 100644 --- a/bridge/src/revolt/events.ts +++ b/bridge/src/revolt/events.ts @@ -7,6 +7,7 @@ import { clipText, discordFetchMessage } from "../util"; client.on('message/update', async message => { if (message.content && typeof message.content != 'string') return; + if (message.author_id == client.user?._id) return; try { logger.debug(`[E] Revolt: ${message.content}`); @@ -24,7 +25,7 @@ client.on('message/update', async message => { 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.editMessage(targetMsg, { content: message.content }); + await client.editMessage(targetMsg, { content: message.content, allowedMentions: { parse: [ ] } }); client.destroy(); } catch(e) { console.error(e) } }); @@ -78,6 +79,7 @@ client.on('message', async message => { .filter(e => e.type == "Text") .map(e => new GenericEmbed(e as SendableEmbed).toDiscord()) : undefined, + allowedMentions: { parse: [ ] }, }; if (repliedMessages.length) { diff --git a/bridge/src/util.ts b/bridge/src/util.ts index 76f781e..9863364 100644 --- a/bridge/src/util.ts +++ b/bridge/src/util.ts @@ -17,7 +17,9 @@ async function revoltFetchUser(id?: string): Promise { return user; } -async function revoltFetchMessage(id: string, channel: Channel): Promise { +async function revoltFetchMessage(id?: string, channel?: Channel): Promise { + if (!id || !channel) return undefined; + let message = revoltClient.messages.get(id); if (message) return message;