54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
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;
|
|
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 text;
|
|
}
|
|
}
|