From a3446ff0881b5e66dbbce75da34397b828e5b654 Mon Sep 17 00:00:00 2001 From: stjet <49297268+stjet@users.noreply.github.com> Date: Wed, 11 Feb 2026 07:40:03 +0000 Subject: [PATCH] cursed webhooks for discord sending --- index.ts | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index 7797ac8..0f2ec40 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -import { readFileSync } from "fs"; +import { appendFileSync, readFileSync } from "fs"; import * as discord from "discord.js"; import * as matrix from "matrix-js-sdk"; @@ -31,21 +31,48 @@ matrix_client.startClient({ initialSyncLimit: 0 }); //discord discord_client.on("messageCreate", async (message) => { - if (!MAPPING[message.channelId] || message.author.id === discord_client.user.id) return; - await matrix_client.sendEvent(MAPPING[message.channelId], matrix.EventType.RoomMessage, { + if (!MAPPING[message.channelId] || message.author.id === discord_client.user.id || (message.webhookId && (await message.fetchWebhook()).name === "Matrix Bridge")) return; + const channel_id = message.channelId; + let attachments = ""; + for (const [_, a] of message.attachments) { + attachments += "\n" + a.url; + } + await matrix_client.sendEvent(MAPPING[channel_id], matrix.EventType.RoomMessage, { msgtype: matrix.MsgType.Text, format: "org.matrix.custom.html", body: `${message.author.tag}: ${message.content}`, - formatted_body: `${message.author.tag}: ${message.content}`, + formatted_body: `${message.author.tag}: ${message.content}${attachments}`, }); }); //matrix +//authenticated media must be bypassed, see https://blog.kimiblock.top/2024/11/23/bypassing-auth-media/index.html matrix_client.on(matrix.RoomEvent.Timeline, async (event, room) => { if (event.getType() !== "m.room.message") return; const { sender, content, room_id } = event.event; if (!MAPPING[room_id] || sender === process.env.MATRIX_USER) return; - await (await discord_client.channels.fetch(MAPPING[room_id]) as discord.TextChannel).send(`**${sender}:** ${content.body}`); + const channel_id = MAPPING[room_id]; + let webhook_id = process.env[`webhookid_${channel_id}`]; + let webhook_token = process.env[`webhooktoken_${channel_id}`]; + if (!webhook_id || !webhook_token) { + console.log("Creating new webhook"); + const webhook = await (await discord_client.channels.fetch(MAPPING[room_id]) as discord.TextChannel).createWebhook({ + name: "Matrix Bridge", + }); + webhook_id = webhook.id; + process.env[`webhookid_${channel_id}`] = webhook_id; + webhook_token = webhook.token; + process.env[`webhooktoken_${channel_id}`] = webhook_token; + appendFileSync(".env", `\nwebhookid_${channel_id}=${webhook_id}\nwebhooktoken_${channel_id}=${webhook_token}`); + } + const webhook_client = new discord.WebhookClient({ id: webhook_id, token: webhook_token }); + await webhook_client.send({ + content: content.body, + username: sender, + avatarURL: event.sender.getAvatarUrl(matrix_client.getHomeserverUrl(), 128, 128, "crop", true, false).replace("media/v3", "client/v1/media"), + }); + // + //await (await discord_client.channels.fetch(channel_id) as discord.TextChannel).send(`**${sender}:** ${content.body}`); });