From a0bffe375e01cd8238182ea19c5996c16664856b Mon Sep 17 00:00:00 2001 From: vincent <> Date: Wed, 26 Feb 2025 12:21:02 +0100 Subject: [PATCH] trying a smart ping method, ping is neutralized if the user pinged is in the channel of origin (mostly useless, because you cant ping a user that is not in the channel where the message is comming from) --- Bridge/class/Bridge.js | 2 +- DiscordBot/class/Client.js | 131 ++++++++++++++++++++++++++++++++++--- 2 files changed, 122 insertions(+), 11 deletions(-) diff --git a/Bridge/class/Bridge.js b/Bridge/class/Bridge.js index 439227c..b66d598 100644 --- a/Bridge/class/Bridge.js +++ b/Bridge/class/Bridge.js @@ -33,7 +33,7 @@ class Bridge { }); } - broadcast(msg) { + async broadcast(msg) { this.log("Broadcasting", msg); for (const client of this.clients) { diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index 8509c41..35425d1 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -1,6 +1,5 @@ const { Client } = require("discord.js"); -const { Message } = require("../../Bridge/class/Message") - +const { Message } = require("../../Bridge/class/Message"); class DiscordClient extends Client { constructor(bridge, verbose = false, channels) { @@ -14,9 +13,13 @@ class DiscordClient extends Client { "MessageContent", ], }); + this.bridge = bridge; this.verbose = verbose; - this.trackedChannels = channels; + this.trackedChannelsIds = channels; + this.trackedChannels = new Map(); + this.channelsGuilds = new Map(); + this.guildsMembers = new Map(); this.on("messageCreate", (msg) => { this.log("message recived", msg); @@ -24,7 +27,7 @@ class DiscordClient extends Client { // catch self messages if (msg.author.id == this.user.id) return; // only propagate messages from tracked channels - if (!this.trackedChannels.includes(msg.channelId)) return; + if (!this.trackedChannelsIds.includes(msg.channelId)) return; const internal_msg = new Message(); internal_msg.fromDiscordFormat(msg); @@ -36,21 +39,59 @@ class DiscordClient extends Client { if (this.verbose) console.log("[Discord]", ...args); } - broadcast(message) { - const msg = this.fromatMessageForForward(message); - for (const chan of this.trackedChannels) { - if (chan != msg.channel) { // do not send in the channel the message is comming from + async broadcast(message) { + const msg = await this.formatMessageForForward(message); + for (const chan of this.trackedChannelsIds) { + if (chan != msg.channel) { + // do not send in the channel the message is comming from this.sendTextInChannel(msg.text, chan); } } } - fromatMessageForForward(msg) { + async formatMessageForForward(msg) { const res_msg = { ...msg }; - res_msg.text = "[**" + msg.author + "**]: " + msg.text; + + res_msg.text = await this.escapePing(res_msg.text, (id) => { + return this.memberCanReadChannel(id, this.trackedChannels.get(msg.channel)); + }); + res_msg.text = `[**${msg.author}**]: ${res_msg.text}`; + return res_msg; } + async escapePing(text, filter) { + 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 this.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; + } + } + sendTextInChannel(text, chan) { this.channels.cache .get(chan) @@ -61,6 +102,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.id); + } + } 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 { @@ -70,6 +171,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)); + } }); } }