Finish preliminary rework, ready to test

This commit is contained in:
Dark 2021-06-02 04:07:50 +02:00
parent 57893e7f76
commit d0a1beb809
No known key found for this signature in database
GPG key ID: 2CD6ACB6B0A87B8A
177 changed files with 854 additions and 707 deletions

View file

@ -17,16 +17,17 @@ import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { getGuildPreview } from "./getGuildPreview";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { MessageEmbedOptions, CategoryChannel, TextChannel, VoiceChannel } from "discord.js";
export async function getServerInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
serverId: string,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
): Promise<MessageEmbedOptions | null> {
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)
? memoize(() => pluginData.client.guilds.fetch(serverId), `getRESTGuild_${serverId}`, 10 * MINUTES)
: null,
getGuildPreview(pluginData.client, serverId),
]);
@ -46,12 +47,12 @@ export async function getServerInfoEmbed(
embed.author = {
name: `Server: ${(guildPreview || restGuild)!.name}`,
icon_url: (guildPreview || restGuild)!.iconURL ?? undefined,
iconURL: (guildPreview || restGuild)!.iconURL() ?? undefined,
};
// BASIC INFORMATION
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAt = moment.utc((guildPreview || restGuild)!.createdAt, "x");
const createdAt = moment.utc((guildPreview || restGuild)!.id, "x"); // FIXME ID -> Timestamp
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
@ -86,7 +87,7 @@ export async function getServerInfoEmbed(
const bannerUrl = restGuild?.bannerURL ? `[Link](${restGuild.bannerURL})` : "None";
const splashUrl =
(restGuild || guildPreview)!.splashURL != null
? `[Link](${(restGuild || guildPreview)!.splashURL?.replace("size=128", "size=2048")})`
? `[Link](${(restGuild || guildPreview)!.splashURL()?.replace("size=128", "size=2048")})`
: "None";
embed.fields.push(
@ -113,33 +114,33 @@ export async function getServerInfoEmbed(
restGuild?.approximateMemberCount ||
restGuild?.memberCount ||
thisServer?.memberCount ||
thisServer?.members.size ||
thisServer?.members.cache.size ||
0;
let onlineMemberCount = (guildPreview?.approximatePresenceCount || restGuild?.approximatePresenceCount)!;
if (onlineMemberCount == null && restGuild?.vanityURL) {
if (onlineMemberCount == null && restGuild?.vanityURLCode) {
// For servers with a vanity URL, we can also use the numbers from the invite for online count
const invite = await resolveInvite(pluginData.client, restGuild.vanityURL!, true);
const invite = await resolveInvite(pluginData.client, restGuild.vanityURLCode!, true);
if (invite && inviteHasCounts(invite)) {
onlineMemberCount = invite.presenceCount;
}
}
if (!onlineMemberCount && thisServer) {
onlineMemberCount = thisServer.members.filter(m => m.status !== "offline").length; // Extremely inaccurate fallback
onlineMemberCount = thisServer.members.cache.filter(m => m.presence.status !== "offline").size; // Extremely inaccurate fallback
}
const offlineMemberCount = totalMembers - onlineMemberCount;
let memberCountTotalLines = `Total: **${formatNumber(totalMembers)}**`;
if (restGuild?.maxMembers) {
memberCountTotalLines += `\nMax: **${formatNumber(restGuild.maxMembers)}**`;
if (restGuild?.maximumMembers) {
memberCountTotalLines += `\nMax: **${formatNumber(restGuild.maximumMembers)}**`;
}
let memberCountOnlineLines = `Online: **${formatNumber(onlineMemberCount)}**`;
if (restGuild?.maxPresences) {
memberCountOnlineLines += `\nMax online: **${formatNumber(restGuild.maxPresences)}**`;
if (restGuild?.maximumPresences) {
memberCountOnlineLines += `\nMax online: **${formatNumber(restGuild.maximumPresences)}**`;
}
embed.fields.push({
@ -154,19 +155,19 @@ export async function getServerInfoEmbed(
// 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);
const totalChannels = thisServer.channels.cache.size;
const categories = thisServer.channels.cache.filter(channel => channel instanceof CategoryChannel);
const textChannels = thisServer.channels.cache.filter(channel => channel instanceof TextChannel);
const voiceChannels = thisServer.channels.cache.filter(channel => channel instanceof VoiceChannel);
embed.fields.push({
name: preEmbedPadding + "Channels",
inline: true,
value: trimLines(`
Total: **${totalChannels}** / 500
Categories: **${categories.length}**
Text: **${textChannels.length}**
Voice: **${voiceChannels.length}**
Categories: **${categories.size}**
Text: **${textChannels.size}**
Voice: **${voiceChannels.size}**
`),
});
}
@ -175,7 +176,7 @@ export async function getServerInfoEmbed(
const otherStats: string[] = [];
if (thisServer) {
otherStats.push(`Roles: **${thisServer.roles.size}** / 250`);
otherStats.push(`Roles: **${thisServer.roles.cache.size}** / 250`);
}
if (restGuild) {
@ -186,9 +187,9 @@ export async function getServerInfoEmbed(
2: 150,
3: 250,
}[restGuild.premiumTier] || 50;
otherStats.push(`Emojis: **${restGuild.emojis.length}** / ${maxEmojis * 2}`);
otherStats.push(`Emojis: **${restGuild.emojis.cache.size}** / ${maxEmojis * 2}`);
} else {
otherStats.push(`Emojis: **${guildPreview!.emojis.length}**`);
otherStats.push(`Emojis: **${guildPreview!.emojis.size}**`);
}
if (thisServer) {