From 62ed5c67b1d4ac60d351611c72e56acd6d7aeecf Mon Sep 17 00:00:00 2001 From: vincent <> Date: Tue, 4 Mar 2025 22:03:51 +0100 Subject: [PATCH] handle discord attachement --- Bridge/class/Message.js | 113 +++++++++++++++++++------------------ DiscordBot/class/Client.js | 1 - config.json | 4 +- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/Bridge/class/Message.js b/Bridge/class/Message.js index 9df02f3..5018fe6 100644 --- a/Bridge/class/Message.js +++ b/Bridge/class/Message.js @@ -1,63 +1,64 @@ export class Message { - constructor() { - this.text; - this.channel; - this.author; - this.source_type; // irc/discord... - } - - 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.displayName; - 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"); - } - - async escapeDiscordPing(text, client) { - let res_text = text; - - 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 }); - } + constructor() { + this.text; + this.channel; + this.author; + this.source_type; // irc/discord... } - 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); + async fromDiscordFormat(discord_message, discordClient) { + this.text = await this.escapeDiscordPing( + discord_message.content, + discordClient, + ); + this.text += discord_message.attachments.map((a) => a.url).join("\n"); + console.log("escaped text", this.text); + this.channel = discord_message.channelId; + this.author = discord_message.author.displayName; + this.source_type = "discord"; } - return res_text; - } + fromIRCFormat(irc_message) { + (this.text = irc_message.message), + (this.channel = irc_message.target.toLowerCase()), + (this.author = irc_message.nick), + (this.source_type = "irc"); + } - copy(msg){ - this.text = msg.text; - this.channel = msg.channel; - this.author = msg.author; - this.source_type = msg.source_type; - } + async escapeDiscordPing(text, client) { + let res_text = text; + + 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; + } + + copy(msg) { + this.text = msg.text; + this.channel = msg.channel; + this.author = msg.author; + this.source_type = msg.source_type; + } } diff --git a/DiscordBot/class/Client.js b/DiscordBot/class/Client.js index 8a327d0..fadb3f4 100644 --- a/DiscordBot/class/Client.js +++ b/DiscordBot/class/Client.js @@ -19,7 +19,6 @@ class DiscordClient extends Client { this.trackedChannelsIds = channels; this.trackedChannels = new Map(); this.channelsGuilds = new Map(); - this.guildsMembers = new Map(); this.on("messageCreate", (msg) => { this.log("message recived", msg); diff --git a/config.json b/config.json index 1a76350..d1d13b3 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { "verbose": true, - "discordChannels": ["1161739009614151751", "727212463988146206"], - "ircChannels": ["#urlab"] + "discordChannels": ["1335942797726846989", "1335962777063264326"], + "ircChannels": ["#vincentdev"] }