escape ping from discord messages to avoid double ping
This commit is contained in:
parent
a0bffe375e
commit
d21fd5d71a
3 changed files with 40 additions and 117 deletions
|
|
@ -6,8 +6,9 @@ export class Message{
|
||||||
this.source_type; // irc/discord...
|
this.source_type; // irc/discord...
|
||||||
}
|
}
|
||||||
|
|
||||||
fromDiscordFormat(discord_message){
|
async fromDiscordFormat(discord_message, discordClient){
|
||||||
this.text = discord_message.content
|
this.text = await this.escapeDiscordPing(discord_message.content, discordClient)
|
||||||
|
console.log("escaped text", this.text);
|
||||||
this.channel = discord_message.channelId
|
this.channel = discord_message.channelId
|
||||||
this.author = discord_message.author.username
|
this.author = discord_message.author.username
|
||||||
this.source_type = "discord"
|
this.source_type = "discord"
|
||||||
|
|
@ -19,4 +20,36 @@ export class Message{
|
||||||
this.author = irc_message.nick,
|
this.author = irc_message.nick,
|
||||||
this.source_type = "irc"
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,6 @@ class DiscordClient extends Client {
|
||||||
this.bridge = bridge;
|
this.bridge = bridge;
|
||||||
this.verbose = verbose;
|
this.verbose = verbose;
|
||||||
this.trackedChannelsIds = channels;
|
this.trackedChannelsIds = channels;
|
||||||
this.trackedChannels = new Map();
|
|
||||||
this.channelsGuilds = new Map();
|
|
||||||
this.guildsMembers = new Map();
|
|
||||||
|
|
||||||
this.on("messageCreate", (msg) => {
|
this.on("messageCreate", (msg) => {
|
||||||
this.log("message recived", msg);
|
this.log("message recived", msg);
|
||||||
|
|
@ -30,8 +27,9 @@ class DiscordClient extends Client {
|
||||||
if (!this.trackedChannelsIds.includes(msg.channelId)) return;
|
if (!this.trackedChannelsIds.includes(msg.channelId)) return;
|
||||||
|
|
||||||
const internal_msg = new Message();
|
const internal_msg = new Message();
|
||||||
internal_msg.fromDiscordFormat(msg);
|
internal_msg
|
||||||
this.bridge.broadcast(internal_msg);
|
.fromDiscordFormat(msg, this)
|
||||||
|
.then(() => this.bridge.broadcast(internal_msg));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -52,46 +50,11 @@ class DiscordClient extends Client {
|
||||||
async formatMessageForForward(msg) {
|
async formatMessageForForward(msg) {
|
||||||
const res_msg = { ...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}`;
|
res_msg.text = `[**${msg.author}**]: ${res_msg.text}`;
|
||||||
|
|
||||||
return res_msg;
|
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) {
|
sendTextInChannel(text, chan) {
|
||||||
this.channels.cache
|
this.channels.cache
|
||||||
.get(chan)
|
.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() {
|
start() {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -171,16 +74,6 @@ class DiscordClient extends Client {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reject(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));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
{
|
{
|
||||||
"verbose": true,
|
"verbose": true,
|
||||||
"discordChannels": [
|
"discordChannels": ["1335942797726846989", "1335962777063264326"],
|
||||||
"1161739009614151751",
|
"ircChannels": ["#vincentdev"]
|
||||||
"727212463988146206"
|
|
||||||
],
|
|
||||||
"ircChannels": ["#urlab"]
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue