zappyzep/backend/src/plugins/Utility/functions/getChannelInfoEmbed.ts

126 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-06-30 04:56:56 +02:00
import { MessageEmbedOptions, Snowflake, StageChannel, VoiceChannel } from "discord.js";
import humanizeDuration from "humanize-duration";
import { GuildPluginData } from "knub";
2020-08-05 22:54:14 +03:00
import moment from "moment-timezone";
import { ChannelTypeStrings } from "src/types";
import { EmbedWith, formatNumber, preEmbedPadding, trimLines } from "../../../utils";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
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";
const STAGE_CHANNEL_ICON =
2021-05-06 21:27:50 +03:00
"https://cdn.discordapp.com/attachments/740650744830623756/839930647711186995/stage-channel.png";
2020-08-05 22:54:14 +03:00
export async function getChannelInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
2020-08-05 22:54:14 +03:00
channelId: string,
requestMemberId?: string,
): 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;
}
const embed: EmbedWith<"fields"> = {
2020-08-05 22:54:14 +03:00
fields: [],
};
let icon = TEXT_CHANNEL_ICON;
if (channel.type === ChannelTypeStrings.VOICE) {
2020-08-05 22:54:14 +03:00
icon = VOICE_CHANNEL_ICON;
} else if (channel.type === ChannelTypeStrings.NEWS) {
2020-08-05 22:54:14 +03:00
icon = ANNOUNCEMENT_CHANNEL_ICON;
} else if (channel.type === ChannelTypeStrings.STAGE) {
icon = STAGE_CHANNEL_ICON;
2020-08-05 22:54:14 +03:00
}
const channelType =
{
[ChannelTypeStrings.TEXT]: "Text channel",
[ChannelTypeStrings.VOICE]: "Voice channel",
[ChannelTypeStrings.CATEGORY]: "Category",
[ChannelTypeStrings.NEWS]: "Announcement channel",
[ChannelTypeStrings.STORE]: "Store channel",
[ChannelTypeStrings.STAGE]: "Stage channel",
2020-08-05 22:54:14 +03:00
}[channel.type] || "Channel";
embed.author = {
name: `${channelType}: ${channel.name}`,
icon_url: icon,
};
let channelName = `#${channel.name}`;
if (
channel.type === ChannelTypeStrings.VOICE ||
channel.type === ChannelTypeStrings.CATEGORY ||
channel.type === ChannelTypeStrings.STAGE
) {
2020-08-05 22:54:14 +03:00
channelName = channel.name;
}
const createdAt = moment.utc(channel.createdAt, "x");
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const channelAge = humanizeDuration(Date.now() - channel.createdTimestamp, {
2020-08-05 22:54:14 +03:00
largest: 2,
round: true,
});
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}\`
Created: **${channelAge} ago** (\`${prettyCreatedAt}\`)
2020-08-05 22:54:14 +03:00
Type: **${channelType}**
${showMention ? `Mention: <#${channel.id}>` : ""}
`),
});
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({
name: preEmbedPadding + `${voiceOrStage} information`,
2020-08-05 22:54:14 +03:00
value: trimLines(`
Users on ${voiceOrStage.toLowerCase()} channel: **${formatNumber(voiceMembers.length)}**
2020-08-05 22:54:14 +03:00
Muted: **${formatNumber(muted.length)}**
Deafened: **${formatNumber(deafened.length)}**
`),
});
}
if (channel.type === ChannelTypeStrings.CATEGORY) {
const textChannels = pluginData.guild.channels.cache.filter(
ch => ch.parentID === channel.id && ch.type !== ChannelTypeStrings.VOICE,
2020-08-05 22:54:14 +03:00
);
const voiceChannels = pluginData.guild.channels.cache.filter(
ch =>
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(`
Text channels: **${textChannels.size}**
Voice channels: **${voiceChannels.size}**
2020-08-05 22:54:14 +03:00
`),
});
}
return embed;
}