From 9586cbf9780e6d2ce045ff532f0e49ed10cf4c1a Mon Sep 17 00:00:00 2001 From: ~purplebored Date: Mon, 5 May 2025 23:16:51 +0200 Subject: [PATCH] Updated the bridge to support multiple servers and chanles --- bot.js | 107 +++++++++++++++++++++++++++------------------ example.config.xml | 37 +++++++++++----- 2 files changed, 92 insertions(+), 52 deletions(-) diff --git a/bot.js b/bot.js index 2e41047..a85a7b4 100644 --- a/bot.js +++ b/bot.js @@ -18,66 +18,89 @@ try { process.exit(1); } -// Extract configuration const DISCORD_TOKEN = config.Discord.Token; -const DISCORD_CHANNEL_ID = config.Discord.ChannelId; -const IRC_SERVER = config.IRC.Server; -const IRC_PORT = parseInt(config.IRC.Port, 10); -const IRC_CHANNEL = config.IRC.Channel; -const IRC_NICK = config.IRC.Nick; +const bridges = Array.isArray(config.Bridges.Bridge) +? config.Bridges.Bridge +: [config.Bridges.Bridge]; -// Discord client setup +// Create Discord client const discordClient = new Client({ checkUpdate: false, intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); -// IRC client setup -const ircClient = new irc.Client(IRC_SERVER, IRC_NICK, { - channels: [IRC_CHANNEL], - port: IRC_PORT, - autoConnect: false -}); +// Map to hold IRC clients and their config +const ircClients = new Map(); -// Function to start IRC connection -function startIRC() { - console.log('Attempting to connect to IRC...'); - ircClient.connect(5, () => { - console.log(`Connected to IRC server ${IRC_SERVER}`); +// Helper function to create and connect an IRC client for each bridge +function createIRCClient(bridge) { + const ircConfig = bridge.IRC; + const discordChannelId = bridge.Discord.ChannelId; + + const server = ircConfig.Server; + const port = parseInt(ircConfig.Port, 10); + const nick = ircConfig.Nick; + const channel = ircConfig.Channel; + + const client = new irc.Client(server, nick, { + channels: [channel], + port: port, + autoConnect: false }); + + client.on('error', (message) => { + console.error(`IRC error on ${server} (${channel}):`, message); + }); + + client.on('message', (from, to, message) => { + // Avoid echoing own messages + if (from === nick) return; + + const ircMessage = `<${from}> ${message}`; + const discordChannel = discordClient.channels.cache.get(discordChannelId); + if (discordChannel) { + discordChannel.send(ircMessage).catch(console.error); + } else { + console.warn(`Discord channel ${discordChannelId} not found.`); + } + }); + + return { client, server, channel, nick, discordChannelId }; } -// Handle IRC connection errors -ircClient.addListener('error', (message) => { - console.error('IRC error:', message); -}); +// Initialize all IRC clients +for (const bridge of bridges) { + const { client, server, channel, nick } = createIRCClient(bridge); + ircClients.set(client, { server, channel, nick, discordChannelId: bridge.Discord.ChannelId }); +} -// Handle IRC messages and forward them to Discord -ircClient.addListener('message', (from, to, message) => { - console.log(`Received IRC message from ${from}: ${message}`); - if (from === IRC_NICK) return; +// Connect all IRC clients once Discord is ready +discordClient.once('ready', () => { + console.log(`Logged in as ${discordClient.user.tag}`); - const ircMessage = `<${from}> ${message}`; - const discordChannel = discordClient.channels.cache.get(DISCORD_CHANNEL_ID); - if (discordChannel) { - discordChannel.send(ircMessage); + for (const [ircClient, info] of ircClients.entries()) { + console.log(`Connecting to IRC server ${info.server} on channel ${info.channel}...`); + ircClient.connect(5, () => { + console.log(`Connected to IRC server ${info.server} channel ${info.channel}`); + }); } }); -// Handle Discord messages and forward them to IRC -discordClient.on('messageCreate', message => { - if (message.author.bot || message.channel.id !== DISCORD_CHANNEL_ID) return; - if (message.author.id === discordClient.user.id) return; +// Forward Discord messages to corresponding IRC channel +discordClient.on('messageCreate', (message) => { + if (message.author.bot) return; - const discordMessage = `<${message.author.username}> ${message.content}`; - console.log(`Forwarding Discord message to IRC: ${discordMessage}`); - ircClient.say(IRC_CHANNEL, discordMessage); -}); + // Find the IRC client(s) that correspond to this Discord channel + for (const [ircClient, info] of ircClients.entries()) { + if (message.channel.id === info.discordChannelId) { + // Avoid echoing messages sent by the bot itself + if (message.author.id === discordClient.user.id) return; -// Handle Discord ready event -discordClient.on('ready', () => { - console.log(`Logged in as ${discordClient.user.tag}`); - startIRC(); + const discordMessage = `<${message.author.username}> ${message.content}`; + console.log(`Forwarding Discord message to IRC [${info.server} ${info.channel}]: ${discordMessage}`); + ircClient.say(info.channel, discordMessage); + } + } }); // Login to Discord diff --git a/example.config.xml b/example.config.xml index 59e649c..00b732e 100644 --- a/example.config.xml +++ b/example.config.xml @@ -1,13 +1,30 @@ - - YOUR_DISCORD_TOKEN_HERE - YOUR_DISCORD_CHANNEL_ID_HERE + YOUR_DISCORD_TOKEN - - IRC_SERVER_HERE - IRC_PORT_HERE - IRC_CHANNEL_HERE - Bridge nick over on the IRC side - - \ No newline at end of file + + + + irc.example1.net + 6667 + MyIRCBot1 + #channel1 + + + DISCORD_CHANNEL_ID_1 + + + + + irc.example2.org + 6667 + MyIRCBot2 + #channel2 + + + DISCORD_CHANNEL_ID_2 + + + + +