Updated the bridge to support multiple servers and chanles

This commit is contained in:
2025-05-05 23:16:51 +02:00
parent d12a0c20d5
commit 9586cbf978
2 changed files with 92 additions and 52 deletions

103
bot.js
View File

@@ -18,66 +18,89 @@ try {
process.exit(1); process.exit(1);
} }
// Extract configuration
const DISCORD_TOKEN = config.Discord.Token; const DISCORD_TOKEN = config.Discord.Token;
const DISCORD_CHANNEL_ID = config.Discord.ChannelId; const bridges = Array.isArray(config.Bridges.Bridge)
const IRC_SERVER = config.IRC.Server; ? config.Bridges.Bridge
const IRC_PORT = parseInt(config.IRC.Port, 10); : [config.Bridges.Bridge];
const IRC_CHANNEL = config.IRC.Channel;
const IRC_NICK = config.IRC.Nick;
// Discord client setup // Create Discord client
const discordClient = new Client({ const discordClient = new Client({
checkUpdate: false, checkUpdate: false,
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
}); });
// IRC client setup // Map to hold IRC clients and their config
const ircClient = new irc.Client(IRC_SERVER, IRC_NICK, { const ircClients = new Map();
channels: [IRC_CHANNEL],
port: IRC_PORT, // 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 autoConnect: false
});
// 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}`);
}); });
}
// Handle IRC connection errors client.on('error', (message) => {
ircClient.addListener('error', (message) => { console.error(`IRC error on ${server} (${channel}):`, message);
console.error('IRC error:', message); });
});
// Handle IRC messages and forward them to Discord client.on('message', (from, to, message) => {
ircClient.addListener('message', (from, to, message) => { // Avoid echoing own messages
console.log(`Received IRC message from ${from}: ${message}`); if (from === nick) return;
if (from === IRC_NICK) return;
const ircMessage = `<${from}> ${message}`; const ircMessage = `<${from}> ${message}`;
const discordChannel = discordClient.channels.cache.get(DISCORD_CHANNEL_ID); const discordChannel = discordClient.channels.cache.get(discordChannelId);
if (discordChannel) { if (discordChannel) {
discordChannel.send(ircMessage); discordChannel.send(ircMessage).catch(console.error);
} else {
console.warn(`Discord channel ${discordChannelId} not found.`);
}
});
return { client, server, channel, nick, discordChannelId };
}
// 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 });
}
// Connect all IRC clients once Discord is ready
discordClient.once('ready', () => {
console.log(`Logged in as ${discordClient.user.tag}`);
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 // Forward Discord messages to corresponding IRC channel
discordClient.on('messageCreate', message => { discordClient.on('messageCreate', (message) => {
if (message.author.bot || message.channel.id !== DISCORD_CHANNEL_ID) return; if (message.author.bot) return;
// 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; if (message.author.id === discordClient.user.id) return;
const discordMessage = `<${message.author.username}> ${message.content}`; const discordMessage = `<${message.author.username}> ${message.content}`;
console.log(`Forwarding Discord message to IRC: ${discordMessage}`); console.log(`Forwarding Discord message to IRC [${info.server} ${info.channel}]: ${discordMessage}`);
ircClient.say(IRC_CHANNEL, discordMessage); ircClient.say(info.channel, discordMessage);
}); }
}
// Handle Discord ready event
discordClient.on('ready', () => {
console.log(`Logged in as ${discordClient.user.tag}`);
startIRC();
}); });
// Login to Discord // Login to Discord

View File

@@ -1,13 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<BridgeConfig> <BridgeConfig>
<Discord> <Discord>
<Token>YOUR_DISCORD_TOKEN_HERE</Token> <Token>YOUR_DISCORD_TOKEN</Token>
<ChannelId>YOUR_DISCORD_CHANNEL_ID_HERE</ChannelId>
</Discord> </Discord>
<Bridges>
<Bridge>
<IRC> <IRC>
<Server>IRC_SERVER_HERE</Server> <Server>irc.example1.net</Server>
<Port>IRC_PORT_HERE</Port> <Port>6667</Port>
<Channel>IRC_CHANNEL_HERE</Channel> <Nick>MyIRCBot1</Nick>
<Nick>Bridge nick over on the IRC side</Nick> <Channel>#channel1</Channel>
</IRC> </IRC>
<Discord>
<ChannelId>DISCORD_CHANNEL_ID_1</ChannelId>
</Discord>
</Bridge>
<Bridge>
<IRC>
<Server>irc.example2.org</Server>
<Port>6667</Port>
<Nick>MyIRCBot2</Nick>
<Channel>#channel2</Channel>
</IRC>
<Discord>
<ChannelId>DISCORD_CHANNEL_ID_2</ChannelId>
</Discord>
</Bridge>
<!-- Add more Bridge entries as needed -->
</Bridges>
</BridgeConfig> </BridgeConfig>