utility.guildinfo: implement preview, discovery and widget

This commit is contained in:
Cynthia Foxwell 2023-10-20 13:30:57 -06:00
parent 3abaf21585
commit 6c08af5aa9

View file

@ -473,7 +473,7 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
} else {
const guild = msg.channel.guild || hf.bot.guilds.get(msg.guildID);
const url = `${ICON_BASE}${guild.id}/${guild.icon}.${
guild.icon.startsWith("a_") ? "gif?size=1024&_=.gif" : "png?size=1024"
guild.icon.startsWith("a_") ? "gif?size=4096&_=.gif" : "png?size=4096"
}`;
return {
embeds: [
@ -523,8 +523,8 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
normalAvatar +
"." +
(normalAvatar.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024");
? "gif?size=4096&_=.gif"
: "png?size=4096");
const guildUrl =
guildAvatar &&
"https://cdn.discordapp.com/guilds/" +
@ -535,8 +535,8 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
guildAvatar +
"." +
(guildAvatar.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024");
? "gif?size=4096&_=.gif"
: "png?size=4096");
baseEmbed.description =
`[Normal avatar](${normalUrl})` +
@ -568,8 +568,8 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
normalAvatar +
"." +
(normalAvatar.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024");
? "gif?size=4096&_=.gif"
: "png?size=4096");
const guildUrl =
guildAvatar &&
"https://cdn.discordapp.com/guilds/" +
@ -580,8 +580,8 @@ avatar.callback = async function (msg, line, [user], {server, guild}) {
guildAvatar +
"." +
(guildAvatar.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024");
? "gif?size=4096&_=.gif"
: "png?size=4096");
baseEmbed.description =
`[Normal avatar](${normalUrl})` +
@ -651,7 +651,7 @@ banner.callback = async function (msg, line, [user], {server, guild}) {
if (!userObj.banner) return "This user does not have a banner.";
const url = `${BANNER_BASE}${userObj.id}/${userObj.banner}.${
userObj.banner.startsWith("a_") ? "gif?size=1024&_=.gif" : "png?size=1024"
userObj.banner.startsWith("a_") ? "gif?size=4096&_=.gif" : "png?size=4096"
}`;
return {
embeds: [
@ -807,8 +807,8 @@ lookupinvite.callback = async function (msg, line) {
invite.guild.icon &&
`${ICON_BASE}${invite.guild.id}/${invite.guild.icon}.${
invite.guild.icon.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024"
? "gif?size=4096&_=.gif"
: "png?size=4096"
}`,
};
@ -822,15 +822,15 @@ lookupinvite.callback = async function (msg, line) {
invite.guild.icon
? `[Icon](${ICON_BASE}${invite.guild.id}/${invite.guild.icon}.${
invite.guild.icon.startsWith("a_")
? "gif?size=1024"
: "png?size=1024"
? "gif?size=4096"
: "png?size=4096"
})`
: ""
}${
invite.guild.splash
? `${invite.guild.icon ? " | " : ""}[Splash](${SPLASH_BASE}${
invite.guild.id
}/${invite.guild.splash}.png?size=2048)`
}/${invite.guild.splash}.png?size=4096)`
: ""
}${
invite.guild.banner
@ -840,8 +840,8 @@ lookupinvite.callback = async function (msg, line) {
invite.guild.banner
}.${
invite.guild.banner.startsWith("a_")
? "gif?size=1024"
: "png?size=1024"
? "gif?size=4096"
: "png?size=4096"
})`
: ""
}`,
@ -888,8 +888,8 @@ lookupinvite.callback = async function (msg, line) {
const groupIcon = invite.channel.icon
? `${CHANNEL_ICON_BASE}${invite.channel.id}/${invite.channel.icon}.${
invite.channel.icon.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024"
? "gif?size=4096&_=.gif"
: "png?size=4096"
}`
: "https://discord.com" +
DEFAULT_GROUP_DM_AVATARS[
@ -914,8 +914,8 @@ lookupinvite.callback = async function (msg, line) {
invite.inviter?.avatar &&
`${AVATAR_BASE}${invite.inviter.id}/${invite.inviter.avatar}.${
invite.inviter.avatar.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024"
? "gif?size=4096&_=.gif"
: "png?size=4096"
}`;
if (avatarURL) {
@ -1479,7 +1479,7 @@ appinfo.callback = async function (msg, line) {
if (app.icon) {
embed.thumbnail = {
url: `${APP_ICON_BASE}${app.id}/${app.icon}.png?size=1024`,
url: `${APP_ICON_BASE}${app.id}/${app.icon}.png?size=4096`,
};
}
@ -1871,6 +1871,26 @@ guildinfo.callback = async function (msg, line) {
: "Only Mentions",
inline: true,
},
guild.rulesChannelID && {
name: "Rules",
value: `<#${guild.rulesChannelID}>`,
inline: true,
},
guild.systemChannelID && {
name: "System Messages",
value: `<#${guild.systemChannelID}>`,
inline: true,
},
guild.publicUpdatesChannelID && {
name: "Community Updates",
value: `<#${guild.publicUpdatesChannelID}>`,
inline: true,
},
guild.safetyAlertsChannelID && {
name: "Safety Alerts",
value: `<#${guild.safetyAlertsChannelID}>`,
inline: true,
},
{
name: `Channels (${guild.channels.size})`,
value:
@ -1922,49 +1942,12 @@ guildinfo.callback = async function (msg, line) {
embed.thumbnail = {
url: `${ICON_BASE}${guild.id}/${guild.icon}.${
guild.icon.startsWith("a_")
? "gif?size=1024&_=.gif"
: "png?size=1024"
? "gif?size=4096&_=.gif"
: "png?size=4096"
}`,
};
}
if (guild.systemChannelID) {
embed.fields.push({
name: "System Messages",
value: `#${guild.channels.get(guild.systemChannelID).name} (\`${
guild.systemChannelID
}\`)`,
inline: true,
});
}
if (guild.rulesChannelID) {
embed.fields.push({
name: "Rules",
value: `#${guild.channels.get(guild.rulesChannelID).name} (\`${
guild.rulesChannelID
}\`)`,
inline: true,
});
}
if (guild.publicUpdatesChannelID) {
embed.fields.push({
name: "Community Updates",
value: `#${
guild.channels.get(guild.publicUpdatesChannelID).name
} (\`${guild.publicUpdatesChannelID}\`)`,
inline: true,
});
}
if (guild.safetyAlertsChannelID) {
embed.fields.push({
name: "Safety Alerts",
value: `#${guild.channels.get(guild.safetyAlertsChannelID).name} (\`${
guild.safetyAlertsChannelID
}\`)`,
inline: true,
});
}
const members = Array.from(guild.members.values());
const online = members.filter(
(member) => member.status != null && member.status != "offline"
@ -2038,13 +2021,330 @@ guildinfo.callback = async function (msg, line) {
return {embed};
}
case "preview": {
return "TODO: guild from preview";
const embed = {
title: guild.name,
description: guild.description ?? "*No description.*",
fields: [
{
name: "Created",
value: `<t:${Math.floor(snowflakeToTimestamp(guild.id) / 1000)}:R>`,
inline: true,
},
{
name: `Emotes (${guild.emojis.length})`,
value: `${
guild.emojis.filter((e) => e.animated).length
} animated, ${
guild.emojis.filter((e) => e.managed).length
} managed\n${
guild.emojis.filter((e) => !e.available).length
} unavailable`,
inline: true,
},
{
name: `Stickers (${guild.stickers.length})`,
value: `${
guild.stickers.filter((s) => s.format_type == 1).length
} PNG, ${
guild.stickers.filter((s) => s.format_type == 2).length
} APNG, ${
guild.stickers.filter((s) => s.format_type == 4).length
} GIF, ${
guild.stickers.filter((s) => s.format_type == 3).length
} Lottie\n${
guild.stickers.filter((s) => !s.available).length
} unavailable`,
inline: true,
},
],
footer: {
text: "Fetched from guild preview",
},
};
if (guild.icon) {
embed.thumbnail = {
url: `${ICON_BASE}${guild.id}/${guild.icon}.${
guild.icon.startsWith("a_")
? "gif?size=4096&_=.gif"
: "png?size=4096"
}`,
};
}
embed.fields.push({
name: "Member Count",
value: `<:online:1152111668856361010>${guild.approximate_presence_count} online\t\t<:offline:1152111682886316042>${guild.approximate_member_count} members`,
inline: false,
});
const features = guild.features.sort().map(
(feature) =>
(GUILD_FEATURES[feature]?.icon ?? "\u2753") +
" " +
(GUILD_FEATURES[feature]?.name ??
feature
.split("_")
.map((x) => x[0] + x.substring(1).toLowerCase())
.join(" "))
);
embed.fields.push({
name: `Features (${features.length})`,
value:
features.length > 0
? features.slice(0, Math.ceil(features.length / 2)).join("\n")
: "None",
inline: true,
});
if (features.length > 1)
embed.fields.push({
name: "\u200b",
value: features
.slice(Math.ceil(features.length / 2), features.length)
.join("\n"),
inline: true,
});
const images = [];
if (guild.icon) {
images.push(`[Icon](${embed.thumbnail.url})`);
}
if (guild.splash) {
images.push(
`[Invite Splash](${SPLASH_BASE}${guild.id}/${guild.splash}.png?size=4096)`
);
}
if (guild.discovery_splash) {
images.push(
`[Discovery Splash](${DISCOVERY_SPLASH_BASE}${guild.id}/${guild.discoverySplash}.png?size=4096)`
);
}
if (images.length > 0) {
embed.fields.push({
name: "\u200b",
value: images.join(" | "),
inline: false,
});
}
return {embed};
}
case "discovery": {
return "TODO: guild from discovery";
if (!guild.store_page) {
return "Got discovery data but no store page.";
}
const guildObj = guild.store_page.guild;
let invite;
if (
guildObj.invite?.code ||
guild.store_page.role_subscription.purchase_page_invite?.code
) {
const code =
guildObj.invite?.code ??
guild.store_page.role_subscription.purchase_page_invite?.code;
invite = await hf.bot.requestHandler.request(
"GET",
`/invites/${code}?with_counts=true&with_expiration=true`
);
}
const embed = {
title: guildObj.name,
description: invite?.guild?.description ?? "*No description.*",
fields: [
{
name: "Created",
value: `<t:${Math.floor(
snowflakeToTimestamp(guildObj.id) / 1000
)}:R>`,
inline: true,
},
],
footer: {
text: "Fetched from discovery" + (invite ? " + invite" : ""),
},
};
if (guildObj.icon_hash) {
embed.thumbnail = {
url: `${ICON_BASE}${guildObj.id}/${guildObj.icon_hash}.${
guildObj.icon_hash.startsWith("a_")
? "gif?size=4096&_=.gif"
: "png?size=4096"
}`,
};
}
const invites = [];
if (guildObj.invite?.code) invites.push(guildObj.invite.code);
if (guild.store_page.role_subscription.purchase_page_invite?.code)
invites.push(
guild.store_page.role_subscription.purchase_page_invite.code
);
if (invites.length > 0) {
embed.fields.push({
name: "Invites",
value: invites.map((code) => "https://discord.gg/" + code).join("\n"),
inline: true,
});
}
embed.fields.push({
name: "Member Count",
value: `<:online:1152111668856361010>${guildObj.approximate_presence_count} online\t\t<:offline:1152111682886316042>${guildObj.approximate_member_count} members`,
inline: false,
});
if (invite?.guild?.features) {
const features = invite.guild.features.sort().map(
(feature) =>
(GUILD_FEATURES[feature]?.icon ?? "\u2753") +
" " +
(GUILD_FEATURES[feature]?.name ??
feature
.split("_")
.map((x) => x[0] + x.substring(1).toLowerCase())
.join(" "))
);
embed.fields.push({
name: `Features (${features.length})`,
value:
features.length > 0
? features.slice(0, Math.ceil(features.length / 2)).join("\n")
: "None",
inline: true,
});
if (features.length > 1)
embed.fields.push({
name: "\u200b",
value: features
.slice(Math.ceil(features.length / 2), features.length)
.join("\n"),
inline: true,
});
}
const images = [];
if (guildObj.icon_hash) {
images.push(`[Icon](${embed.thumbnail.url})`);
}
if (invite?.guild?.splash) {
images.push(
`[Invite Splash](${SPLASH_BASE}${guild.id}/${guild.splash}.png?size=4096)`
);
}
if (invite?.guild?.banner) {
images.push(
`[Banner](${BANNER_BASE}${guild.id}/${guild.banner}.png?size=4096)`
);
}
if (images.length > 0) {
embed.fields.push({
name: "\u200b",
value: images.join(" | "),
inline: false,
});
}
return {embed};
}
case "widget": {
return "TODO: guild from widget";
let invite;
if (guild.instant_invite) {
invite = await hf.bot.requestHandler.request(
"GET",
`/invites/${guild.instant_invite}?with_counts=true&with_expiration=true`
);
}
const embed = {
title: guild.name,
description: invite?.guild?.description ?? "*No description.*",
fields: [
{
name: "Created",
value: `<t:${Math.floor(snowflakeToTimestamp(guild.id) / 1000)}:R>`,
inline: true,
},
],
footer: {
text: "Fetched from widget" + (invite ? " + invite" : ""),
},
};
if (invite) {
embed.fields.push({
name: "Member Count",
value: `<:online:1152111668856361010>${invite.approximate_presence_count} online\t\t<:offline:1152111682886316042>${invite.approximate_member_count} members`,
inline: false,
});
const features = invite.guild.features.sort().map(
(feature) =>
(GUILD_FEATURES[feature]?.icon ?? "\u2753") +
" " +
(GUILD_FEATURES[feature]?.name ??
feature
.split("_")
.map((x) => x[0] + x.substring(1).toLowerCase())
.join(" "))
);
embed.fields.push({
name: `Features (${features.length})`,
value:
features.length > 0
? features.slice(0, Math.ceil(features.length / 2)).join("\n")
: "None",
inline: true,
});
if (features.length > 1)
embed.fields.push({
name: "\u200b",
value: features
.slice(Math.ceil(features.length / 2), features.length)
.join("\n"),
inline: true,
});
const images = [];
if (invite.guild.icon) {
images.push(`[Icon](${embed.thumbnail.url})`);
}
if (invite.guild.splash) {
images.push(
`[Invite Splash](${SPLASH_BASE}${guild.id}/${guild.splash}.png?size=4096)`
);
}
if (invite.guild.banner) {
images.push(
`[Banner](${BANNER_BASE}${guild.id}/${guild.banner}.png?size=4096)`
);
}
if (images.length > 0) {
embed.fields.push({
name: "\u200b",
value: images.join(" | "),
inline: false,
});
}
} else {
embed.fields.push({
name: "Member Count",
value: `<:online:1152111668856361010>${guild.presence_count} online`,
inline: false,
});
}
return embed;
}
default:
return "Guild not found.";