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

@ -1,25 +1,82 @@
const DiscordClient = require("../../DiscordBot") const DiscordClient = require("../../DiscordBot")
const IRCClient = require("../../IRCBot") const IRCClient = require("../../IRCBot")
const {EventEmitter} = require("node:events") const { EventEmitter } = require("node:events")
class Bridge extends EventEmitter { class Bridge extends EventEmitter {
constructor(verbose) { constructor(config) {
super() super()
this.discordClient = new DiscordClient(this, verbose) const verbose = (config.verbose ?? false)
this.ircClient = new IRCClient(this, verbose) 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() { start() {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
await this.discordClient.start() await this.discordClient.start()
await this.ircClient.start() await this.ircClient.start()
resolve() resolve()
} catch (error) { } catch (error) {
reject(error) reject(error)
} }
}) })
} }
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 module.exports = Bridge

View file

@ -1,7 +1,7 @@
const { Client } = require("discord.js"); const { Client } = require("discord.js");
class DiscordClient extends Client { class DiscordClient extends Client {
constructor(bridge, verbose = false) { constructor(bridge, verbose = false, channels) {
super({ super({
intents: [ intents: [
"Guilds", "Guilds",
@ -14,7 +14,12 @@ class DiscordClient extends Client {
}) })
this.bridge = bridge this.bridge = bridge
this.verbose = verbose 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) { log(...args) {

View file

@ -1,13 +1,19 @@
const { Client } = require("irc-framework"); const { Client } = require("irc-framework");
class IRCClient extends Client { class IRCClient extends Client {
constructor(bridge, verbose = false) { constructor(bridge, verbose = false, channels) {
super() super()
this.bridge = bridge this.bridge = bridge
this.channels = channels
this.verbose = verbose 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) { log(...args) {
if (this.verbose) console.log("[IRC]", ...args) if (this.verbose) console.log("[IRC]", ...args)
} }
@ -20,9 +26,36 @@ class IRCClient extends Client {
this.on("connected", () => { this.on("connected", () => {
clearTimeout(timeOutMsg) clearTimeout(timeOutMsg)
this.log("Client connected") this.log("Client connected")
for (const channel of this.channels) {
this.join(channel)
}
resolve() 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", () => { this.on("close", () => {
clearTimeout(timeOutMsg) clearTimeout(timeOutMsg)
this.log(`Can't connect to ${process.env.IRC_HOST}:${process.env.IRC_PORT}`) 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, port: process.env.IRC_PORT,
nick: process.env.NICKNAME, nick: process.env.NICKNAME,
username: process.env.NICKNAME, username: process.env.NICKNAME,
tls: true
}) })
} catch (error) { } catch (error) {
reject(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() require("dotenv").config()
const Bridge = require("./Bridge") const Bridge = require("./Bridge")
const config = require("./config.json")
const bridge = new Bridge(true) const bridge = new Bridge(config)
bridge.start() bridge.start()