From 265047c71af1e098b78ae528ca3de7c038a498a1 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Thu, 15 May 2025 10:20:47 -0600 Subject: [PATCH] userinfo: fix getting server tag guild name --- src/modules/logging.js | 2 +- src/modules/utility/guildinfo.js | 106 +++++++++++-------------------- src/modules/utility/userinfo.js | 31 +++------ src/util/misc.js | 40 ++++++++++++ 4 files changed, 88 insertions(+), 91 deletions(-) diff --git a/src/modules/logging.js b/src/modules/logging.js index 7c716f1..a7e1cb6 100644 --- a/src/modules/logging.js +++ b/src/modules/logging.js @@ -279,7 +279,7 @@ events.add("guildAuditLogEntryCreate", "logging", async function (entry) { default: { fields.push({ name: key, - value: `\`${oldValue}\` -> \`${newValue}\``, + value: formatChange(oldValue, newValue), inline: true, }); break; diff --git a/src/modules/utility/guildinfo.js b/src/modules/utility/guildinfo.js index 309c5fb..457e1c6 100644 --- a/src/modules/utility/guildinfo.js +++ b/src/modules/utility/guildinfo.js @@ -19,7 +19,7 @@ const { ChannelTypeNames, } = require("#util/constants.js"); const {snowflakeToTimestamp} = require("#util/time.js"); -const {getGuild, tryGetGuild, formatGuildFeatures, makeGuildFeatureFields} = require("#util/misc.js"); +const {getGuild, getGuildProfile, tryGetGuild, formatGuildFeatures, makeGuildFeatureFields} = require("#util/misc.js"); const guildinfo = new Command("guildinfo"); guildinfo.category = "utility"; @@ -72,49 +72,16 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { id = snowflake; } - let clan = await hf.bot.requestHandler.request("GET", APIEndpoints.CLAN(id), true).catch(() => {}); - if (!clan) { - if (_guild?.data?.profile) { - clan = _guild?.profile; - } else { - const verif = await hf.bot.requestHandler - .request("GET", `${APIEndpoints.GUILD_MEMBER_VERIFICATION(id)}?with_guild=true`, true) - .catch(() => {}); - if (verif?.profile) { - clan = verif.profile; - } else { - let code = - _guild?.data?.vanityURL ?? - _guild?.data?.store_page?.guild?.invite?.code ?? - _guild?.data?.store_page?.role_subscription?.purchase_page_invite?.code ?? - _guild?.data?.instant_invite?.replace( - /(https?:\/\/)?(canary\.|ptb\.)?discord(\.gg|(app)?.com\/invite)\//, - "" - ); - if (!code && hf.bot.guilds.has(id)) { - const invites = await hf.bot.requestHandler - .request("GET", APIEndpoints.GUILD_INVITES(id), true) - .catch(() => {}); - if (invites?.[0]?.code) code = invites[0].code; - } - - if (code) { - const invite = await hf.bot.requestHandler - .request("GET", `${APIEndpoints.INVITE(code)}?with_counts=true&with_expiration=true`) - .catch(() => {}); - if (invite?.profile) clan = invite.profile; - } - } - } - } - - if (clan) { + const profile = await getGuildProfile(id, _guild); + if (profile) { const images = []; - const game_ids = clan.game_ids ?? clan.game_application_ids ?? []; + const game_ids = profile.game_ids ?? profile.game_application_ids ?? []; const games = await Promise.all( game_ids - .sort((a, b) => (clan.game_activity[b]?.activity_score ?? 0) - (clan.game_activity[a]?.activity_score ?? 0)) + .sort( + (a, b) => (profile.game_activity[b]?.activity_score ?? 0) - (profile.game_activity[a]?.activity_score ?? 0) + ) .map(async (id) => { let game = Games.find((x) => x.id == id); if (!game) { @@ -123,7 +90,7 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { let out = `${game.name} (\`${id}\`)`; - if (clan.game_activity[id]?.activity_level > 1) { + if (profile.game_activity[id]?.activity_level > 1) { out = `:fire: ${out}`; } else { out = `${Icons.blank} ${out}`; @@ -133,13 +100,14 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { }) ); - if (clan.wildcard_descriptors) clan.wildcard_descriptors = clan.wildcard_descriptors.filter((x) => x != ""); + if (profile.wildcard_descriptors) + profile.wildcard_descriptors = profile.wildcard_descriptors.filter((x) => x != ""); const termLines = []; let currentTerm = ""; - if (clan.search_terms) { - for (const index in clan.search_terms) { - const term = clan.search_terms[index]; + if (profile.search_terms) { + for (const index in profile.search_terms) { + const term = profile.search_terms[index]; const formattedTerm = `\`\u2004${term.replaceAll(" ", "\u2005")}\u2004\``; if (currentTerm.length + 1 + formattedTerm.length > 56) { termLines.push(currentTerm); @@ -147,11 +115,11 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { } else { currentTerm += "\u2004" + formattedTerm; } - if (index == clan.search_terms.length - 1) termLines.push(currentTerm); + if (index == profile.search_terms.length - 1) termLines.push(currentTerm); } - } else if (clan.traits) { - for (const index in clan.traits) { - const term = clan.traits[index]; + } else if (profile.traits) { + for (const index in profile.traits) { + const term = profile.traits[index]; const formattedTerm = `${term.emoji_name ? `:${term.emoji_name}:` : ""}\`\u2004${term.label.replaceAll( " ", "\u2005" @@ -162,7 +130,7 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { } else { currentTerm += "\u2004" + formattedTerm; } - if (index == clan.traits.length - 1) termLines.push(currentTerm); + if (index == profile.traits.length - 1) termLines.push(currentTerm); } } @@ -177,21 +145,21 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { } clanEmbed = { - color: clan.brand_color_primary ? parseInt(clan.brand_color_primary.replace("#", "0x")) : 0, - title: _guild == null ? clan.name : null, + color: profile.brand_color_primary ? parseInt(profile.brand_color_primary.replace("#", "0x")) : 0, + title: _guild == null ? profile.name : null, author: { - name: clan.tag, + name: profile.tag, }, description: - clan.playstyle || clan.wildcard_descriptors - ? `-# :video_game:${ClanPlaystyle[clan.playstyle] ?? "Unknown"}${ - clan.wildcard_descriptors.length > 0 ? ` \u2022 **${clan.wildcard_descriptors.join(", ")}**` : "" - }\n\n${clan.description ?? "*No description*"}` - : clan.description ?? "*No description*", + profile.playstyle || profile.wildcard_descriptors + ? `-# :video_game:${ClanPlaystyle[profile.playstyle] ?? "Unknown"}${ + profile.wildcard_descriptors.length > 0 ? ` \u2022 **${profile.wildcard_descriptors.join(", ")}**` : "" + }\n\n${profile.description ?? "*No description*"}` + : profile.description ?? "*No description*", fields: [ !_guild && { name: "Member Count", - value: clan.member_count, + value: profile.member_count, inline: true, }, termLines.length > 0 && { @@ -209,28 +177,30 @@ guildinfo.callback = async function (msg, line, args, {nolocal, debug}) { value: gameLines2, inline: false, }, - clan.badge_color_primary && { + profile.badge_color_primary && { name: "Badge Colors", - value: `${clan.badge_color_primary}, ${clan.badge_color_secondary}`, + value: `${profile.badge_color_primary}, ${profile.badge_color_secondary}`, inline: true, }, - clan.brand_color_primary && { + profile.brand_color_primary && { name: "Banner/Brand Colors", - value: `${clan.brand_color_primary}${clan.brand_color_secondary ? `, ${clan.brand_color_secondary}` : ""}`, + value: `${profile.brand_color_primary}${ + profile.brand_color_secondary ? `, ${profile.brand_color_secondary}` : "" + }`, inline: true, }, ].filter((x) => !!x), - thumbnail: !_guild ? {url: CDNEndpoints.GUILD_ICON(clan.id, clan.icon_hash)} : null, + thumbnail: !_guild ? {url: CDNEndpoints.GUILD_ICON(profile.id, profile.icon_hash)} : null, footer: !_guild ? {text: "Fetched from clan"} : null, }; - if (clan.badge_hash) { - const url = CDNEndpoints.CLAN_BADGE(clan.id, clan.badge_hash); + if (profile.badge_hash) { + const url = CDNEndpoints.CLAN_BADGE(profile.id, profile.badge_hash); images.push(`[Badge](${url})`); clanEmbed.author.icon_url = url; } - if (clan.banner_hash) { - const url = CDNEndpoints.CLAN_BANNER(clan.id, clan.banner_hash); + if (profile.banner_hash) { + const url = CDNEndpoints.CLAN_BANNER(profile.id, profile.banner_hash); images.push(`[Banner](${url})`); clanEmbed.image = {url}; } diff --git a/src/modules/utility/userinfo.js b/src/modules/utility/userinfo.js index d0ec781..114bd3b 100644 --- a/src/modules/utility/userinfo.js +++ b/src/modules/utility/userinfo.js @@ -8,12 +8,11 @@ const { ApplicationFlags, BadgeURLs, CDNEndpoints, - ClanPlaystyle, UserFlags, } = require("#util/dconstants.js"); const {Icons} = require("#util/constants.js"); -const {formatUsername, getDefaultAvatar, getTopColor, pastelize} = require("#util/misc.js"); +const {formatUsername, getDefaultAvatar, getGuild, getTopColor, pastelize} = require("#util/misc.js"); const {snowflakeToTimestamp} = require("#util/time.js"); const {lookupUser} = require("#util/selection.js"); @@ -306,13 +305,8 @@ userinfo.callback = async function (msg, line) { if (user.system) { descLines.push("**System account**"); } - let clanData; - if (user.clan?.identity_guild_id) { - clanData = await hf.bot.requestHandler - .request("GET", APIEndpoints.CLAN(user.clan.identity_guild_id), true) - .catch(() => {}); - if (clanData) clanData.wildcard_descriptors = clanData.wildcard_descriptors.filter((x) => x != ""); - } + + const primaryGuild = await getGuild(user.primary_guild.identity_guild_id); if (anyMember) { const icons = []; @@ -347,15 +341,10 @@ userinfo.callback = async function (msg, line) { const embed = { color: getTopColor(msg, id, user.accent_color ?? pastelize(id)), - author: clanData + author: user.primary_guild?.identity_guild_id ? { - icon_url: CDNEndpoints.CLAN_BADGE(clanData.id, clanData.badge_hash), - name: clanData.tag, - } - : user.clan?.identity_guild_id - ? { - icon_url: CDNEndpoints.CLAN_BADGE(user.clan.identity_guild_id, user.clan.badge), - name: user.clan.tag, + icon_url: CDNEndpoints.CLAN_BADGE(user.primary_guild.identity_guild_id, user.primary_guild.badge), + name: user.primary_guild.tag, } : null, thumbnail: { @@ -393,11 +382,9 @@ userinfo.callback = async function (msg, line) { } (\`${user.avatar_decoration_data.sku_id}\`)\n[Image](${decorationUrl})`, inline: true, }, - clanData && { - name: "Clan", - value: `${clanData.name} (\`${user.clan.identity_guild_id}\`)\n-# :video_game:${ - ClanPlaystyle[clanData.playstyle] ?? "Unknown" - }${clanData.wildcard_descriptors.length > 0 ? ` \u2022 **${clanData.wildcard_descriptors.join(", ")}**` : ""}`, + primaryGuild?.name && { + name: "Server Tag", + value: `${primaryGuild.name} (\`${user.primary_guild.identity_guild_id}\`)`, inline: true, }, vcBadges?.length > 0 && { diff --git a/src/util/misc.js b/src/util/misc.js index fcea33d..775d610 100644 --- a/src/util/misc.js +++ b/src/util/misc.js @@ -93,6 +93,45 @@ async function getGuild(id, noLocal = false) { return null; } +async function getGuildProfile(id, guild = null) { + if (guild == null) guild = await getGuild(id); + + let profile = await hf.bot.requestHandler.request("GET", APIEndpoints.CLAN(id), true).catch(() => {}); + if (!profile) { + if (guild?.data?.profile) { + profile = guild?.profile; + } else { + const verif = await hf.bot.requestHandler + .request("GET", `${APIEndpoints.GUILD_MEMBER_VERIFICATION(id)}?with_guild=true`, true) + .catch(() => {}); + if (verif?.profile) { + profile = verif.profile; + } else { + let code = + guild?.data?.vanityURL ?? + guild?.data?.store_page?.guild?.invite?.code ?? + guild?.data?.store_page?.role_subscription?.purchase_page_invite?.code ?? + guild?.data?.instant_invite?.replace(/(https?:\/\/)?(canary\.|ptb\.)?discord(\.gg|(app)?.com\/invite)\//, ""); + if (!code && hf.bot.guilds.has(id)) { + const invites = await hf.bot.requestHandler + .request("GET", APIEndpoints.GUILD_INVITES(id), true) + .catch(() => {}); + if (invites?.[0]?.code) code = invites[0].code; + } + + if (code) { + const invite = await hf.bot.requestHandler + .request("GET", `${APIEndpoints.INVITE(code)}?with_counts=true&with_expiration=true`) + .catch(() => {}); + if (invite?.profile) profile = invite.profile; + } + } + } + } + + return profile; +} + async function tryGetGuild(id) { const sources = { local: false, @@ -282,6 +321,7 @@ module.exports = { hastebin, getUploadLimit, getGuild, + getGuildProfile, tryGetGuild, enumKeyToName, formatGuildFeatures,