refractor + add try catch if sending messages fail
This commit is contained in:
parent
64014e73dc
commit
3e989717bf
5 changed files with 158 additions and 113 deletions
|
|
@ -1,82 +1,55 @@
|
||||||
const DiscordClient = require("../../DiscordBot")
|
const DiscordClient = require("../../DiscordBot");
|
||||||
const IRCClient = require("../../IRCBot")
|
const IRCClient = require("../../IRCBot");
|
||||||
const { EventEmitter } = require("node:events")
|
|
||||||
|
|
||||||
class Bridge extends EventEmitter {
|
class Bridge {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
super()
|
const verbose = config.verbose ?? false;
|
||||||
const verbose = (config.verbose ?? false)
|
|
||||||
const discordChannels = config.discordChannels
|
const discordChannels = config.discordChannels;
|
||||||
const ircChannels = config.ircChannels.map(chan => chan.toLowerCase())
|
const ircChannels = config.ircChannels.map((chan) => chan.toLowerCase());
|
||||||
|
|
||||||
if (!discordChannels || !ircChannels) {
|
if (!discordChannels || !ircChannels) {
|
||||||
this.log("Missing channels");
|
this.log("Missing channels");
|
||||||
throw new Error("Missing discordChannels or ircChannels")
|
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.clients = [
|
||||||
this.log("NEW MESSAGE !", channel)
|
new DiscordClient(this, verbose, discordChannels),
|
||||||
this.messageListener(msg, channel)
|
new IRCClient(this, verbose, ircChannels),
|
||||||
})
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
await this.discordClient.start()
|
for (const client of this.clients) {
|
||||||
await this.ircClient.start()
|
await client.start();
|
||||||
resolve()
|
}
|
||||||
|
|
||||||
|
resolve();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error)
|
reject(error);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast(msg, excludechan) {
|
broadcast(msg) {
|
||||||
this.log("Broadcasting", msg)
|
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)
|
for (const client of this.clients) {
|
||||||
|
client.broadcast(this.fromatMessageForForward(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log("IRC Channels", this.config)
|
fromatMessageForForward(msg) {
|
||||||
for (const ichan of this.ircClient.channels) {
|
const res_msg = { ...msg };
|
||||||
this.log("ICHAN BROADCAST", ichan, msg, excludechan)
|
res_msg.text = "<" + msg.author + ">: " + msg.text;
|
||||||
if (!(excludechan.type == "irc" && ichan == excludechan.name)) {
|
return res_msg;
|
||||||
|
|
||||||
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) {
|
log(...args) {
|
||||||
return console.log("[BRIDGE]", ...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;
|
||||||
|
|
|
||||||
22
Bridge/class/Message.js
Normal file
22
Bridge/class/Message.js
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
const { Client } = require("discord.js");
|
const { Client } = require("discord.js");
|
||||||
|
const { Message } = require("../../Bridge/class/Message")
|
||||||
|
|
||||||
|
|
||||||
class DiscordClient extends Client {
|
class DiscordClient extends Client {
|
||||||
constructor(bridge, verbose = false, channels) {
|
constructor(bridge, verbose = false, channels) {
|
||||||
|
|
@ -10,33 +12,57 @@ class DiscordClient extends Client {
|
||||||
"GuildMessageTyping",
|
"GuildMessageTyping",
|
||||||
"GuildMembers",
|
"GuildMembers",
|
||||||
"MessageContent",
|
"MessageContent",
|
||||||
]
|
],
|
||||||
})
|
});
|
||||||
this.bridge = bridge
|
this.bridge = bridge;
|
||||||
this.verbose = verbose
|
this.verbose = verbose;
|
||||||
|
this.trackedChannels = channels;
|
||||||
|
|
||||||
this.on("messageCreate", (msg) => {
|
this.on("messageCreate", (msg) => {
|
||||||
// this.log(msg)
|
//this.log("message recived", msg);
|
||||||
if (!channels.includes(msg.channel.id) || msg.author.id == this.user.id) return
|
|
||||||
// this.log("Message is ok")
|
// catch self messages
|
||||||
this.bridge.emit("message", msg, {type: "discord", id: msg.channel.id})
|
if (msg.author.id == this.user.id) return;
|
||||||
})
|
|
||||||
|
const internal_msg = new Message();
|
||||||
|
internal_msg.fromDiscordFormat(msg);
|
||||||
|
this.bridge.broadcast(internal_msg);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
log(...args) {
|
log(...args) {
|
||||||
if (this.verbose) console.log("[Discord]", ...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() {
|
start() {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
await this.login(process.env.DISCORD_TOKEN)
|
await this.login(process.env.DISCORD_TOKEN);
|
||||||
this.log("Client connected")
|
this.log("Client connected");
|
||||||
resolve()
|
resolve();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(error)
|
reject(error);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = DiscordClient
|
module.exports = DiscordClient;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
const { Client } = require("irc-framework");
|
const { Client } = require("irc-framework");
|
||||||
|
const { Message } = require("../../Bridge/class/Message");
|
||||||
|
|
||||||
class IRCClient extends Client {
|
class IRCClient extends Client {
|
||||||
constructor(bridge, verbose = false, channels) {
|
constructor(bridge, verbose = false, channels) {
|
||||||
|
|
@ -8,23 +9,46 @@ class IRCClient extends Client {
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
|
|
||||||
this.on("message", (msg) => {
|
this.on("message", (msg) => {
|
||||||
if (!msg.target) {
|
//this.log("message recived", msg);
|
||||||
return;
|
|
||||||
}
|
if (msg.from_server) return;
|
||||||
|
if (!msg.target) return;
|
||||||
|
|
||||||
msg.target = msg.target.toLowerCase();
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.log("Message is okay");
|
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) {
|
log(...args) {
|
||||||
if (this.verbose) console.log("[IRC]", ...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() {
|
start() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const timeOutMsg = setTimeout(() => {
|
const timeOutMsg = setTimeout(() => {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"verbose": true,
|
"verbose": true,
|
||||||
"discordChannels": [
|
"discordChannels": [
|
||||||
"1335942797726846989",
|
"1161739009614151751",
|
||||||
"727212463988146206",
|
"727212463988146206"
|
||||||
"1207060538341720104"
|
|
||||||
],
|
],
|
||||||
"ircChannels": ["#UrLab"]
|
"ircChannels": ["#urlab"]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue