From 249fc99c138138bb1bcb30e2cbd3700ffc8bc807 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Mon, 18 Apr 2022 19:05:31 +0200 Subject: [PATCH] attachments revolt->discord --- bridge/src/revolt/client.ts | 11 +++++++++-- bridge/src/revolt/events.ts | 21 ++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/bridge/src/revolt/client.ts b/bridge/src/revolt/client.ts index 4abaeaf..9e21553 100644 --- a/bridge/src/revolt/client.ts +++ b/bridge/src/revolt/client.ts @@ -1,6 +1,9 @@ import { Client } from '@janderedev/revolt.js'; +import axios from 'axios'; import { logger } from '..'; +let AUTUMN_URL = `http://autumnUrl`; + const client = new Client({ messageTimeoutFix: true, autoReconnect: true, @@ -9,12 +12,16 @@ const client = new Client({ const login = () => new Promise((resolve: (value: Client) => void) => { client.loginBot(process.env['REVOLT_TOKEN']!); - client.once('ready', () => { + client.once('ready', async () => { logger.info(`Revolt: ${client.user?.username} ready - ${client.servers.size} servers`); + + const apiConfig = await axios.get(client.apiURL); + AUTUMN_URL = apiConfig.data?.features?.autumn?.url; + resolve(client); }); }); import('./events'); -export { client, login } +export { client, login, AUTUMN_URL } diff --git a/bridge/src/revolt/events.ts b/bridge/src/revolt/events.ts index f4f14cc..2b53445 100644 --- a/bridge/src/revolt/events.ts +++ b/bridge/src/revolt/events.ts @@ -1,15 +1,15 @@ import axios from "axios"; import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from ".."; -import { client } from "./client"; +import { AUTUMN_URL, client } from "./client"; import { client as discordClient } from "../discord/client"; -import { MessageEmbed, WebhookClient } from "discord.js"; +import { MessageEmbed, MessagePayload, Webhook, WebhookClient, WebhookMessageOptions } from "discord.js"; import GenericEmbed from "../types/GenericEmbed"; import { SendableEmbed } from "revolt-api"; import { clipText, discordFetchMessage, discordFetchUser, revoltFetchMessage, revoltFetchUser } from "../util"; client.on('message', async message => { try { - if (!message.content || typeof message.content != 'string') return; + if (message.content && typeof message.content != 'string') return; logger.debug(`[M] Revolt: ${message.content}`); const [ bridgeCfg, bridgedMsg, ...repliedMessages ] = await Promise.all([ @@ -47,8 +47,8 @@ client.on('message', async message => { token: bridgeCfg.discordWebhook.token, }); - const payload = { - content: `${message.content}`, + const payload: MessagePayload|WebhookMessageOptions = { + content: message.content || undefined, username: message.author?.username ?? 'Unknown user', avatarURL: message.author?.generateAvatarURL({ max_side: 128 }), embeds: message.embeds?.length @@ -93,6 +93,17 @@ client.on('message', async message => { else payload.embeds = [ embed ]; } + if (message.attachments?.length) { + payload.files = []; + + for (const attachment of message.attachments) { + payload.files.push({ + attachment: `${AUTUMN_URL}/attachments/${attachment._id}/${attachment.filename}`, + name: attachment.filename, + }); + } + } + client.send(payload) .then(async res => { await BRIDGED_MESSAGES.update({