2021-07-29 18:46:52 +02:00
|
|
|
import { MessageEmbedOptions, Snowflake, StageChannel, ThreadChannel, VoiceChannel } from "discord.js";
|
2021-06-06 23:51:32 +02:00
|
|
|
import humanizeDuration from "humanize-duration";
|
2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-08-05 22:54:14 +03:00
|
|
|
import moment from "moment-timezone";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { ChannelTypeStrings } from "src/types";
|
2021-07-29 18:46:52 +02:00
|
|
|
import { EmbedWith, formatNumber, MINUTES, preEmbedPadding, trimLines, verboseUserMention } from "../../../utils";
|
2020-08-19 00:19:12 +03:00
|
|
|
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { UtilityPluginType } from "../types";
|
|
|
|
|
2020-08-05 22:54:14 +03:00
|
|
|
const TEXT_CHANNEL_ICON =
|
|
|
|
"https://cdn.discordapp.com/attachments/740650744830623756/740656843545772062/text-channel.png";
|
|
|
|
const VOICE_CHANNEL_ICON =
|
|
|
|
"https://cdn.discordapp.com/attachments/740650744830623756/740656845982662716/voice-channel.png";
|
|
|
|
const ANNOUNCEMENT_CHANNEL_ICON =
|
|
|
|
"https://cdn.discordapp.com/attachments/740650744830623756/740656841687564348/announcement-channel.png";
|
2021-05-06 19:25:17 +01:00
|
|
|
const STAGE_CHANNEL_ICON =
|
2021-05-06 21:27:50 +03:00
|
|
|
"https://cdn.discordapp.com/attachments/740650744830623756/839930647711186995/stage-channel.png";
|
2021-07-29 18:46:52 +02:00
|
|
|
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";
|
2020-08-05 22:54:14 +03:00
|
|
|
|
|
|
|
export async function getChannelInfoEmbed(
|
2020-10-01 01:43:38 +03:00
|
|
|
pluginData: GuildPluginData<UtilityPluginType>,
|
2020-08-05 22:54:14 +03:00
|
|
|
channelId: string,
|
2020-08-19 00:19:12 +03:00
|
|
|
requestMemberId?: string,
|
2021-06-02 04:07:50 +02:00
|
|
|
): Promise<MessageEmbedOptions | null> {
|
2021-06-30 04:56:56 +02:00
|
|
|
const channel = pluginData.guild.channels.cache.get(channelId as Snowflake);
|
2020-08-05 22:54:14 +03:00
|
|
|
if (!channel) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const embed: EmbedWith<"fields"> = {
|
2020-08-05 22:54:14 +03:00
|
|
|
fields: [],
|
|
|
|
};
|
|
|
|
|
2021-07-29 18:46:52 +02:00
|
|
|
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;
|
2020-08-05 22:54:14 +03:00
|
|
|
|
|
|
|
const channelType =
|
|
|
|
{
|
2021-06-02 04:07:50 +02:00
|
|
|
[ChannelTypeStrings.TEXT]: "Text channel",
|
|
|
|
[ChannelTypeStrings.VOICE]: "Voice channel",
|
|
|
|
[ChannelTypeStrings.CATEGORY]: "Category",
|
|
|
|
[ChannelTypeStrings.NEWS]: "Announcement channel",
|
|
|
|
[ChannelTypeStrings.STORE]: "Store channel",
|
|
|
|
[ChannelTypeStrings.STAGE]: "Stage channel",
|
2021-07-29 18:46:52 +02:00
|
|
|
[ChannelTypeStrings.PUBLIC_THREAD]: "Public Thread channel",
|
|
|
|
[ChannelTypeStrings.PRIVATE_THREAD]: "Private Thread channel",
|
|
|
|
[ChannelTypeStrings.NEWS_THREAD]: "News Thread channel",
|
2020-08-05 22:54:14 +03:00
|
|
|
}[channel.type] || "Channel";
|
|
|
|
|
|
|
|
embed.author = {
|
|
|
|
name: `${channelType}: ${channel.name}`,
|
|
|
|
icon_url: icon,
|
|
|
|
};
|
|
|
|
|
2021-05-06 19:25:17 +01:00
|
|
|
let channelName = `#${channel.name}`;
|
|
|
|
if (
|
2021-06-02 04:07:50 +02:00
|
|
|
channel.type === ChannelTypeStrings.VOICE ||
|
|
|
|
channel.type === ChannelTypeStrings.CATEGORY ||
|
|
|
|
channel.type === ChannelTypeStrings.STAGE
|
2021-05-06 19:25:17 +01:00
|
|
|
) {
|
2020-08-05 22:54:14 +03:00
|
|
|
channelName = channel.name;
|
|
|
|
}
|
|
|
|
|
2020-08-10 00:24:06 +03:00
|
|
|
const createdAt = moment.utc(channel.createdAt, "x");
|
2020-08-19 00:19:12 +03:00
|
|
|
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
|
|
|
|
const tzCreatedAt = requestMemberId
|
|
|
|
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
|
|
|
|
: timeAndDate.inGuildTz(createdAt);
|
|
|
|
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
|
2021-06-02 04:07:50 +02:00
|
|
|
const channelAge = humanizeDuration(Date.now() - channel.createdTimestamp, {
|
2020-08-05 22:54:14 +03:00
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
|
|
|
|
2021-06-02 04:07:50 +02:00
|
|
|
const showMention = channel.type !== ChannelTypeStrings.CATEGORY;
|
2020-08-05 22:54:14 +03:00
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: preEmbedPadding + "Channel information",
|
|
|
|
value: trimLines(`
|
|
|
|
Name: **${channelName}**
|
|
|
|
ID: \`${channel.id}\`
|
2020-08-10 00:24:06 +03:00
|
|
|
Created: **${channelAge} ago** (\`${prettyCreatedAt}\`)
|
2020-08-05 22:54:14 +03:00
|
|
|
Type: **${channelType}**
|
|
|
|
${showMention ? `Mention: <#${channel.id}>` : ""}
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
|
2021-06-02 04:07:50 +02:00
|
|
|
if (channel.type === ChannelTypeStrings.VOICE || channel.type === ChannelTypeStrings.STAGE) {
|
|
|
|
const voiceMembers = Array.from((channel as VoiceChannel | StageChannel).members.values());
|
|
|
|
const muted = voiceMembers.filter(vm => vm.voice.mute || vm.voice.selfMute);
|
|
|
|
const deafened = voiceMembers.filter(vm => vm.voice.deaf || vm.voice.selfDeaf);
|
|
|
|
const voiceOrStage = channel.type === ChannelTypeStrings.VOICE ? "Voice" : "Stage";
|
2020-08-05 22:54:14 +03:00
|
|
|
|
|
|
|
embed.fields.push({
|
2021-05-06 19:25:17 +01:00
|
|
|
name: preEmbedPadding + `${voiceOrStage} information`,
|
2020-08-05 22:54:14 +03:00
|
|
|
value: trimLines(`
|
2021-05-06 19:25:17 +01:00
|
|
|
Users on ${voiceOrStage.toLowerCase()} channel: **${formatNumber(voiceMembers.length)}**
|
2020-08-05 22:54:14 +03:00
|
|
|
Muted: **${formatNumber(muted.length)}**
|
|
|
|
Deafened: **${formatNumber(deafened.length)}**
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-02 04:07:50 +02:00
|
|
|
if (channel.type === ChannelTypeStrings.CATEGORY) {
|
|
|
|
const textChannels = pluginData.guild.channels.cache.filter(
|
2021-07-04 23:14:12 +02:00
|
|
|
ch => ch.parentId === channel.id && ch.type !== ChannelTypeStrings.VOICE,
|
2020-08-05 22:54:14 +03:00
|
|
|
);
|
2021-06-02 04:07:50 +02:00
|
|
|
const voiceChannels = pluginData.guild.channels.cache.filter(
|
2021-05-06 19:25:17 +01:00
|
|
|
ch =>
|
2021-07-04 23:14:12 +02:00
|
|
|
ch.parentId === channel.id && (ch.type === ChannelTypeStrings.VOICE || ch.type === ChannelTypeStrings.STAGE),
|
2020-08-05 22:54:14 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: preEmbedPadding + "Category information",
|
|
|
|
value: trimLines(`
|
2021-06-02 04:07:50 +02:00
|
|
|
Text channels: **${textChannels.size}**
|
|
|
|
Voice channels: **${voiceChannels.size}**
|
2020-08-05 22:54:14 +03:00
|
|
|
`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-29 18:46:52 +02:00
|
|
|
if (channel.type === ChannelTypeStrings.PRIVATE_THREAD || channel.type === ChannelTypeStrings.PUBLIC_THREAD) {
|
|
|
|
const thread = channel as ThreadChannel;
|
2021-08-04 20:45:42 +01:00
|
|
|
const parentChannelName = thread.parent?.name ?? `<#${thread.parentId}>`;
|
2021-07-29 18:46:52 +02:00
|
|
|
const memberCount = thread.memberCount ?? thread.members.cache.size;
|
2021-08-04 20:45:42 +01:00
|
|
|
const owner = await thread.fetchOwner().catch(() => null);
|
|
|
|
const ownerMention = owner?.user ? verboseUserMention(owner.user) : "Unknown#0000";
|
|
|
|
const humanizedArchiveTime = `Archive duration: **${humanizeDuration(
|
|
|
|
(thread.autoArchiveDuration ?? 0) * MINUTES,
|
|
|
|
)}**`;
|
2021-07-29 18:46:52 +02:00
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: preEmbedPadding + "Thread information",
|
|
|
|
value: trimLines(`
|
|
|
|
Parent channel: **#${parentChannelName}**
|
|
|
|
Member count: **${memberCount}**
|
|
|
|
Thread creator: ${ownerMention}
|
|
|
|
${thread.archived ? "Archived: **True**" : humanizedArchiveTime}`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:54:14 +03:00
|
|
|
return embed;
|
|
|
|
}
|