smart ping return, when pinging on discord, users are only pinged in one channel, ping are translated, so its possible to ping from irc
This commit is contained in:
parent
7755e6a252
commit
8633a7538d
2 changed files with 188 additions and 49 deletions
|
|
@ -7,27 +7,29 @@ export class Message{
|
|||
}
|
||||
|
||||
async fromDiscordFormat(discord_message, discordClient) {
|
||||
this.text = await this.escapeDiscordPing(discord_message.content, discordClient)
|
||||
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.displayName;
|
||||
this.source_type = "discord"
|
||||
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"
|
||||
(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++) {
|
||||
|
|
@ -48,7 +50,14 @@ export class Message{
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue