handle discord attachement

This commit is contained in:
vincent 2025-03-04 22:03:51 +01:00
parent bea6ed2fe2
commit 62ed5c67b1
3 changed files with 59 additions and 59 deletions

View file

@ -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;
}
}

View file

@ -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);

View file

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