utility: last-modified header checks for avatar and banner
This commit is contained in:
parent
da7874c53f
commit
3914429d21
2 changed files with 82 additions and 60 deletions
|
@ -10,16 +10,28 @@ avatar.category = "utility";
|
|||
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;
|
||||
|
||||
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 res = await fetch(url, {method: "HEAD"});
|
||||
const mod = res.headers.get("last-modified");
|
||||
let description;
|
||||
if (mod) {
|
||||
const modDate = Math.floor(new Date(mod).getTime() / 10000);
|
||||
description = `Updated <t:${modDate}:R>`;
|
||||
}
|
||||
|
||||
return {
|
||||
embeds: [
|
||||
{
|
||||
title: "Server Icon",
|
||||
description,
|
||||
url,
|
||||
image: {
|
||||
url,
|
||||
|
@ -33,66 +45,48 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
|
|||
if (lookup == "No results" || lookup == "Canceled" || lookup == "Request timed out") {
|
||||
return lookup;
|
||||
} else {
|
||||
let member = lookup;
|
||||
const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
|
||||
if (guild) {
|
||||
if (guild.members.has(lookup.id)) {
|
||||
member = guild.members.get(lookup.id);
|
||||
} else {
|
||||
const fetched = await guild.fetchMembers({
|
||||
userIDs: [lookup.id],
|
||||
});
|
||||
member = fetched[0];
|
||||
}
|
||||
}
|
||||
|
||||
const baseEmbed = {
|
||||
title: `Avatar for \`${formatUsername(member)}\``,
|
||||
};
|
||||
|
||||
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 normalUrl = CDNEndpoints.USER_AVATAR(member.id, normalAvatar);
|
||||
const guildUrl = CDNEndpoints.GUILD_MEMBER_AVATAR(guild.id, member.id, guildAvatar);
|
||||
|
||||
baseEmbed.description =
|
||||
`[Normal avatar](${normalUrl})` + (guild && guildAvatar ? `\n[Guild avatar](${guildUrl})` : "");
|
||||
baseEmbed.url = normalUrl;
|
||||
|
||||
const guildEmbed = {...baseEmbed};
|
||||
baseEmbed.image = {url: normalUrl};
|
||||
guildEmbed.image = {url: guildUrl};
|
||||
|
||||
return {
|
||||
embeds: [baseEmbed, guildAvatar && guildEmbed].filter((x) => x != null),
|
||||
};
|
||||
member = lookup;
|
||||
}
|
||||
} else {
|
||||
const guild = msg.channel.guild ?? hf.bot.guilds.get(msg.guildID);
|
||||
|
||||
const baseEmbed = {
|
||||
title: `Avatar for \`${formatUsername(msg.author)}\``,
|
||||
};
|
||||
|
||||
const normalAvatar = msg.author?.avatar ?? getDefaultAvatar(msg.author.id, msg.author.discriminator);
|
||||
const guildAvatar = msg.member?.avatar;
|
||||
|
||||
const normalUrl = CDNEndpoints.USER_AVATAR(msg.author.id, normalAvatar);
|
||||
const guildUrl = CDNEndpoints.GUILD_MEMBER_AVATAR(guild.id, msg.author.id, guildAvatar);
|
||||
|
||||
baseEmbed.description =
|
||||
`[Normal avatar](${normalUrl})` + (guild && guildAvatar ? `\n[Guild avatar](${guildUrl})` : "");
|
||||
baseEmbed.url = normalUrl;
|
||||
|
||||
const guildEmbed = {...baseEmbed};
|
||||
baseEmbed.image = {url: normalUrl};
|
||||
guildEmbed.image = {url: guildUrl};
|
||||
|
||||
return {
|
||||
embeds: [baseEmbed, guildAvatar && guildEmbed].filter((x) => x != null),
|
||||
};
|
||||
}
|
||||
|
||||
const baseEmbed = {
|
||||
title: `Avatar for \`${formatUsername(member)}\``,
|
||||
};
|
||||
|
||||
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 normalUrl = CDNEndpoints.USER_AVATAR(member.id, normalAvatar);
|
||||
const guildUrl = CDNEndpoints.GUILD_MEMBER_AVATAR(guild.id, member.id, guildAvatar);
|
||||
|
||||
const res = await fetch(normalUrl, {method: "HEAD"});
|
||||
const mod = res.headers.get("last-modified");
|
||||
let modStr = "";
|
||||
if (mod) {
|
||||
const modDate = Math.floor(new Date(mod).getTime() / 10000);
|
||||
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>`;
|
||||
}
|
||||
|
||||
baseEmbed.description =
|
||||
`[Normal avatar](${normalUrl})${modStr}` +
|
||||
(guild && guildAvatar ? `\n[Guild avatar](${guildUrl})${modStrGuild}` : "");
|
||||
baseEmbed.url = normalUrl;
|
||||
|
||||
const guildEmbed = {...baseEmbed};
|
||||
baseEmbed.image = {url: normalUrl};
|
||||
guildEmbed.image = {url: guildUrl};
|
||||
|
||||
return {
|
||||
embeds: [baseEmbed, guildAvatar && guildEmbed].filter((x) => x != null),
|
||||
};
|
||||
};
|
||||
hf.registerCommand(avatar);
|
||||
|
|
|
@ -21,10 +21,20 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
|
|||
if (!guild.banner) return "This guild does not have a banner.";
|
||||
|
||||
const url = CDNEndpoints.BANNER(guild.id, guild.banner);
|
||||
|
||||
const res = await fetch(url, {method: "HEAD"});
|
||||
const mod = res.headers.get("last-modified");
|
||||
let description;
|
||||
if (mod) {
|
||||
const modDate = Math.floor(new Date(mod).getTime() / 10000);
|
||||
description = `Updated <t:${modDate}:R>`;
|
||||
}
|
||||
|
||||
return {
|
||||
embeds: [
|
||||
{
|
||||
title: "Server Banner",
|
||||
description,
|
||||
url,
|
||||
image: {
|
||||
url,
|
||||
|
@ -52,12 +62,29 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
|
|||
if (!userObj.banner && !memberObj?.banner) return "This user does not have a banner.";
|
||||
|
||||
const url = userObj.banner && CDNEndpoints.BANNER(userObj.id, userObj.banner);
|
||||
|
||||
const guildUrl = memberObj?.banner && CDNEndpoints.GUILD_MEMBER_BANNER(msg.guildID, userObj.id, memberObj.banner);
|
||||
|
||||
const res = await fetch(url, {method: "HEAD"});
|
||||
const mod = res.headers.get("last-modified");
|
||||
let modStr;
|
||||
if (mod) {
|
||||
const modDate = Math.floor(new Date(mod).getTime() / 10000);
|
||||
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>`;
|
||||
}
|
||||
|
||||
return {
|
||||
embeds: [
|
||||
url && {
|
||||
title: `Banner for \`${formatUsername(userObj)}\``,
|
||||
description: modStr,
|
||||
url,
|
||||
image: {
|
||||
url,
|
||||
|
@ -65,6 +92,7 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
|
|||
},
|
||||
guildUrl && {
|
||||
title: `Server banner for \`${formatUsername(userObj)}\``,
|
||||
description: modStrGuild,
|
||||
url: guildUrl,
|
||||
image: {
|
||||
url: guildUrl,
|
||||
|
|
Loading…
Reference in a new issue