UNPKG

@fantinodavide/discord-html-transcripts

Version:

A nicely formatted html transcript generator for discord.js.

145 lines 6.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildProfiles = buildProfiles; const discord_js_1 = require("discord.js"); async function buildProfiles(messages) { const profiles = {}; const roles = {}; const channels = {}; // loop through messages for (const message of messages) { // add all users const author = message.author; if (!profiles[author.id]) { // add profile profiles[author.id] = buildProfile(message.member, author); } // add interaction users if (message.interaction) { const user = message.interaction.user; if (!profiles[user.id]) { profiles[user.id] = buildProfile(null, user); } } // threads if (message.thread && message.thread.lastMessage) { profiles[message.thread.lastMessage.author.id] = buildProfile(message.thread.lastMessage.member, message.thread.lastMessage.author); } // extract user mentions from message content and V2 components await extractMentionedEntities(message, profiles, roles, channels); } // return as a JSON with all entity types return Object.assign(Object.assign({}, profiles), { _roles: roles, _channels: channels }); } async function extractMentionedEntities(message, profiles, roles, channels) { var _a; const userMentions = new Set(); const roleMentions = new Set(); const channelMentions = new Set(); // Extract from regular message content if (message.content) { extractMentionsFromText(message.content, userMentions, roleMentions, channelMentions); } // Extract from V2 components if (message.components) { extractMentionsFromComponents(message.components, userMentions, roleMentions, channelMentions); } // Resolve mentioned users for (const userId of userMentions) { if (!profiles[userId]) { try { const user = await message.client.users.fetch(userId); const member = ((_a = message.guild) === null || _a === void 0 ? void 0 : _a.members.cache.get(userId)) || (message.guild ? await message.guild.members.fetch(userId).catch(() => null) : null); profiles[userId] = buildProfile(member, user); } catch (error) { // If user can't be fetched, create a minimal profile profiles[userId] = { author: `User ${userId}`, bot: false, verified: false, }; } } } // Resolve mentioned roles for (const roleId of roleMentions) { if (!roles[roleId] && message.guild) { try { const role = await message.guild.roles.fetch(roleId); roles[roleId] = { name: (role === null || role === void 0 ? void 0 : role.name) || `Role ${roleId}`, color: (role === null || role === void 0 ? void 0 : role.hexColor) || '#99aab5', }; } catch (error) { roles[roleId] = { name: `Role ${roleId}`, color: '#99aab5', }; } } } // Resolve mentioned channels for (const channelId of channelMentions) { if (!channels[channelId]) { try { const channel = await message.client.channels.fetch(channelId); channels[channelId] = { name: (channel === null || channel === void 0 ? void 0 : channel.name) || `Channel ${channelId}`, type: (channel === null || channel === void 0 ? void 0 : channel.type) || 'text', }; } catch (error) { channels[channelId] = { name: `Channel ${channelId}`, type: 'text', }; } } } } function extractMentionsFromText(text, userMentions, roleMentions, channelMentions) { // User mentions: <@123> or <@!123> const userRegex = /<@!?(\d+)>/g; let match; while ((match = userRegex.exec(text)) !== null) { userMentions.add(match[1]); } // Role mentions: <@&123> const roleRegex = /<@&(\d+)>/g; while ((match = roleRegex.exec(text)) !== null) { roleMentions.add(match[1]); } // Channel mentions: <#123> const channelRegex = /<#(\d+)>/g; while ((match = channelRegex.exec(text)) !== null) { channelMentions.add(match[1]); } } function extractMentionsFromComponents(components, userMentions, roleMentions, channelMentions) { var _a; for (const component of components) { if ((_a = component.data) === null || _a === void 0 ? void 0 : _a.content) { extractMentionsFromText(component.data.content, userMentions, roleMentions, channelMentions); } // Recursively check nested components if (component.components) { extractMentionsFromComponents(component.components, userMentions, roleMentions, channelMentions); } } } function buildProfile(member, author) { var _a, _b, _c, _d, _e, _f, _g, _h; return { author: (_b = (_a = member === null || member === void 0 ? void 0 : member.nickname) !== null && _a !== void 0 ? _a : author.displayName) !== null && _b !== void 0 ? _b : author.username, avatar: (_c = member === null || member === void 0 ? void 0 : member.displayAvatarURL({ size: 64 })) !== null && _c !== void 0 ? _c : author.displayAvatarURL({ size: 64 }), roleColor: member === null || member === void 0 ? void 0 : member.displayHexColor, roleIcon: (_e = (_d = member === null || member === void 0 ? void 0 : member.roles.icon) === null || _d === void 0 ? void 0 : _d.iconURL()) !== null && _e !== void 0 ? _e : undefined, roleName: (_g = (_f = member === null || member === void 0 ? void 0 : member.roles.hoist) === null || _f === void 0 ? void 0 : _f.name) !== null && _g !== void 0 ? _g : undefined, bot: author.bot, verified: (_h = author.flags) === null || _h === void 0 ? void 0 : _h.has(discord_js_1.UserFlags.VerifiedBot), }; } //# sourceMappingURL=buildProfiles.js.map