diff --git a/src/modules/utility/avatar.js b/src/modules/utility/avatar.js index 9e94847..7c3e2b8 100644 --- a/src/modules/utility/avatar.js +++ b/src/modules/utility/avatar.js @@ -10,16 +10,28 @@ avatar.category = "utility"; avatar.helpText = "Get avatar of a user"; avatar.usage = ""; 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 `; + } + 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 `; + } + + 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 `; + } + + 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); diff --git a/src/modules/utility/banner.js b/src/modules/utility/banner.js index 993fc63..29d983c 100644 --- a/src/modules/utility/banner.js +++ b/src/modules/utility/banner.js @@ -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 `; + } + 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 `; + } + + 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 `; + } + 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,