From 0053f55576f2fbc5d56247329d108b14d2356f85 Mon Sep 17 00:00:00 2001 From: Dark <7890309+DarkView@users.noreply.github.com> Date: Thu, 29 Jul 2021 18:46:52 +0200 Subject: [PATCH] Fix role member counts, add threads to channel info command --- .../src/plugins/Utility/commands/RolesCmd.ts | 10 ++--- .../Utility/functions/getChannelInfoEmbed.ts | 45 ++++++++++++++----- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/backend/src/plugins/Utility/commands/RolesCmd.ts b/backend/src/plugins/Utility/commands/RolesCmd.ts index 8aa7e85a..2aa3f459 100644 --- a/backend/src/plugins/Utility/commands/RolesCmd.ts +++ b/backend/src/plugins/Utility/commands/RolesCmd.ts @@ -34,13 +34,9 @@ export const RolesCmd = utilityCmd({ if (args.counts) { await refreshMembersIfNeeded(guild); - // If the user requested role member counts as well, calculate them and sort the roles by their member count - const roleCounts: Map = Array.from(guild.members.cache.values()).reduce((map, member) => { - for (const roleId of member.roles.cache) { - if (!map.has(roleId)) map.set(roleId, 0); - map.set(roleId, map.get(roleId) + 1); - } - + // If the user requested role member counts as well, fetch them and sort the roles by their member count + const roleCounts: Map = Array.from(guild.roles.cache.values()).reduce((map, role) => { + map.set(role.id, role.members.size); return map; }, new Map()); diff --git a/backend/src/plugins/Utility/functions/getChannelInfoEmbed.ts b/backend/src/plugins/Utility/functions/getChannelInfoEmbed.ts index fa2ea87d..c1dcc7d8 100644 --- a/backend/src/plugins/Utility/functions/getChannelInfoEmbed.ts +++ b/backend/src/plugins/Utility/functions/getChannelInfoEmbed.ts @@ -1,9 +1,9 @@ -import { MessageEmbedOptions, Snowflake, StageChannel, VoiceChannel } from "discord.js"; +import { MessageEmbedOptions, Snowflake, StageChannel, ThreadChannel, VoiceChannel } from "discord.js"; import humanizeDuration from "humanize-duration"; import { GuildPluginData } from "knub"; import moment from "moment-timezone"; import { ChannelTypeStrings } from "src/types"; -import { EmbedWith, formatNumber, preEmbedPadding, trimLines } from "../../../utils"; +import { EmbedWith, formatNumber, MINUTES, preEmbedPadding, trimLines, verboseUserMention } from "../../../utils"; import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin"; import { UtilityPluginType } from "../types"; @@ -15,6 +15,10 @@ const ANNOUNCEMENT_CHANNEL_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/740656841687564348/announcement-channel.png"; const STAGE_CHANNEL_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/839930647711186995/stage-channel.png"; +const PUBLIC_THREAD_ICON = + "https://cdn.discordapp.com/attachments/740650744830623756/870343055855738921/public-thread.png"; +const PRIVATE_THREAD_UCON = + "https://cdn.discordapp.com/attachments/740650744830623756/870343402447839242/private-thread.png"; export async function getChannelInfoEmbed( pluginData: GuildPluginData, @@ -30,14 +34,14 @@ export async function getChannelInfoEmbed( fields: [], }; - let icon = TEXT_CHANNEL_ICON; - if (channel.type === ChannelTypeStrings.VOICE) { - icon = VOICE_CHANNEL_ICON; - } else if (channel.type === ChannelTypeStrings.NEWS) { - icon = ANNOUNCEMENT_CHANNEL_ICON; - } else if (channel.type === ChannelTypeStrings.STAGE) { - icon = STAGE_CHANNEL_ICON; - } + const icon = + { + [ChannelTypeStrings.VOICE]: VOICE_CHANNEL_ICON, + [ChannelTypeStrings.NEWS]: ANNOUNCEMENT_CHANNEL_ICON, + [ChannelTypeStrings.STAGE]: STAGE_CHANNEL_ICON, + [ChannelTypeStrings.PUBLIC_THREAD]: PUBLIC_THREAD_ICON, + [ChannelTypeStrings.PRIVATE_THREAD]: PRIVATE_THREAD_UCON, + }[channel.type] || TEXT_CHANNEL_ICON; const channelType = { @@ -47,6 +51,9 @@ export async function getChannelInfoEmbed( [ChannelTypeStrings.NEWS]: "Announcement channel", [ChannelTypeStrings.STORE]: "Store channel", [ChannelTypeStrings.STAGE]: "Stage channel", + [ChannelTypeStrings.PUBLIC_THREAD]: "Public Thread channel", + [ChannelTypeStrings.PRIVATE_THREAD]: "Private Thread channel", + [ChannelTypeStrings.NEWS_THREAD]: "News Thread channel", }[channel.type] || "Channel"; embed.author = { @@ -121,5 +128,23 @@ export async function getChannelInfoEmbed( }); } + if (channel.type === ChannelTypeStrings.PRIVATE_THREAD || channel.type === ChannelTypeStrings.PUBLIC_THREAD) { + const thread = channel as ThreadChannel; + const parentChannelName = thread.parent?.name ? thread.parent.name : `<#${thread.parentId}>`; + const memberCount = thread.memberCount ?? thread.members.cache.size; + const owner = await pluginData.guild.members.fetch(thread.ownerId).catch(() => null); + const ownerMention = owner ? verboseUserMention(owner.user) : "Unknown#0000"; + const humanizedArchiveTime = `Archive duration: **${humanizeDuration(thread.autoArchiveDuration * MINUTES)}**`; + + embed.fields.push({ + name: preEmbedPadding + "Thread information", + value: trimLines(` + Parent channel: **#${parentChannelName}** + Member count: **${memberCount}** + Thread creator: ${ownerMention} + ${thread.archived ? "Archived: **True**" : humanizedArchiveTime}`), + }); + } + return embed; }