2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-08-05 04:38:10 +03:00
|
|
|
import { UtilityPluginType } from "../types";
|
2020-11-09 20:03:57 +02:00
|
|
|
import {
|
|
|
|
embedPadding,
|
|
|
|
EmbedWith,
|
|
|
|
formatNumber,
|
2021-04-02 19:02:20 +03:00
|
|
|
inviteHasCounts,
|
2020-11-09 20:03:57 +02:00
|
|
|
memoize,
|
|
|
|
MINUTES,
|
|
|
|
preEmbedPadding,
|
|
|
|
resolveInvite,
|
|
|
|
resolveUser,
|
|
|
|
trimLines,
|
|
|
|
} from "../../../utils";
|
2021-06-01 02:05:55 +02:00
|
|
|
|
2020-08-05 04:38:10 +03:00
|
|
|
import moment from "moment-timezone";
|
|
|
|
import humanizeDuration from "humanize-duration";
|
2020-08-06 01:10:40 +03:00
|
|
|
import { getGuildPreview } from "./getGuildPreview";
|
2020-08-19 00:19:12 +03:00
|
|
|
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
2020-08-05 04:38:10 +03:00
|
|
|
|
2020-08-05 17:40:20 +03:00
|
|
|
export async function getServerInfoEmbed(
|
2020-10-01 01:43:38 +03:00
|
|
|
pluginData: GuildPluginData<UtilityPluginType>,
|
2020-08-05 17:40:20 +03:00
|
|
|
serverId: string,
|
2020-08-19 00:19:12 +03:00
|
|
|
requestMemberId?: string,
|
2020-11-09 20:03:57 +02:00
|
|
|
): Promise<EmbedOptions | null> {
|
2020-08-05 04:38:10 +03:00
|
|
|
const thisServer = serverId === pluginData.guild.id ? pluginData.guild : null;
|
|
|
|
const [restGuild, guildPreview] = await Promise.all([
|
|
|
|
thisServer
|
|
|
|
? memoize(() => pluginData.client.getRESTGuild(serverId), `getRESTGuild_${serverId}`, 10 * MINUTES)
|
|
|
|
: null,
|
2020-08-06 01:10:40 +03:00
|
|
|
getGuildPreview(pluginData.client, serverId),
|
2020-08-05 04:38:10 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
if (!restGuild && !guildPreview) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const features = (restGuild || guildPreview)!.features;
|
2020-08-05 04:59:57 +03:00
|
|
|
if (!thisServer && !features.includes("DISCOVERABLE")) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const embed: EmbedWith<"fields"> = {
|
2020-08-05 04:38:10 +03:00
|
|
|
fields: [],
|
|
|
|
};
|
|
|
|
|
2020-08-05 22:01:29 +03:00
|
|
|
embed.author = {
|
2020-11-09 20:03:57 +02:00
|
|
|
name: `Server: ${(guildPreview || restGuild)!.name}`,
|
|
|
|
icon_url: (guildPreview || restGuild)!.iconURL ?? undefined,
|
2020-08-05 22:01:29 +03:00
|
|
|
};
|
2020-08-05 04:38:10 +03:00
|
|
|
|
|
|
|
// BASIC INFORMATION
|
2020-08-19 00:19:12 +03:00
|
|
|
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
|
2020-11-09 20:03:57 +02:00
|
|
|
const createdAt = moment.utc((guildPreview || restGuild)!.createdAt, "x");
|
2020-08-19 00:19:12 +03:00
|
|
|
const tzCreatedAt = requestMemberId
|
|
|
|
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
|
|
|
|
: timeAndDate.inGuildTz(createdAt);
|
|
|
|
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
|
2020-08-10 00:24:06 +03:00
|
|
|
const serverAge = humanizeDuration(moment.utc().valueOf() - createdAt.valueOf(), {
|
2020-08-05 04:38:10 +03:00
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const basicInformation: string[] = [];
|
2020-08-19 00:47:31 +03:00
|
|
|
basicInformation.push(`Created: **${serverAge} ago** (\`${prettyCreatedAt}\`)`);
|
2020-08-05 04:38:10 +03:00
|
|
|
|
|
|
|
if (thisServer) {
|
|
|
|
const owner = await resolveUser(pluginData.client, thisServer.ownerID);
|
|
|
|
const ownerName = `${owner.username}#${owner.discriminator}`;
|
|
|
|
|
2020-08-19 00:47:31 +03:00
|
|
|
basicInformation.push(`Owner: **${ownerName}** (\`${thisServer.ownerID}\`)`);
|
2020-08-05 04:38:10 +03:00
|
|
|
basicInformation.push(`Voice region: **${thisServer.region}**`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (features.length > 0) {
|
|
|
|
basicInformation.push(`Features: ${features.join(", ")}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
embed.fields.push({
|
2020-08-05 22:01:29 +03:00
|
|
|
name: preEmbedPadding + "Basic information",
|
|
|
|
value: basicInformation.join("\n"),
|
2020-08-05 04:38:10 +03:00
|
|
|
});
|
|
|
|
|
2021-02-13 19:04:59 +01:00
|
|
|
// IMAGE LINKS
|
2021-04-02 20:15:13 +03:00
|
|
|
const iconUrl = `[Link](${(restGuild || guildPreview)!.iconURL})`;
|
2021-04-02 20:17:29 +03:00
|
|
|
const bannerUrl = restGuild?.bannerURL ? `[Link](${restGuild.bannerURL})` : "None";
|
2021-02-13 19:04:59 +01:00
|
|
|
const splashUrl =
|
|
|
|
(restGuild || guildPreview)!.splashURL != null
|
2021-04-02 20:15:13 +03:00
|
|
|
? `[Link](${(restGuild || guildPreview)!.splashURL?.replace("size=128", "size=2048")})`
|
2021-02-13 19:04:59 +01:00
|
|
|
: "None";
|
|
|
|
|
|
|
|
embed.fields.push(
|
|
|
|
{
|
|
|
|
name: "Server icon",
|
|
|
|
value: iconUrl,
|
|
|
|
inline: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invite splash",
|
|
|
|
value: splashUrl,
|
|
|
|
inline: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Server banner",
|
|
|
|
value: bannerUrl,
|
|
|
|
inline: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-08-05 04:38:10 +03:00
|
|
|
// MEMBER COUNTS
|
|
|
|
const totalMembers =
|
|
|
|
guildPreview?.approximateMemberCount ||
|
|
|
|
restGuild?.approximateMemberCount ||
|
|
|
|
restGuild?.memberCount ||
|
|
|
|
thisServer?.memberCount ||
|
|
|
|
thisServer?.members.size ||
|
|
|
|
0;
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
let onlineMemberCount = (guildPreview?.approximatePresenceCount || restGuild?.approximatePresenceCount)!;
|
2020-08-05 04:38:10 +03:00
|
|
|
|
|
|
|
if (onlineMemberCount == null && restGuild?.vanityURL) {
|
|
|
|
// For servers with a vanity URL, we can also use the numbers from the invite for online count
|
2020-11-09 20:03:57 +02:00
|
|
|
const invite = await resolveInvite(pluginData.client, restGuild.vanityURL!, true);
|
2021-04-02 19:02:20 +03:00
|
|
|
if (invite && inviteHasCounts(invite)) {
|
2020-08-05 04:38:10 +03:00
|
|
|
onlineMemberCount = invite.presenceCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!onlineMemberCount && thisServer) {
|
|
|
|
onlineMemberCount = thisServer.members.filter(m => m.status !== "offline").length; // Extremely inaccurate fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
const offlineMemberCount = totalMembers - onlineMemberCount;
|
|
|
|
|
|
|
|
let memberCountTotalLines = `Total: **${formatNumber(totalMembers)}**`;
|
|
|
|
if (restGuild?.maxMembers) {
|
|
|
|
memberCountTotalLines += `\nMax: **${formatNumber(restGuild.maxMembers)}**`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let memberCountOnlineLines = `Online: **${formatNumber(onlineMemberCount)}**`;
|
|
|
|
if (restGuild?.maxPresences) {
|
|
|
|
memberCountOnlineLines += `\nMax online: **${formatNumber(restGuild.maxPresences)}**`;
|
|
|
|
}
|
|
|
|
|
|
|
|
embed.fields.push({
|
2020-08-05 22:01:29 +03:00
|
|
|
name: preEmbedPadding + "Members",
|
2020-08-05 04:38:10 +03:00
|
|
|
inline: true,
|
|
|
|
value: trimLines(`
|
|
|
|
${memberCountTotalLines}
|
|
|
|
${memberCountOnlineLines}
|
|
|
|
Offline: **${formatNumber(offlineMemberCount)}**
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
|
|
|
|
// CHANNEL COUNTS
|
|
|
|
if (thisServer) {
|
|
|
|
const totalChannels = thisServer.channels.size;
|
|
|
|
const categories = thisServer.channels.filter(channel => channel instanceof CategoryChannel);
|
|
|
|
const textChannels = thisServer.channels.filter(channel => channel instanceof TextChannel);
|
|
|
|
const voiceChannels = thisServer.channels.filter(channel => channel instanceof VoiceChannel);
|
|
|
|
|
|
|
|
embed.fields.push({
|
2020-08-05 22:01:29 +03:00
|
|
|
name: preEmbedPadding + "Channels",
|
2020-08-05 04:38:10 +03:00
|
|
|
inline: true,
|
2020-08-05 22:01:29 +03:00
|
|
|
value: trimLines(`
|
2020-08-05 04:38:10 +03:00
|
|
|
Total: **${totalChannels}** / 500
|
|
|
|
Categories: **${categories.length}**
|
|
|
|
Text: **${textChannels.length}**
|
|
|
|
Voice: **${voiceChannels.length}**
|
2020-08-05 22:01:29 +03:00
|
|
|
`),
|
2020-08-05 04:38:10 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// OTHER STATS
|
2020-11-09 20:03:57 +02:00
|
|
|
const otherStats: string[] = [];
|
2020-08-05 04:38:10 +03:00
|
|
|
|
|
|
|
if (thisServer) {
|
|
|
|
otherStats.push(`Roles: **${thisServer.roles.size}** / 250`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restGuild) {
|
|
|
|
const maxEmojis =
|
|
|
|
{
|
|
|
|
0: 50,
|
|
|
|
1: 100,
|
|
|
|
2: 150,
|
|
|
|
3: 250,
|
|
|
|
}[restGuild.premiumTier] || 50;
|
2020-08-05 05:00:45 +03:00
|
|
|
otherStats.push(`Emojis: **${restGuild.emojis.length}** / ${maxEmojis * 2}`);
|
2020-08-05 04:38:10 +03:00
|
|
|
} else {
|
2020-11-09 20:03:57 +02:00
|
|
|
otherStats.push(`Emojis: **${guildPreview!.emojis.length}**`);
|
2020-08-05 04:38:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (thisServer) {
|
|
|
|
otherStats.push(`Boosts: **${thisServer.premiumSubscriptionCount ?? 0}** (level ${thisServer.premiumTier})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
embed.fields.push({
|
2020-08-05 22:01:29 +03:00
|
|
|
name: preEmbedPadding + "Other stats",
|
2020-08-05 04:38:10 +03:00
|
|
|
inline: true,
|
2020-08-05 22:01:29 +03:00
|
|
|
value: otherStats.join("\n"),
|
2020-08-05 04:38:10 +03:00
|
|
|
});
|
|
|
|
|
2020-08-05 04:59:57 +03:00
|
|
|
if (!thisServer) {
|
|
|
|
embed.footer = {
|
|
|
|
text: "⚠️ Only showing publicly available information for this server",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-05 04:38:10 +03:00
|
|
|
return embed;
|
|
|
|
}
|