fix getTopMember fallback for no member
This commit is contained in:
		
							parent
							
								
									ff8cbabf7a
								
							
						
					
					
						commit
						0926532073
					
				
					 2 changed files with 60 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue