From 8633a7538db972bfb0c94d2347aa036c112c64ac Mon Sep 17 00:00:00 2001 From: vincent <> Date: Thu, 27 Feb 2025 19:21:51 +0100 Subject: [PATCH] smart ping return, when pinging on discord, users are only pinged in one channel, ping are translated, so its possible to ping from irc --- Bridge/class/Message.js | 105 +++++++++++++++-------------- DiscordBot/class/Client.js | 132 ++++++++++++++++++++++++++++++++++++- 2 files changed, 188 insertions(+), 49 deletions(-) diff --git a/Bridge/class/Message.js b/Bridge/class/Message.js index e6cffbd..9df02f3 100644 --- a/Bridge/class/Message.js +++ b/Bridge/class/Message.js @@ -1,54 +1,63 @@ -export class Message{ - constructor(){ - this.text; - this.channel; - this.author; - this.source_type; // irc/discord... - } +export class Message { + constructor() { + this.text; + this.channel; + this.author; + this.source_type; // irc/discord... + } - async fromDiscordFormat(discord_message, discordClient){ - this.text = await this.escapeDiscordPing(discord_message.content, discordClient) - console.log("escaped text", this.text); - this.channel = discord_message.channelId - this.author = discord_message.author.displayName; - this.source_type = "discord" - } + async fromDiscordFormat(discord_message, discordClient) { + this.text = await this.escapeDiscordPing( + discord_message.content, + discordClient + ); + console.log("escaped text", this.text); + this.channel = discord_message.channelId; + this.author = discord_message.author.displayName; + 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" - } + fromIRCFormat(irc_message) { + (this.text = irc_message.message), + (this.channel = irc_message.target.toLowerCase()), + (this.author = irc_message.nick), + (this.source_type = "irc"); + } - async escapeDiscordPing(text, client) { - let res_text = text; - if (text.includes("<@")) { - const regex = /<@(\d+)>/g; - - const matches = [...text.matchAll(regex)]; - - const userReplacements = []; - - for (let i = 0; i < matches.length; i++) { - const match = matches[i]; - const userId = match[1]; - try { - const user = await client.users.fetch(userId); - userReplacements.push({ match, username: user.username }); - } catch (error) { - console.error("Error fetching user:", error); - userReplacements.push({ match, username: null }); - } - } + async escapeDiscordPing(text, client) { + let res_text = text; - for (let i = 0; i < userReplacements.length; i++) { - const { match, username } = userReplacements[i]; - - const replacement = username ? `@${username}` : match[0]; - res_text = res_text.replace(match[0], replacement); - } - } - return res_text; + const regex = /<@(\d+)>/g; + + const matches = [...text.matchAll(regex)]; + const userReplacements = []; + + for (let i = 0; i < matches.length; i++) { + const match = matches[i]; + const userId = match[1]; + try { + const user = await client.users.fetch(userId); + userReplacements.push({ match, username: user.username }); + } catch (error) { + console.error("Error fetching user:", error); + userReplacements.push({ match, username: null }); } + } + + for (let i = 0; i < userReplacements.length; i++) { + const { match, username } = userReplacements[i]; + + const replacement = username ? `@${username}` : match[0]; + res_text = res_text.replace(match[0], replacement); + } + + return res_text; + } + + copy(msg){ + this.text = msg.text; + this.channel = msg.channel; + this.author = msg.author; + this.source_type = msg.source_type; + } } diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index 8b0f2c1..e03b298 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -17,6 +17,9 @@ class DiscordClient extends Client { 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); @@ -39,10 +42,36 @@ class DiscordClient extends Client { 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(msg.text, chan); + this.sendTextInChannel(chan_msg.text, chan); } } } @@ -55,6 +84,37 @@ class DiscordClient extends Client { return res_msg; } + 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()); + } + + 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); + } + } + } + } + } + return channels; + } + sendTextInChannel(text, chan) { this.channels.cache .get(chan) @@ -65,6 +125,66 @@ class DiscordClient extends Client { ); } + 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 mapChannelIdsToGuild(channelIds) { + const channelToGuildMap = new Map(); + + 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; + } + start() { return new Promise(async (resolve, reject) => { try { @@ -74,6 +194,16 @@ class DiscordClient extends Client { } catch (error) { reject(error); } + + 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)); + } }); } }