shitty discord->revolt attachments

This commit is contained in:
JandereDev 2022-04-18 21:07:09 +02:00
parent 249fc99c13
commit aaddec9948
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
2 changed files with 58 additions and 17 deletions

View file

@ -17,6 +17,7 @@
"axios": "^0.26.1",
"discord.js": "^13.6.0",
"dotenv": "^16.0.0",
"form-data": "^4.0.0",
"log75": "^2.2.0",
"monk": "^7.3.4",
"revolt-api": "^0.5.3-rc.8",

View file

@ -1,15 +1,16 @@
import { BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
import { client } from "./client";
import { client as revoltClient } from "../revolt/client";
import { AUTUMN_URL, client as revoltClient } from "../revolt/client";
import { ChannelPermission } from "@janderedev/revolt.js";
import axios from 'axios';
import { ulid } from "ulid";
import GenericEmbed from "../types/GenericEmbed";
import FormData from 'form-data';
const MAX_BRIDGED_FILE_SIZE = 1_048_576; // 1 MiB
client.on('messageCreate', async message => {
try {
if (!message.content) return;
logger.debug(`[M] Discord: ${message.content}`);
const [ bridgeCfg, bridgedReply ] = await Promise.all([
BRIDGE_CONFIG.findOne({ discord: message.channelId }),
@ -55,10 +56,44 @@ client.on('messageCreate', async message => {
{ upsert: true }
);
const sendBridgeMessage = async (reply?: string) => {
await axios.post(
`${revoltClient.apiURL}/channels/${channel._id}/messages`,
const autumnUrls: string[] = [];
// todo: upload all attachments at once instead of sequentially
for (const a of message.attachments) {
try {
if (a[1].size > MAX_BRIDGED_FILE_SIZE) {
logger.debug(`Skipping attachment ${a[0]} ${a[1].name}: Size ${a[1].size} > max (${MAX_BRIDGED_FILE_SIZE})`);
continue;
}
logger.debug(`Downloading attachment ${a[0]} ${a[1].name} (Size ${a[1].size})`);
const formData = new FormData();
const file = await axios.get(a[1].url, { responseType: 'arraybuffer' });
logger.debug(`Downloading attachment ${a[0]} finished, uploading to autumn`);
formData.append(
a[0],
file.data,
{
filename: a[1].name || a[0],
contentType: a[1].contentType || undefined
}
);
const res = await axios.post(
`${AUTUMN_URL}/attachments`, formData, { headers: formData.getHeaders() }
);
logger.debug(`Uploading attachment ${a[0]} finished`);
autumnUrls.push(res.data.id);
} catch(e) { console.error(e) }
}
const sendBridgeMessage = async (reply?: string) => {
const payload = {
content: message.content,
//attachments: [],
//embeds: [],
@ -71,7 +106,12 @@ client.on('messageCreate', async message => {
embeds: message.embeds.length
? message.embeds.map(e => new GenericEmbed(e).toRevolt())
: undefined,
},
attachments: autumnUrls.length ? autumnUrls : undefined,
};
await axios.post(
`${revoltClient.apiURL}/channels/${channel._id}/messages`,
payload,
{
headers: {
'x-bot-token': process.env['REVOLT_TOKEN']!