From 0926532073ead544dae483f24ec3434c0d6a8218 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Wed, 14 Aug 2024 15:48:48 -0600 Subject: [PATCH] fix getTopMember fallback for no member --- src/modules/utility/userinfo.js | 54 +++++++++++++++++++++++++++++++++ src/util/misc.js | 11 ++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/modules/utility/userinfo.js b/src/modules/utility/userinfo.js index 9e39c63..d97f5ee 100644 --- a/src/modules/utility/userinfo.js +++ b/src/modules/utility/userinfo.js @@ -19,6 +19,42 @@ const {lookupUser} = require("@util/selection.js"); const ONE_MONTH = 2628000; +let vencordFetch = 0; +const vencordBadges = new Map(); +const vencordContributors = new Set(); + +const REGEX_DEVS = /id: (\d+)n(,\n\s+badge: false)?/; +const REGEX_DEVS_GLOBAL = new RegExp(REGEX_DEVS.source, "g"); + +async function fetchVencordData() { + const badges = await fetch("https://badges.vencord.dev/badges.json", { + headers: { + "User-Agent": "HiddenPhox/userinfo (https://gitdab.com/Cynosphere/HiddenPhox)", + }, + }).then((res) => res.json()); + + vencordBadges.clear(); + for (const [id, entry] of Object.entries(badges)) { + vencordBadges.set(id, entry); + } + + const constants = await fetch( + "https://raw.githubusercontent.com/Vendicated/Vencord/main/src/utils/constants.ts" + ).then((res) => res.text()); + + vencordContributors.clear(); + const entries = constants.match(REGEX_DEVS_GLOBAL); + for (const match of entries) { + const [, id, noBadge] = match.match(REGEX_DEVS); + if (noBadge) continue; + if (id == 0) continue; + + vencordContributors.add(id); + } + + vencordFetch = Date.now() + 60 * 60 * 1000; +} + const userinfo = new Command("userinfo"); userinfo.category = "utility"; userinfo.helpText = "Get information on a user"; @@ -62,6 +98,14 @@ userinfo.callback = async function (msg, line) { member = guild.members.get(id); } + if (Date.now() > vencordFetch) { + try { + fetchVencordData(); + } catch { + // noop + } + } + // FIXME: horrible, probably needs to be moved out of this command for later const badges = []; @@ -179,6 +223,10 @@ userinfo.callback = async function (msg, line) { badges.push(`[${Icons.badges.quest_completed}](${BadgeURLs.quest_completed})`); } + if (vencordContributors.has(id)) { + badges.push(`[<:VencordContributor:1273333728709574667>](https://vencord.dev)`); + } + const defaultAvatar = getDefaultAvatar(id, user.discriminator ?? 0); const avatar = user.avatar ? CDNEndpoints.USER_AVATAR(id, user.avatar) : defaultAvatar; @@ -291,6 +339,8 @@ userinfo.callback = async function (msg, line) { } } + const vcBadges = vencordBadges.has(id) && vencordBadges.get(id); + const embed = { color: getTopColor(msg, id, user.accent_color ?? pastelize(id)), thumbnail: { @@ -335,6 +385,10 @@ userinfo.callback = async function (msg, line) { }${clanData.wildcard_descriptors.length > 0 ? ` \u2022 **${clanData.wildcard_descriptors.join(", ")}**` : ""}`, inline: true, }, + vcBadges?.length > 0 && { + name: `Vencord Donator Badge${vcBadges.length > 1 ? "s" : ""}`, + value: `- ${vcBadges.map(({tooltip, badge}) => `[${tooltip}](${badge})`).join("\n- ")}`, + }, member?.roles?.length > 0 && { name: "Roles", value: member.roles diff --git a/src/util/misc.js b/src/util/misc.js index e3d3aa4..712ad15 100644 --- a/src/util/misc.js +++ b/src/util/misc.js @@ -21,13 +21,14 @@ function getTopColor(msg, id, fallback = 0x7289da) { const guild = msg.channel?.guild ?? hf.bot.guilds.get(msg.guildID); if (!guild) return fallback; - const roles = guild.members - .get(id) - .roles.map((role) => guild.roles.get(role)) - .filter((role) => role.color); + const member = guild.members.get(id); + + if (!member) return fallback; + + const roles = member.roles.map((role) => guild.roles.get(role)).filter((role) => role.color); roles.sort((a, b) => b.position - a.position); - return roles[0]?.color || fallback; + return roles[0]?.color ?? fallback; } function safeString(string, newLines = true) {