brain damage

This commit is contained in:
JandereDev 2022-04-05 21:45:44 +02:00
parent 7379b0106a
commit 9ce873975c
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
6 changed files with 103 additions and 61 deletions

4
.vscode/launch.json vendored
View file

@ -5,10 +5,10 @@
"version": "0.2.0",
"configurations": [
{
"command": "yarn dev",
"command": "cd bridge && yarn dev",
"name": "Debug",
"request": "launch",
"type": "node-terminal"
"type": "node-terminal",
}
]
}

View file

@ -18,6 +18,7 @@
"discord.js": "^13.6.0",
"dotenv": "^16.0.0",
"log75": "^2.2.0",
"monk": "^7.3.4"
"monk": "^7.3.4",
"ulid": "^2.3.0"
}
}

View file

@ -3,12 +3,21 @@ import { client } from "./client";
import { client as revoltClient } from "../revolt/client";
import { ChannelPermission } from "@janderedev/revolt.js";
import axios from 'axios';
import BridgedMessage from "../types/BridgedMessage";
import { ulid } from "ulid";
client.on('messageCreate', async message => {
try {
if (!message.content) return;
logger.debug(`[M] Discord: ${message.content}`);
const bridgeCfg = await BRIDGE_CONFIG.findOne({ discord: message.channelId });
const [ bridgeCfg, bridgedReply ] = await Promise.all([
BRIDGE_CONFIG.findOne({ discord: message.channelId }),
(message.reference?.messageId
? BRIDGED_MESSAGES.findOne({ "discord.messageId": message.reference.messageId })
: undefined
),
]);
if (message.webhookId && bridgeCfg?.discordWebhook?.id == message.webhookId) {
return logger.debug(`Discord: Message has already been bridged; ignoring`);
}
@ -25,13 +34,35 @@ client.on('messageCreate', async message => {
return logger.debug(`Discord: Lacking Masquerade permission; refusing to send`);
}
// Setting a known nonce allows us to ignore bridged
// messages while still letting other AutoMod messages pass.
const nonce = ulid();
await BRIDGED_MESSAGES.update(
{ "discord.messageId": message.id },
{
$setOnInsert: {
origin: 'discord',
discord: {
messageId: message.id,
},
},
$set: {
'revolt.nonce': nonce,
}
},
{ upsert: true }
);
const sendBridgeMessage = async (reply?: string) => {
await axios.post(
`${revoltClient.apiURL}/channels/${channel._id}/messages`,
{
content: message.content, // todo: parse and normalize this
content: message.content,
//attachments: [],
//embeds: [],
//replies: [],
nonce: nonce,
replies: reply ? [ { id: reply, mention: !!message.mentions.repliedUser } ] : undefined,
masquerade: {
name: message.author.username,
avatar: message.author.displayAvatarURL({ size: 128 }),
@ -44,19 +75,23 @@ client.on('messageCreate', async message => {
}
)
.then(async res => {
await BRIDGED_MESSAGES.insert({
origin: 'discord',
discord: {
messageId: message.id,
},
revolt: {
messageId: res.data._id,
},
});
await BRIDGED_MESSAGES.update(
{ "discord.messageId": message.id },
{
$set: { "revolt.messageId": res.data._id },
}
);
})
.catch(e => {
console.error(`Failed to send message`, e.response.data)
.catch(async e => {
console.error(`Failed to send message`, e.response.data);
if (reply) {
console.info('Reytring without reply');
await sendBridgeMessage(undefined);
}
});
}
await sendBridgeMessage(bridgedReply?.revolt?.messageId);
} catch(e) {
console.error(e);
}

View file

@ -28,4 +28,4 @@ for (const v of [ 'REVOLT_TOKEN', 'DISCORD_TOKEN', 'DB_STRING' ]) {
]);
})();
export { logger, BRIDGED_MESSAGES, BRIDGE_CONFIG }
export { logger, db, BRIDGED_MESSAGES, BRIDGE_CONFIG }

View file

@ -2,14 +2,17 @@ import axios from "axios";
import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
import { client } from "./client";
import { client as discordClient } from "../discord/client";
import { WebhookClient } from "discord.js";
client.on('message', async message => {
try {
if (!message.content || typeof message.content != 'string') return;
logger.debug(`[M] Revolt: ${message.content}`);
const [ bridgedMsg, bridgeCfg ] = await Promise.all([
BRIDGED_MESSAGES.findOne({ "revolt.messageId": message._id }),
const [ bridgeCfg, bridgedMsg, ...repliedMessages ] = await Promise.all([
BRIDGE_CONFIG.findOne({ revolt: message.channel_id }),
BRIDGED_MESSAGES.findOne({ "revolt.nonce": message.nonce }),
//...(message.reply_ids?.map(id => BRIDGED_MESSAGES.findOne({ "revolt.messageId": id })) ?? [])
]);
if (bridgedMsg) return logger.debug(`Revolt: Message has already been bridged; ignoring`);
@ -19,42 +22,44 @@ client.on('message', async message => {
return logger.debug(`Revolt: No Discord webhook stored`);
}
await BRIDGED_MESSAGES.insert({
origin: 'revolt',
discord: {},
await BRIDGED_MESSAGES.update(
{ 'revolt.messageId': message._id },
{
$set: {
revolt: {
messageId: message._id,
nonce: message.nonce,
},
},
$setOnInsert: {
discord: {},
origin: 'revolt',
}
},
{ upsert: true }
);
const client = new WebhookClient({
id: bridgeCfg.discordWebhook.id,
token: bridgeCfg.discordWebhook.token,
});
axios.post(
`https://discord.com/api/v9/webhooks/${bridgeCfg.discordWebhook.id}/${bridgeCfg.discordWebhook.token}?wait=true`,
{
content: message.content,
client.send({
content: `${message.content}`,
username: message.author?.username ?? 'Unknown user',
avatar_url: message.author?.generateAvatarURL({ max_side: 128 }),
},
{
headers: {
"Authorization": `Bot ${discordClient.token}`
}
}
)
avatarURL: message.author?.generateAvatarURL({ max_side: 128 }),
})
.then(async res => {
await BRIDGED_MESSAGES.update({
revolt: {
messageId: message._id
}
"revolt.messageId": message._id
}, {
$set: {
discord: {
messageId: res.data._id
}
"discord.messageId": res.id
}
});
})
.catch(e => {
console.error('Failed to execute webhook', e.response.data);
.catch(async e => {
console.error('Failed to execute webhook', e?.response?.data ?? e);
});
} catch(e) {
console.error(e);

View file

@ -7,5 +7,6 @@ export default class {
revolt: {
messageId?: string;
nonce?: string;
}
}