avatar: cleanup code so guild avatars are more consistent

This commit is contained in:
Cynthia Foxwell 2024-08-14 16:32:53 -06:00
parent 45b402ae38
commit a6ff9c275f
3 changed files with 31 additions and 12 deletions

View file

@ -1,6 +1,7 @@
const Command = require("@lib/command.js");
const {CDNEndpoints} = require("@util/dconstants.js");
const {APIEndpoints, CDNEndpoints} = require("@util/dconstants.js");
const {Icons} = require("@util/constants.js");
const {formatUsername, getDefaultAvatar} = require("@util/misc.js");
const {lookupUser} = require("@util/selection.js");
@ -13,8 +14,8 @@ avatar.addAlias("av");
avatar.addAlias("avi");
avatar.addAlias("pfp");
avatar.callback = async function (msg, line, [user], {server, guild}) {
let member = msg.member ?? msg.author ?? msg.user;
const guildObj = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
let id = msg.author?.id ?? msg.user?.id;
const guildObj = msg.channel.guild ?? hf.bot.guilds.get(msg.guildID);
if (server || guild) {
if (!msg.guildID) {
@ -50,20 +51,38 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
if (lookup == "No results" || lookup == "Canceled" || lookup == "Request timed out") {
return lookup;
} else {
member = lookup;
id = lookup.id;
}
}
let userObj;
try {
userObj = await hf.bot.requestHandler.request("GET", APIEndpoints.USER(id), true);
} catch (err) {
if (err.code == 10013) {
return "User not found.";
} else {
return `${Icons.silk.error} Failed to get user:\n\`\`\`\n${err}\`\`\``;
}
}
if (!userObj) return `${Icons.silk.error} Failed to get user without erroring.`;
let member;
if (guildObj) {
member = guild.members.get(id);
}
const baseEmbed = {
title: `Avatar for \`${formatUsername(member)}\``,
title: `Avatar for \`${formatUsername(userObj)}\``,
};
const defaultAvatar = getDefaultAvatar(member.id, member.user?.discriminator ?? member.discriminator ?? 0);
const normalAvatar = member.user?.avatar ?? member.avatar;
const guildAvatar = guildObj && guildObj.members.get(member.id) ? member.avatar : null;
const defaultAvatar = getDefaultAvatar(id, userObj.discriminator ?? 0);
const normalAvatar = userObj.avatar;
const guildAvatar = member && member.avatar;
const normalUrl = normalAvatar ? CDNEndpoints.USER_AVATAR(member.id, normalAvatar) : defaultAvatar;
const guildUrl = guildAvatar && CDNEndpoints.GUILD_MEMBER_AVATAR(guildObj.id, member.id, guildAvatar);
const normalUrl = normalAvatar ? CDNEndpoints.USER_AVATAR(id, normalAvatar) : defaultAvatar;
const guildUrl = guildAvatar && CDNEndpoints.GUILD_MEMBER_AVATAR(guildObj.id, id, guildAvatar);
const res = await fetch(normalUrl, {method: "HEAD"});
const mod = res.headers.get("last-modified");

View file

@ -56,7 +56,7 @@ presence.callback = async function (msg, line) {
return user;
} else {
let member = user;
const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
const guild = msg.channel.guild ?? hf.bot.guilds.get(msg.guildID);
if (guild) {
if (guild.members.has(user.id)) {
member = guild.members.get(user.id);

View file

@ -94,7 +94,7 @@ userinfo.callback = async function (msg, line) {
let guild, member;
if (msg.guildID) {
guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
guild = msg.channel.guild ?? hf.bot.guilds.get(msg.guildID);
member = guild.members.get(id);
}