Multiple channels bridge works !

This commit is contained in:
F2Ville 2024-02-13 21:30:12 +01:00
parent 3174c01b7d
commit f83bb10340
5 changed files with 118 additions and 12 deletions

View file

@ -3,10 +3,23 @@ const IRCClient = require("../../IRCBot")
const { EventEmitter } = require("node:events")
class Bridge extends EventEmitter {
constructor(verbose) {
constructor(config) {
super()
this.discordClient = new DiscordClient(this, verbose)
this.ircClient = new IRCClient(this, verbose)
const verbose = (config.verbose ?? false)
const discordChannels = config.discordChannels
const ircChannels = config.ircChannels
if (!discordChannels || !ircChannels) {
this.log("Missing channels");
throw new Error("Missing discordChannels or ircChannels")
}
this.config = config
this.discordClient = new DiscordClient(this, verbose, discordChannels)
this.ircClient = new IRCClient(this, verbose, ircChannels)
this.on("message", (msg, channel) => {
this.log("NEW MESSAGE !")
this.messageListener(msg, channel)
})
}
start() {
@ -20,6 +33,50 @@ class Bridge extends EventEmitter {
}
})
}
broadcast(msg, excludechan) {
this.log("Broadcasting", msg)
for (const dchan of this.config.discordChannels) {
// this.log("DCHAN BROADCAST", dchan, msg, excludechan)
if (!(excludechan.type == "discord" && dchan == excludechan.id)) {
this.discordClient.channels.cache.get(dchan)?.send(msg)
}
}
this.log("IRC Channels", this.config)
for (const ichan of this.config.ircChannels) {
this.log("ICHAN BROADCAST", ichan, msg, excludechan)
if (!(excludechan.type == "irc" && msg.target == excludechan.name)) {
this.log("verif chan", this.ircClient.channels.includes(ichan))
if (this.ircClient.channels.includes(ichan)) {
this.log(this.ircClient.channel(ichan))
this.ircClient.channel(ichan).say(msg.content)
}
}
}
}
log(...args) {
return console.log("[BRIDGE]", ...args)
}
messageListener(msg, channel) {
this.log("Message listener", msg, channel)
if (msg.target && msg.nick && msg.message && msg.target !== process.env.NICKNAME) {
this.log("Message from IRC")
this.broadcast({
content: `**<${msg.nick}>** ${msg.message}`
}, channel)
}
else if (msg.author) {
this.log("Message from Discord")
this.broadcast({
content: `**<${msg.author.username}>** ${msg.content}`
}, channel)
}
}
}
module.exports = Bridge

View file

@ -1,7 +1,7 @@
const { Client } = require("discord.js");
class DiscordClient extends Client {
constructor(bridge, verbose = false) {
constructor(bridge, verbose = false, channels) {
super({
intents: [
"Guilds",
@ -14,7 +14,12 @@ class DiscordClient extends Client {
})
this.bridge = bridge
this.verbose = verbose
this.on("message", (msg) => this.bridge.emit("message", msg))
this.on("messageCreate", (msg) => {
// this.log(msg)
if (!channels.includes(msg.channel.id) || msg.author.id == this.user.id) return
// this.log("Message is ok")
this.bridge.emit("message", msg, {type: "discord", id: msg.channel.id})
})
}
log(...args) {

View file

@ -1,13 +1,19 @@
const { Client } = require("irc-framework");
class IRCClient extends Client {
constructor(bridge, verbose = false) {
constructor(bridge, verbose = false, channels) {
super()
this.bridge = bridge
this.channels = channels
this.verbose = verbose
this.on("message", (msg) => this.bridge.emit("message", msg))
this.on("message", (msg) => {
// this.log(msg)
if (!channels.includes(msg.target) && !channels.includes(msg.channel) && msg.nick != this.user.nick) return
// this.log("Message is okay")
this.bridge.emit("message", msg, {type: "irc", name: msg.target})
})
}
nick
log(...args) {
if (this.verbose) console.log("[IRC]", ...args)
}
@ -20,9 +26,36 @@ class IRCClient extends Client {
this.on("connected", () => {
clearTimeout(timeOutMsg)
this.log("Client connected")
for (const channel of this.channels) {
this.join(channel)
}
resolve()
})
this.on("join", (channel) => {
this.log(`Joined ${channel.channel}`)
})
this.on("socket close", () => {
clearTimeout(timeOutMsg)
this.log("Socket closed")
})
this.on("socket error", (error) => {
clearTimeout(timeOutMsg)
this.log("Socket error: ", error)
})
this.on("irc error", (error) => {
clearTimeout(timeOutMsg)
this.log("IRC error: ", error)
})
this.on("error", (error) => {
clearTimeout(timeOutMsg)
this.log("Error: ", error)
})
this.on("close", () => {
clearTimeout(timeOutMsg)
this.log(`Can't connect to ${process.env.IRC_HOST}:${process.env.IRC_PORT}`)
@ -34,6 +67,7 @@ class IRCClient extends Client {
port: process.env.IRC_PORT,
nick: process.env.NICKNAME,
username: process.env.NICKNAME,
tls: true
})
} catch (error) {
reject(error)

10
config.json Normal file
View file

@ -0,0 +1,10 @@
{
"verbose": true,
"discordChannels": [
"1161739009614151751",
"1207060538341720104"
],
"ircChannels": [
"#f2ville.dev"
]
}

View file

@ -1,7 +1,7 @@
require("dotenv").config()
const Bridge = require("./Bridge")
const bridge = new Bridge(true)
const config = require("./config.json")
const bridge = new Bridge(config)
bridge.start()