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
|
|
@ -1,54 +1,63 @@
|
||||||
export class Message{
|
export class Message {
|
||||||
constructor(){
|
constructor() {
|
||||||
this.text;
|
this.text;
|
||||||
this.channel;
|
this.channel;
|
||||||
this.author;
|
this.author;
|
||||||
this.source_type; // irc/discord...
|
this.source_type; // irc/discord...
|
||||||
}
|
}
|
||||||
|
|
||||||
async fromDiscordFormat(discord_message, discordClient){
|
async fromDiscordFormat(discord_message, discordClient) {
|
||||||
this.text = await this.escapeDiscordPing(discord_message.content, discordClient)
|
this.text = await this.escapeDiscordPing(
|
||||||
console.log("escaped text", this.text);
|
discord_message.content,
|
||||||
this.channel = discord_message.channelId
|
discordClient
|
||||||
this.author = discord_message.author.displayName;
|
);
|
||||||
this.source_type = "discord"
|
console.log("escaped text", this.text);
|
||||||
}
|
this.channel = discord_message.channelId;
|
||||||
|
this.author = discord_message.author.displayName;
|
||||||
|
this.source_type = "discord";
|
||||||
|
}
|
||||||
|
|
||||||
fromIRCFormat(irc_message){
|
fromIRCFormat(irc_message) {
|
||||||
this.text = irc_message.message,
|
(this.text = irc_message.message),
|
||||||
this.channel = irc_message.target.toLowerCase(),
|
(this.channel = irc_message.target.toLowerCase()),
|
||||||
this.author = irc_message.nick,
|
(this.author = irc_message.nick),
|
||||||
this.source_type = "irc"
|
(this.source_type = "irc");
|
||||||
}
|
}
|
||||||
|
|
||||||
async escapeDiscordPing(text, client) {
|
async escapeDiscordPing(text, client) {
|
||||||
let res_text = text;
|
let res_text = text;
|
||||||
if (text.includes("<@")) {
|
|
||||||
const regex = /<@(\d+)>/g;
|
|
||||||
|
|
||||||
const matches = [...text.matchAll(regex)];
|
const regex = /<@(\d+)>/g;
|
||||||
|
|
||||||
const userReplacements = [];
|
const matches = [...text.matchAll(regex)];
|
||||||
|
const userReplacements = [];
|
||||||
|
|
||||||
for (let i = 0; i < matches.length; i++) {
|
for (let i = 0; i < matches.length; i++) {
|
||||||
const match = matches[i];
|
const match = matches[i];
|
||||||
const userId = match[1];
|
const userId = match[1];
|
||||||
try {
|
try {
|
||||||
const user = await client.users.fetch(userId);
|
const user = await client.users.fetch(userId);
|
||||||
userReplacements.push({ match, username: user.username });
|
userReplacements.push({ match, username: user.username });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching user:", error);
|
console.error("Error fetching user:", error);
|
||||||
userReplacements.push({ match, username: null });
|
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;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,9 @@ 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);
|
||||||
|
|
@ -39,10 +42,36 @@ class DiscordClient extends Client {
|
||||||
|
|
||||||
async broadcast(message) {
|
async broadcast(message) {
|
||||||
const msg = await this.formatMessageForForward(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) {
|
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) {
|
if (chan != msg.channel) {
|
||||||
// do not send in the channel the message is comming from
|
// 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;
|
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) {
|
sendTextInChannel(text, chan) {
|
||||||
this.channels.cache
|
this.channels.cache
|
||||||
.get(chan)
|
.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() {
|
start() {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -74,6 +194,16 @@ 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));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue