bridge kinda works

This commit is contained in:
JandereDev 2022-04-05 10:31:02 +02:00
parent cc4f18a9b0
commit 7379b0106a
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
4 changed files with 81 additions and 7 deletions

View file

@ -8,8 +8,10 @@ import BridgedMessage from "../types/BridgedMessage";
client.on('messageCreate', async message => { client.on('messageCreate', async message => {
try { try {
logger.debug(`[M] Discord: ${message.content}`); logger.debug(`[M] Discord: ${message.content}`);
const bridgeCfg = await BRIDGE_CONFIG.findOne({ discord: message.channelId }); const bridgeCfg = await BRIDGE_CONFIG.findOne({ discord: message.channelId });
if (message.webhookId && bridgeCfg?.discordWebhook?.id == message.webhookId) {
return logger.debug(`Discord: Message has already been bridged; ignoring`);
}
if (!bridgeCfg?.revolt) return logger.debug(`Discord: No Revolt channel associated`); if (!bridgeCfg?.revolt) return logger.debug(`Discord: No Revolt channel associated`);
const channel = revoltClient.channels.get(bridgeCfg.revolt); const channel = revoltClient.channels.get(bridgeCfg.revolt);
@ -50,7 +52,7 @@ client.on('messageCreate', async message => {
revolt: { revolt: {
messageId: res.data._id, messageId: res.data._id,
}, },
} as BridgedMessage); });
}) })
.catch(e => { .catch(e => {
console.error(`Failed to send message`, e.response.data) console.error(`Failed to send message`, e.response.data)

View file

@ -3,13 +3,16 @@ import Log75, { LogLevel } from 'log75';
import { getDb } from './db'; import { getDb } from './db';
import { login as loginRevolt } from './revolt/client'; import { login as loginRevolt } from './revolt/client';
import { login as loginDiscord } from './discord/client'; import { login as loginDiscord } from './discord/client';
import { ICollection } from 'monk';
import BridgeConfig from './types/BridgeConfig';
import BridgedMessage from './types/BridgedMessage';
config(); config();
const logger: Log75 = new (Log75 as any).default(LogLevel.Debug); const logger: Log75 = new (Log75 as any).default(LogLevel.Debug);
const db = getDb(); const db = getDb();
const BRIDGED_MESSAGES = db.get('bridged_messages'); const BRIDGED_MESSAGES: ICollection<BridgedMessage> = db.get('bridged_messages');
const BRIDGE_CONFIG = db.get('bridge_config'); const BRIDGE_CONFIG: ICollection<BridgeConfig> = db.get('bridge_config');
for (const v of [ 'REVOLT_TOKEN', 'DISCORD_TOKEN', 'DB_STRING' ]) { for (const v of [ 'REVOLT_TOKEN', 'DISCORD_TOKEN', 'DB_STRING' ]) {
if (!process.env[v]) { if (!process.env[v]) {

View file

@ -1,6 +1,62 @@
import { logger } from ".."; import axios from "axios";
import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
import { client } from "./client"; import { client } from "./client";
import { client as discordClient } from "../discord/client";
client.on('message', message => { client.on('message', async message => {
try {
logger.debug(`[M] Revolt: ${message.content}`); logger.debug(`[M] Revolt: ${message.content}`);
const [ bridgedMsg, bridgeCfg ] = await Promise.all([
BRIDGED_MESSAGES.findOne({ "revolt.messageId": message._id }),
BRIDGE_CONFIG.findOne({ revolt: message.channel_id }),
]);
if (bridgedMsg) return logger.debug(`Revolt: Message has already been bridged; ignoring`);
if (!bridgeCfg?.discord) return logger.debug(`Revolt: No Discord channel associated`);
if (!bridgeCfg.discordWebhook) {
// Todo: Create a new webhook instead of exiting
return logger.debug(`Revolt: No Discord webhook stored`);
}
await BRIDGED_MESSAGES.insert({
origin: 'revolt',
discord: {},
revolt: {
messageId: message._id,
},
});
axios.post(
`https://discord.com/api/v9/webhooks/${bridgeCfg.discordWebhook.id}/${bridgeCfg.discordWebhook.token}?wait=true`,
{
content: message.content,
username: message.author?.username ?? 'Unknown user',
avatar_url: message.author?.generateAvatarURL({ max_side: 128 }),
},
{
headers: {
"Authorization": `Bot ${discordClient.token}`
}
}
)
.then(async res => {
await BRIDGED_MESSAGES.update({
revolt: {
messageId: message._id
}
}, {
$set: {
discord: {
messageId: res.data._id
}
}
});
})
.catch(e => {
console.error('Failed to execute webhook', e.response.data);
});
} catch(e) {
console.error(e);
}
}); });

View file

@ -0,0 +1,13 @@
export default class {
// Revolt channel ID
revolt?: string;
// Discord channel ID
discord?: string;
// Discord webhook
discordWebhook?: {
id: string;
token: string;
}
}