From 7379b0106a5b590a29ba14988ce597f311df452a Mon Sep 17 00:00:00 2001 From: JandereDev Date: Tue, 5 Apr 2022 10:31:02 +0200 Subject: [PATCH] bridge kinda works --- bridge/src/discord/events.ts | 6 ++-- bridge/src/index.ts | 7 ++-- bridge/src/revolt/events.ts | 62 ++++++++++++++++++++++++++++++-- bridge/src/types/BridgeConfig.ts | 13 +++++++ 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 bridge/src/types/BridgeConfig.ts diff --git a/bridge/src/discord/events.ts b/bridge/src/discord/events.ts index 3bec695..285a9b8 100644 --- a/bridge/src/discord/events.ts +++ b/bridge/src/discord/events.ts @@ -8,8 +8,10 @@ import BridgedMessage from "../types/BridgedMessage"; client.on('messageCreate', async message => { try { logger.debug(`[M] Discord: ${message.content}`); - 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`); const channel = revoltClient.channels.get(bridgeCfg.revolt); @@ -50,7 +52,7 @@ client.on('messageCreate', async message => { revolt: { messageId: res.data._id, }, - } as BridgedMessage); + }); }) .catch(e => { console.error(`Failed to send message`, e.response.data) diff --git a/bridge/src/index.ts b/bridge/src/index.ts index 372c095..30c99c3 100644 --- a/bridge/src/index.ts +++ b/bridge/src/index.ts @@ -3,13 +3,16 @@ import Log75, { LogLevel } from 'log75'; import { getDb } from './db'; import { login as loginRevolt } from './revolt/client'; import { login as loginDiscord } from './discord/client'; +import { ICollection } from 'monk'; +import BridgeConfig from './types/BridgeConfig'; +import BridgedMessage from './types/BridgedMessage'; config(); const logger: Log75 = new (Log75 as any).default(LogLevel.Debug); const db = getDb(); -const BRIDGED_MESSAGES = db.get('bridged_messages'); -const BRIDGE_CONFIG = db.get('bridge_config'); +const BRIDGED_MESSAGES: ICollection = db.get('bridged_messages'); +const BRIDGE_CONFIG: ICollection = db.get('bridge_config'); for (const v of [ 'REVOLT_TOKEN', 'DISCORD_TOKEN', 'DB_STRING' ]) { if (!process.env[v]) { diff --git a/bridge/src/revolt/events.ts b/bridge/src/revolt/events.ts index 6f3ebb8..a536083 100644 --- a/bridge/src/revolt/events.ts +++ b/bridge/src/revolt/events.ts @@ -1,6 +1,62 @@ -import { logger } from ".."; +import axios from "axios"; +import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from ".."; import { client } from "./client"; +import { client as discordClient } from "../discord/client"; -client.on('message', message => { - logger.debug(`[M] Revolt: ${message.content}`); +client.on('message', async message => { + try { + 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); + } }); diff --git a/bridge/src/types/BridgeConfig.ts b/bridge/src/types/BridgeConfig.ts new file mode 100644 index 0000000..03a1fde --- /dev/null +++ b/bridge/src/types/BridgeConfig.ts @@ -0,0 +1,13 @@ +export default class { + // Revolt channel ID + revolt?: string; + + // Discord channel ID + discord?: string; + + // Discord webhook + discordWebhook?: { + id: string; + token: string; + } +}