diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index e03b298..8a327d0 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -2,210 +2,246 @@ 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", - ], - }); + constructor(bridge, verbose = false, channels) { + super({ + intents: [ + "Guilds", + "GuildMessages", + "GuildMessageReactions", + "GuildMessageTyping", + "GuildMembers", + "MessageContent", + ], + }); - this.bridge = bridge; - this.verbose = verbose; - this.trackedChannelsIds = channels; - this.trackedChannels = new Map(); - this.channelsGuilds = new Map(); - this.guildsMembers = new Map(); + this.bridge = bridge; + this.verbose = verbose; + this.trackedChannelsIds = channels; + this.trackedChannels = new Map(); + this.channelsGuilds = new Map(); + this.guildsMembers = new Map(); - this.on("messageCreate", (msg) => { - this.log("message recived", msg); + 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.trackedChannelsIds.includes(msg.channelId)) return; + // catch self messages + if (msg.author.id == this.user.id) return; + // only propagate messages from tracked channels + if (!this.trackedChannelsIds.includes(msg.channelId)) return; - const internal_msg = new Message(); - internal_msg - .fromDiscordFormat(msg, this) - .then(() => this.bridge.broadcast(internal_msg)); - }); - } - - log(...args) { - if (this.verbose) console.log("[Discord]", ...args); - } - - async broadcast(message) { - const msg = await this.formatMessageForForward(message); - - const mentioned_user = this.getMentionedUsers(message.text); - - const userToChanMap = new Map(); - mentioned_user.forEach((user) => - userToChanMap.set(user, this.findUserChannels(user)) - ); - - for (const chan of this.trackedChannelsIds) { - const chan_msg = new Message(); - chan_msg.copy(msg); - - for (let [user, user_chans] of userToChanMap) { - if ( - user_chans.includes(chan) && - !user_chans.includes(chan_msg.channel) - ) { - const user_id = this.getUserIdOfUserInChan( - chan, - user.replace("@", "") - ); - chan_msg.text = chan_msg.text.replace(user, "<@" + user_id + ">"); - - userToChanMap.delete(user); - } - } - - if (chan != msg.channel) { - // do not send in the channel the message is comming from - this.sendTextInChannel(chan_msg.text, chan); - } + const internal_msg = new Message(); + internal_msg + .fromDiscordFormat(msg, this) + .then(() => this.bridge.broadcast(internal_msg)); + }); } - } - async formatMessageForForward(msg) { - const res_msg = { ...msg }; + log(...args) { + if (this.verbose) console.log("[Discord]", ...args); + } - res_msg.text = `[**${msg.author}**]: ${res_msg.text}`; + async broadcast(message) { + const msg = await this.formatMessageForForward(message); - return res_msg; - } + const mentioned_users = this.getMentionedUsers(message.text); - getUserIdOfUserInChan(chan, username) { - return this.channelsGuilds - .get(chan) - .members.cache.find((member) => member.user.username === username).id; - } + const mentionned_users_info = new Map(); + mentioned_users.forEach((user) => + mentionned_users_info.set( + user, + this.getUserInfoFromMention(user.replace("@", "")), + ), + ); - getMentionedUsers(text) { - const regex = /@\S*(?=\s|$)/g; + for (const chan of this.trackedChannelsIds) { + const chan_msg = new Message(); + chan_msg.copy(msg); - const matches = [...text.matchAll(regex)]; + for (let [user, user_info] of mentionned_users_info) { + if ( + user_info.channels.includes(chan) && + !user_info.channels.includes(chan_msg.channel) + ) { + this.log( + "replaced with", + "<@" + (user_info.id ? user_info.id : user) + ">", + ); + // mentionned user is in the channel we are sending to, and not in the channel we are sending from + chan_msg.text = chan_msg.text.replace( + user, + "<@" + (user_info.id ? user_info.id : user) + ">", + ); - return matches.map((str) => str[0].trim()); - } - - findUserChannels(user_name) { - const channels = []; - for (const [guildId, membersMap] of this.guildsMembers) { - for (const [memberId, member] of membersMap) { - if (member.user.username === user_name) { - // Found the user, now find a readable channel - for (const [channelId, channel] of this.trackedChannels) { - if (this.memberCanReadChannel(memberId, channel)) { - channels.push(channelId); + mentionned_users_info.delete(user); + } + } + + if (chan != msg.channel) { + // do not send in the channel the message is comming from + this.sendTextInChannel(chan_msg.text, chan); } - } } - } - } - return channels; - } - - sendTextInChannel(text, chan) { - this.channels.cache - .get(chan) - ?.send(text) - .then(() => {}) - .catch((error) => - this.log("when trying to send message in ", chan, " got ", error) - ); - } - - memberCanReadChannel(userId, channel) { - const guildId = this.channelsGuilds.get(channel.id); - const member = this.guildsMembers.get(guildId).get(userId); - - if (!member.guild) { - console.log("Member not part of a guild"); - return false; } - const permissions = channel.permissionsFor(member); - return permissions && permissions.has("ViewChannel"); - } + async formatMessageForForward(msg) { + const res_msg = { ...msg }; - async mapChannelIdsToGuild(channelIds) { - const channelToGuildMap = new Map(); + res_msg.text = `[**${msg.author}**]: ${res_msg.text}`; - for (let i = 0; i < channelIds.length; i++) { - const channelId = channelIds[i]; - try { - const channel = await this.channels.fetch(channelId); + return res_msg; + } - if (channel.guild) { - channelToGuildMap.set(channel.id, channel.guild); + getUserIdOfUserInChan(chan, username) { + return this.channelsGuilds + .get(chan) + .members.cache.find((member) => member.user.username === username) + ?.id; + } + + getMentionedUsers(text) { + const regex = /@\S*(?=\s|$)/g; + + const matches = [...text.matchAll(regex)]; + + return matches.map((str) => str[0].trim()); + } + + getUserInfoFromMention(user_name) { + const info = { + channels: [], + id: null, + }; + for (const [chan, guild] of this.channelsGuilds) { + for (const [member_id, member] of guild.members.cache) { + this.log(member); + if (member.user.username === user_name) { + // maybe also check against global name and displayname + info.id = member.user.id; + // Found the user, now find readable channels + for (const [channelId, channel] of this.trackedChannels) { + if (this.memberCanReadChannel(member, channel)) { + info.channels.push(channelId); + } + } + } + } } - } catch (error) { - console.error(`Error fetching channel with ID ${channelId}:`, error); - } + return info; } - return channelToGuildMap; - } - - async getGuildMembers(guildId) { - const guild = await this.guilds.fetch(guildId); - const members = await guild.members.fetch(); - - const memberMap = new Map(); - members.forEach((member) => { - memberMap.set(member.id, member); - }); - - return memberMap; - } - - async getGuildsMembers(guildIds) { - const guildsMembersMap = new Map(); - - for (const guildId of guildIds) { - try { - const membersMap = await this.getGuildMembers(guildId); - - guildsMembersMap.set(guildId, membersMap); - } catch (error) { - console.error(`Error fetching members for guild ${guildId}:`, error); - } + sendTextInChannel(text, chan) { + this.channels.cache + .get(chan) + ?.send(text) + .then(() => {}) + .catch((error) => + this.log( + "when trying to send message in ", + chan, + " got ", + error, + ), + ); } - return guildsMembersMap; - } + memberCanReadChannel(member, channel) { + const permissions = channel.permissionsFor(member); + return permissions && permissions.has("ViewChannel"); + } - start() { - return new Promise(async (resolve, reject) => { - try { - await this.login(process.env.DISCORD_TOKEN); - this.log("Client connected"); - resolve(); - } catch (error) { - reject(error); - } + async mapChannelIdsToGuild(channelIds) { + const channelToGuildMap = new Map(); - this.channelsGuilds = await this.mapChannelIdsToGuild( - this.trackedChannelsIds - ); - this.guildsMembers = await this.getGuildsMembers( - Array.from(this.channelsGuilds.values()) - ); - for (const chanId of this.trackedChannelsIds) { - this.trackedChannels.set(chanId, await this.channels.fetch(chanId)); - } - }); - } + for (let i = 0; i < channelIds.length; i++) { + const channelId = channelIds[i]; + try { + const channel = await this.channels.fetch(channelId); + + if (channel.guild) { + channelToGuildMap.set(channel.id, channel.guild); + } + } catch (error) { + console.error( + `Error fetching channel with ID ${channelId}:`, + error, + ); + } + } + + return channelToGuildMap; + } + + async getGuildMembers(guildId) { + const guild = await this.guilds.fetch(guildId); + const members = await guild.members.fetch(); + + const memberMap = new Map(); + members.forEach((member) => { + memberMap.set(member.id, member); + }); + + return memberMap; + } + + async getGuildsMembers(guildIds) { + const guildsMembersMap = new Map(); + + for (const guildId of guildIds) { + try { + const membersMap = await this.getGuildMembers(guildId); + + guildsMembersMap.set(guildId, membersMap); + } catch (error) { + console.error( + `Error fetching members for guild ${guildId}:`, + error, + ); + } + } + + return guildsMembersMap; + } + + async refreshMembersCache() { + for (const [channel, guild] of this.channelsGuilds) { + guild.members.fetch().then((members) => { + this.log("members cache loaded for ", guild.name); + }); + } + } + + start() { + return new Promise(async (resolve, reject) => { + try { + await this.login(process.env.DISCORD_TOKEN); + this.log("Client connected"); + resolve(); + } catch (error) { + reject(error); + } + + this.channelsGuilds = await this.mapChannelIdsToGuild( + this.trackedChannelsIds, + ); + + this.refreshMembersCache(); + + for (const chanId of this.trackedChannelsIds) { + this.trackedChannels.set( + chanId, + await this.channels.fetch(chanId), + ); + } + }); + + this.on("guildMemberAdd", (member) => { + this.refreshMembersCache(); + }); + + this.on("guildMemberRemove", (member) => { + this.refreshMembersCache(); + }); + } } module.exports = DiscordClient; diff --git a/config.json b/config.json index 99fdfc8..1a76350 100644 --- a/config.json +++ b/config.json @@ -1,8 +1,5 @@ { - "verbose": true, - "discordChannels": [ - "1161739009614151751", - "727212463988146206" - ], - "ircChannels": ["#urlab"] + "verbose": true, + "discordChannels": ["1161739009614151751", "727212463988146206"], + "ircChannels": ["#urlab"] }