utility: add guild checks to avatar and banner

This commit is contained in:
Cynthia Foxwell 2024-07-28 16:13:00 -06:00
parent 3914429d21
commit 9873780ac0
2 changed files with 22 additions and 20 deletions

View file

@ -11,13 +11,13 @@ avatar.helpText = "Get avatar of a user";
avatar.usage = "<user>";
avatar.callback = async function (msg, line, [user], {server, guild}) {
let member = msg.member ?? msg.author;
const guildObj = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
if (server || guild) {
if (!msg.guildID) {
return "`--server/--guild` can only be used within guilds.";
} else {
const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
const url = CDNEndpoints.GUILD_ICON(guild.id, guild.icon);
const url = CDNEndpoints.GUILD_ICON(guildObj.id, guildObj.icon);
const res = await fetch(url, {method: "HEAD"});
const mod = res.headers.get("last-modified");
@ -55,10 +55,10 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
const defaultAvatar = getDefaultAvatar(member.id, member.user?.discriminator ?? member.discriminator ?? 0);
const normalAvatar = member.user ? member.user.avatar ?? defaultAvatar : member.avatar ?? defaultAvatar;
const guildAvatar = guild ? member.avatar : null;
const guildAvatar = guildObj ? member.avatar : null;
const normalUrl = CDNEndpoints.USER_AVATAR(member.id, normalAvatar);
const guildUrl = CDNEndpoints.GUILD_MEMBER_AVATAR(guild.id, member.id, guildAvatar);
const guildUrl = CDNEndpoints.GUILD_MEMBER_AVATAR(guildObj.id, member.id, guildAvatar);
const res = await fetch(normalUrl, {method: "HEAD"});
const mod = res.headers.get("last-modified");
@ -68,17 +68,18 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
modStr = ` \u2022 Updated <t:${modDate}:R>`;
}
const guildRes = await fetch(guildUrl, {method: "HEAD"});
const guildMod = guildRes.headers.get("last-modified");
let modStrGuild = "";
if (guildMod) {
const modDate = Math.floor(new Date(guildMod).getTime() / 10000);
modStrGuild = ` \u2022 Updated <t:${modDate}:R>`;
if (guildAvatar) {
const guildRes = await fetch(guildUrl, {method: "HEAD"});
const guildMod = guildRes.headers.get("last-modified");
if (guildMod) {
const modDate = Math.floor(new Date(guildMod).getTime() / 10000);
modStrGuild = ` \u2022 Updated <t:${modDate}:R>`;
}
}
baseEmbed.description =
`[Normal avatar](${normalUrl})${modStr}` +
(guild && guildAvatar ? `\n[Guild avatar](${guildUrl})${modStrGuild}` : "");
`[Normal avatar](${normalUrl})${modStr}` + (guildAvatar ? `\n[Guild avatar](${guildUrl})${modStrGuild}` : "");
baseEmbed.url = normalUrl;
const guildEmbed = {...baseEmbed};

View file

@ -16,11 +16,10 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
if (!msg.guildID) {
return "`--server/--guild` can only be used within guilds.";
} else {
const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
const guildObj = msg.channel.guild ?? hf.bot.guilds.get(msg.guildID);
if (!guildObj.banner) return "This guild does not have a banner.";
if (!guild.banner) return "This guild does not have a banner.";
const url = CDNEndpoints.BANNER(guild.id, guild.banner);
const url = CDNEndpoints.BANNER(guildObj.id, guildObj.banner);
const res = await fetch(url, {method: "HEAD"});
const mod = res.headers.get("last-modified");
@ -72,12 +71,14 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
modStr = `Updated <t:${modDate}:R>`;
}
const guildRes = await fetch(guildUrl, {method: "HEAD"});
const guildMod = guildRes.headers.get("last-modified");
let modStrGuild;
if (guildMod) {
const modDate = Math.floor(new Date(guildMod).getTime() / 10000);
modStrGuild = `Updated <t:${modDate}:R>`;
if (guildUrl) {
const guildRes = await fetch(guildUrl, {method: "HEAD"});
const guildMod = guildRes.headers.get("last-modified");
if (guildMod) {
const modDate = Math.floor(new Date(guildMod).getTime() / 10000);
modStrGuild = `Updated <t:${modDate}:R>`;
}
}
return {