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

107
bot.js
View File

@@ -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

View File

@@ -1,13 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<BridgeConfig>
<Discord>
<Token>YOUR_DISCORD_TOKEN_HERE</Token>
<ChannelId>YOUR_DISCORD_CHANNEL_ID_HERE</ChannelId>
<Token>YOUR_DISCORD_TOKEN</Token>
</Discord>
<IRC>
<Server>IRC_SERVER_HERE</Server>
<Port>IRC_PORT_HERE</Port>
<Channel>IRC_CHANNEL_HERE</Channel>
<Nick>Bridge nick over on the IRC side</Nick>
</IRC>
<Bridges>
<Bridge>
<IRC>
<Server>irc.example1.net</Server>
<Port>6667</Port>
<Nick>MyIRCBot1</Nick>
<Channel>#channel1</Channel>
</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>