waffle/discord-waffle-bridge/src/index.js
2022-10-30 21:32:35 +02:00

140 lines
No EOL
4.7 KiB
JavaScript

import { GatewayEventType } from "./WaffleClient.js";
import { DISCORD_GUILD, DISCORD_TOKEN, WAFFLE_TOKEN } from "./config.js";
import DiscordClient from "./DiscordClient.js";
import WaffleClient from "./WaffleClient.js";
class Bridge {
constructor() {
this.discordClient = new DiscordClient(
DISCORD_TOKEN,
{
intents: 0 | (1 << 0) | (1 << 9) | (1 << 15), // GUILDS | GUILD_MESSAGES | MESSAGE_CONTENT
}
);
this.waffleClient = new WaffleClient(WAFFLE_TOKEN, {
bridgesTo: "Discord Inc. (not affiliated)",
privacy: "https://discord.com/privacy",
terms: "https://discord.com/terms"
});
this.waffleChannelIdToDiscordChannelIdMap = new Map();
this.discordChannelIdToWaffleChannelIdMap = new Map();
this.waffleClient.subscribe(GatewayEventType.MessageCreate, async (message) => {
if (this.waffleClient.user && message.author_id === this.waffleClient.user.id) {
return;
}
const channel = this.waffleChannelIdToDiscordChannelId(message.channel_id);
if (channel === -1) {
return;
}
try {
await this.discordClient.sendMessageAs(channel, message.content, message.author_username);
} catch (o_0) {
console.error("Failed to send message to Discord", o_0);
}
});
this.discordClient.on("MESSAGE_CREATE", async (message) => {
if (message.guild_id !== DISCORD_GUILD || message.application_id) {
return;
}
const channel = this.discordChannelIdToWaffleChannelId(message.channel_id);
if (channel === -1) {
return;
}
let content = "";
message.attachments.forEach(e => {
content += `<attachment ${e.filename}: ${e.proxy_url}> `;
});
if (message.referenced_message) {
let trimmedContent = message.referenced_message.content.substring(0, 300);
if (trimmedContent !== message.referenced_message.content) {
trimmedContent += "...";
}
if (content !== "") {
// Add 2 newlines after the attachments area
content += "\n\n";
}
content += `> ${trimmedContent}\n@Discord/${message.referenced_message.author.username}: `;
}
content += message.content;
try {
await this.waffleClient.sendMessageAs(channel, content, message.author.username);
} catch (o_0) {
console.error("Failed to send message to Waffle", o_0);
}
});
}
discordChannelIdToWaffleChannelId(discordChannelId) {
if (this.discordChannelIdToWaffleChannelIdMap.get(discordChannelId)) {
return this.discordChannelIdToWaffleChannelIdMap.get(discordChannelId);
}
const guild = this.discordClient.guilds.find(e => e.id === DISCORD_GUILD);
if (!this.waffleClient.authenticated || !this.waffleClient.channels || !guild) {
return -1;
}
const discordChannel = guild.channels.find(e => e.id === discordChannelId);
if (!discordChannel) {
return -1;
}
const waffleChannel = this.waffleClient.channels.find(e => e.name === discordChannel.name);
if (!waffleChannel) {
return -1;
}
this.discordChannelIdToWaffleChannelIdMap.set(discordChannelId, waffleChannel.id);
return waffleChannel.id;
}
waffleChannelIdToDiscordChannelId(waffleChannelId) {
if (this.waffleChannelIdToDiscordChannelIdMap.get(waffleChannelId)) {
return this.waffleChannelIdToDiscordChannelIdMap.get(waffleChannelId);
}
const guild = this.discordClient.guilds.find(e => e.id === DISCORD_GUILD);
if (!this.waffleClient.authenticated || !this.waffleClient.channels || !guild) {
return -1;
}
const waffleChannel = this.waffleClient.channels.find(a => a.id === waffleChannelId);
if (!waffleChannel) {
return -1;
}
const discordChannel = guild.channels.find(e => e.name === waffleChannel.name);
if (!discordChannel) {
return -1;
}
this.waffleChannelIdToDiscordChannelIdMap.set(waffleChannelId, discordChannel.id);
return discordChannel.id;
}
connect() {
this.discordClient.connect();
this.waffleClient.connect();
}
}
async function main() {
const bridge = new Bridge();
bridge.connect();
}
main();