escape ping from discord messages to avoid double ping

This commit is contained in:
vincent 2025-02-26 12:45:49 +01:00
parent a0bffe375e
commit d21fd5d71a
3 changed files with 40 additions and 117 deletions

View file

@ -6,8 +6,9 @@ export class Message{
this.source_type; // irc/discord...
}
fromDiscordFormat(discord_message){
this.text = discord_message.content
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.username
this.source_type = "discord"
@ -19,4 +20,36 @@ export class Message{
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 });
}
}
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;
}
}
}

View file

@ -17,9 +17,6 @@ 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);
@ -30,8 +27,9 @@ class DiscordClient extends Client {
if (!this.trackedChannelsIds.includes(msg.channelId)) return;
const internal_msg = new Message();
internal_msg.fromDiscordFormat(msg);
this.bridge.broadcast(internal_msg);
internal_msg
.fromDiscordFormat(msg, this)
.then(() => this.bridge.broadcast(internal_msg));
});
}
@ -52,46 +50,11 @@ class DiscordClient extends Client {
async formatMessageForForward(msg) {
const res_msg = { ...msg };
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)
@ -102,66 +65,6 @@ 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 {
@ -171,16 +74,6 @@ 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));
}
});
}
}

View file

@ -1,8 +1,5 @@
{
"verbose": true,
"discordChannels": [
"1161739009614151751",
"727212463988146206"
],
"ircChannels": ["#urlab"]
"discordChannels": ["1335942797726846989", "1335962777063264326"],
"ircChannels": ["#vincentdev"]
}