Multiple channels bridge works !
This commit is contained in:
parent
3174c01b7d
commit
f83bb10340
5 changed files with 118 additions and 12 deletions
|
|
@ -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
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
10
config.json
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"verbose": true,
|
||||
"discordChannels": [
|
||||
"1161739009614151751",
|
||||
"1207060538341720104"
|
||||
],
|
||||
"ircChannels": [
|
||||
"#f2ville.dev"
|
||||
]
|
||||
}
|
||||
4
index.js
4
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()
|
||||
Loading…
Reference in a new issue