message edits discord->revolt

This commit is contained in:
JandereDev 2022-04-19 11:22:16 +02:00
parent 2f1d02091c
commit 98d1eb318e
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A
4 changed files with 37 additions and 3 deletions

View file

@ -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
});

View file

@ -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 {

View file

@ -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) {

View file

@ -17,7 +17,9 @@ async function revoltFetchUser(id?: string): Promise<User|undefined> {
return user;
}
async function revoltFetchMessage(id: string, channel: Channel): Promise<Message|undefined> {
async function revoltFetchMessage(id?: string, channel?: Channel): Promise<Message|undefined> {
if (!id || !channel) return undefined;
let message = revoltClient.messages.get(id);
if (message) return message;