message edits discord->revolt
This commit is contained in:
parent
2f1d02091c
commit
98d1eb318e
4 changed files with 37 additions and 3 deletions
|
@ -9,6 +9,9 @@ const client = new Discord.Client({
|
||||||
'GUILD_MEMBERS',
|
'GUILD_MEMBERS',
|
||||||
'GUILD_WEBHOOKS',
|
'GUILD_WEBHOOKS',
|
||||||
],
|
],
|
||||||
|
partials: [
|
||||||
|
'MESSAGE', // Allows us to receive message updates for uncached messages
|
||||||
|
],
|
||||||
allowedMentions: { parse: [ ] }, // how the hell does this work
|
allowedMentions: { parse: [ ] }, // how the hell does this work
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,8 +6,35 @@ import axios from 'axios';
|
||||||
import { ulid } from "ulid";
|
import { ulid } from "ulid";
|
||||||
import GenericEmbed from "../types/GenericEmbed";
|
import GenericEmbed from "../types/GenericEmbed";
|
||||||
import FormData from 'form-data';
|
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 => {
|
client.on('messageCreate', async message => {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { clipText, discordFetchMessage } from "../util";
|
||||||
|
|
||||||
client.on('message/update', async message => {
|
client.on('message/update', async message => {
|
||||||
if (message.content && typeof message.content != 'string') return;
|
if (message.content && typeof message.content != 'string') return;
|
||||||
|
if (message.author_id == client.user?._id) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.debug(`[E] Revolt: ${message.content}`);
|
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`);
|
if (!targetMsg) return logger.debug(`Revolt: Could not fetch message from Discord`);
|
||||||
|
|
||||||
const client = new WebhookClient({ id: bridgeCfg.discordWebhook.id, token: bridgeCfg.discordWebhook.token });
|
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();
|
client.destroy();
|
||||||
} catch(e) { console.error(e) }
|
} catch(e) { console.error(e) }
|
||||||
});
|
});
|
||||||
|
@ -78,6 +79,7 @@ client.on('message', async message => {
|
||||||
.filter(e => e.type == "Text")
|
.filter(e => e.type == "Text")
|
||||||
.map(e => new GenericEmbed(e as SendableEmbed).toDiscord())
|
.map(e => new GenericEmbed(e as SendableEmbed).toDiscord())
|
||||||
: undefined,
|
: undefined,
|
||||||
|
allowedMentions: { parse: [ ] },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (repliedMessages.length) {
|
if (repliedMessages.length) {
|
||||||
|
|
|
@ -17,7 +17,9 @@ async function revoltFetchUser(id?: string): Promise<User|undefined> {
|
||||||
return user;
|
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);
|
let message = revoltClient.messages.get(id);
|
||||||
if (message) return message;
|
if (message) return message;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue