diff --git a/Bridge/class/Bridge.js b/Bridge/class/Bridge.js index f969915..38aaef4 100644 --- a/Bridge/class/Bridge.js +++ b/Bridge/class/Bridge.js @@ -1,25 +1,82 @@ const DiscordClient = require("../../DiscordBot") const IRCClient = require("../../IRCBot") -const {EventEmitter} = require("node:events") +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() { return new Promise(async (resolve, reject) => { try { await this.discordClient.start() - await this.ircClient.start() + await this.ircClient.start() resolve() } catch (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 \ No newline at end of file diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index ce196fa..bbbd32e 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -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) { diff --git a/IRCBot/class/Client.js b/IRCBot/class/Client.js index 30cf1b5..fbbd671 100644 --- a/IRCBot/class/Client.js +++ b/IRCBot/class/Client.js @@ -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) diff --git a/config.json b/config.json new file mode 100644 index 0000000..d7b27b2 --- /dev/null +++ b/config.json @@ -0,0 +1,10 @@ +{ + "verbose": true, + "discordChannels": [ + "1161739009614151751", + "1207060538341720104" + ], + "ircChannels": [ + "#f2ville.dev" + ] +} \ No newline at end of file diff --git a/index.js b/index.js index d099237..00b2fb7 100644 --- a/index.js +++ b/index.js @@ -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() \ No newline at end of file