diff --git a/Bridge/class/Bridge.js b/Bridge/class/Bridge.js index d2f8df2..0c84950 100644 --- a/Bridge/class/Bridge.js +++ b/Bridge/class/Bridge.js @@ -1,82 +1,55 @@ -const DiscordClient = require("../../DiscordBot") -const IRCClient = require("../../IRCBot") -const { EventEmitter } = require("node:events") +const DiscordClient = require("../../DiscordBot"); +const IRCClient = require("../../IRCBot"); -class Bridge extends EventEmitter { - constructor(config) { - super() - const verbose = (config.verbose ?? false) - const discordChannels = config.discordChannels - const ircChannels = config.ircChannels.map(chan => chan.toLowerCase()) - 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) +class Bridge { + constructor(config) { + const verbose = config.verbose ?? false; - this.on("message", (msg, channel) => { - this.log("NEW MESSAGE !", channel) - this.messageListener(msg, channel) - }) + const discordChannels = config.discordChannels; + const ircChannels = config.ircChannels.map((chan) => chan.toLowerCase()); + + if (!discordChannels || !ircChannels) { + this.log("Missing channels"); + throw new Error("Missing discordChannels or ircChannels"); } - start() { - return new Promise(async (resolve, reject) => { - try { - await this.discordClient.start() - await this.ircClient.start() - resolve() - } catch (error) { - reject(error) - } - }) - } + this.clients = [ + new DiscordClient(this, verbose, discordChannels), + new IRCClient(this, verbose, ircChannels), + ]; + } - 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) - } + start() { + return new Promise(async (resolve, reject) => { + try { + for (const client of this.clients) { + await client.start(); } - this.log("IRC Channels", this.config) - for (const ichan of this.ircClient.channels) { - this.log("ICHAN BROADCAST", ichan, msg, excludechan) - if (!(excludechan.type == "irc" && ichan == excludechan.name)) { + resolve(); + } catch (error) { + reject(error); + } + }); + } - 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) - } - } - } - } + broadcast(msg) { + this.log("Broadcasting", msg); - log(...args) { - return console.log("[BRIDGE]", ...args) + for (const client of this.clients) { + client.broadcast(this.fromatMessageForForward(msg)); } + } - 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) - } - } + fromatMessageForForward(msg) { + const res_msg = { ...msg }; + res_msg.text = "<" + msg.author + ">: " + msg.text; + return res_msg; + } + + log(...args) { + return console.log("[BRIDGE]", ...args); + } } -module.exports = Bridge \ No newline at end of file +module.exports = Bridge; diff --git a/Bridge/class/Message.js b/Bridge/class/Message.js new file mode 100644 index 0000000..03d9fef --- /dev/null +++ b/Bridge/class/Message.js @@ -0,0 +1,22 @@ +export class Message{ + constructor(){ + this.text; + this.channel; + this.author; + this.source_type; // irc/discord... + } + + fromDiscordFormat(discord_message){ + this.text = discord_message.content + this.channel = discord_message.channelId + this.author = discord_message.author.username + this.source_type = "discord" + } + + fromIRCFormat(irc_message){ + this.text = irc_message.message, + this.channel = irc_message.target.toLowerCase(), + this.author = irc_message.nick, + this.source_type = "irc" + } +} diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index bbbd32e..c27b16f 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -1,42 +1,68 @@ const { Client } = require("discord.js"); +const { Message } = require("../../Bridge/class/Message") + class DiscordClient extends Client { - constructor(bridge, verbose = false, channels) { - super({ - intents: [ - "Guilds", - "GuildMessages", - "GuildMessageReactions", - "GuildMessageTyping", - "GuildMembers", - "MessageContent", - ] - }) - this.bridge = bridge - this.verbose = verbose - 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}) - }) - } + constructor(bridge, verbose = false, channels) { + super({ + intents: [ + "Guilds", + "GuildMessages", + "GuildMessageReactions", + "GuildMessageTyping", + "GuildMembers", + "MessageContent", + ], + }); + this.bridge = bridge; + this.verbose = verbose; + this.trackedChannels = channels; - log(...args) { - if (this.verbose) console.log("[Discord]", ...args) - } + this.on("messageCreate", (msg) => { + //this.log("message recived", msg); - start() { - return new Promise(async (resolve, reject) => { - try { - await this.login(process.env.DISCORD_TOKEN) - this.log("Client connected") - resolve() - } catch (error) { - reject(error) - } - }) + // catch self messages + if (msg.author.id == this.user.id) return; + + const internal_msg = new Message(); + internal_msg.fromDiscordFormat(msg); + this.bridge.broadcast(internal_msg); + }); + } + + log(...args) { + if (this.verbose) console.log("[Discord]", ...args); + } + + broadcast(msg) { + for (const chan of this.trackedChannels) { + if (chan != msg.channel) { // do not send in the channel the message + this.sendTextInChannel(msg.text, chan); + } } + } + + sendTextInChannel(text, chan) { + this.channels.cache + .get(chan) + ?.send(text) + .then(() => {}) + .catch((error) => + this.log("when trying to send message in ", chan, " got ", error) + ); + } + + start() { + return new Promise(async (resolve, reject) => { + try { + await this.login(process.env.DISCORD_TOKEN); + this.log("Client connected"); + resolve(); + } catch (error) { + reject(error); + } + }); + } } -module.exports = DiscordClient \ No newline at end of file +module.exports = DiscordClient; diff --git a/IRCBot/class/Client.js b/IRCBot/class/Client.js index 0ee560c..ca3a04a 100644 --- a/IRCBot/class/Client.js +++ b/IRCBot/class/Client.js @@ -1,4 +1,5 @@ const { Client } = require("irc-framework"); +const { Message } = require("../../Bridge/class/Message"); class IRCClient extends Client { constructor(bridge, verbose = false, channels) { @@ -8,23 +9,46 @@ class IRCClient extends Client { this.verbose = verbose; this.on("message", (msg) => { - if (!msg.target) { - return; - } + //this.log("message recived", msg); + + if (msg.from_server) return; + if (!msg.target) return; + msg.target = msg.target.toLowerCase(); - if (!channels.includes(msg.target) && msg.nick != this.user.nick) { + + // catch self messages + if (msg.nick == this.user.nick) { return; } this.log("Message is okay"); - this.bridge.emit("message", msg, { type: "irc", name: msg.target }); + + const internal_msg = new Message(); + internal_msg.fromIRCFormat(msg); + this.bridge.broadcast(internal_msg); }); } - nick; + log(...args) { if (this.verbose) console.log("[IRC]", ...args); } + broadcast(msg) { + for (const chan of this.channels) { + if (chan != msg.channel) { // do not send in the channel the message + try { + this.sendTextInChannel(msg.text, chan); + } catch (error) { + this.log("when tryin to send message in ", chan, " got ", error); + } + } + } + } + + sendTextInChannel(text, chan) { + this.channel(chan).say(text); + } + start() { return new Promise((resolve, reject) => { const timeOutMsg = setTimeout(() => { diff --git a/config.json b/config.json index 535ee35..9febf5e 100644 --- a/config.json +++ b/config.json @@ -1,9 +1,9 @@ { "verbose": true, "discordChannels": [ - "1335942797726846989", - "727212463988146206", - "1207060538341720104" + "1161739009614151751", + "727212463988146206" + ], - "ircChannels": ["#UrLab"] + "ircChannels": ["#urlab"] }