UrBridge/DiscordBot/class/Client.js
vincent 8b60c229a3 fix broadcasting messag
es from discord from untracked channels
2025-02-19 15:52:31 +01:00

70 lines
1.7 KiB
JavaScript

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.trackedChannels = channels;
this.on("messageCreate", (msg) => {
this.log("message recived", msg);
// catch self messages
if (msg.author.id == this.user.id) return;
// only propagate messages from tracked channels
if (!this.trackedChannels.includes(msg.channelId)) 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 is comming from
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;