2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-08-05 22:54:14 +03:00
|
|
|
import { UtilityPluginType } from "../types";
|
|
|
|
import { Constants, EmbedOptions } from "eris";
|
|
|
|
import moment from "moment-timezone";
|
|
|
|
import humanizeDuration from "humanize-duration";
|
2020-11-09 20:03:57 +02:00
|
|
|
import { EmbedWith, formatNumber, preEmbedPadding, trimLines } from "../../../utils";
|
2020-08-19 00:19:12 +03:00
|
|
|
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
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 =
|
|
|
|
"https://cdn.discordapp.com/attachments/705009450855039042/839292597859516446/stage-channel.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,
|
2020-08-05 22:54:14 +03:00
|
|
|
): Promise<EmbedOptions | null> {
|
|
|
|
const channel = pluginData.guild.channels.get(channelId);
|
|
|
|
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-05-06 19:25:17 +01:00
|
|
|
let icon = TEXT_CHANNEL_ICON;
|
2020-08-05 22:54:14 +03:00
|
|
|
if (channel.type === Constants.ChannelTypes.GUILD_VOICE) {
|
|
|
|
icon = VOICE_CHANNEL_ICON;
|
|
|
|
} else if (channel.type === Constants.ChannelTypes.GUILD_NEWS) {
|
|
|
|
icon = ANNOUNCEMENT_CHANNEL_ICON;
|
2021-05-06 19:25:17 +01:00
|
|
|
} else if (channel.type === Constants.ChannelTypes.GUILD_STAGE) {
|
|
|
|
icon = STAGE_CHANNEL_ICON;
|
2020-08-05 22:54:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const channelType =
|
|
|
|
{
|
|
|
|
[Constants.ChannelTypes.GUILD_TEXT]: "Text channel",
|
|
|
|
[Constants.ChannelTypes.GUILD_VOICE]: "Voice channel",
|
|
|
|
[Constants.ChannelTypes.GUILD_CATEGORY]: "Category",
|
|
|
|
[Constants.ChannelTypes.GUILD_NEWS]: "Announcement channel",
|
|
|
|
[Constants.ChannelTypes.GUILD_STORE]: "Store channel",
|
2021-05-06 19:25:17 +01:00
|
|
|
[Constants.ChannelTypes.GUILD_STAGE]: "Stage 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 (
|
|
|
|
channel.type === Constants.ChannelTypes.GUILD_VOICE ||
|
|
|
|
channel.type === Constants.ChannelTypes.GUILD_CATEGORY ||
|
|
|
|
channel.type === Constants.ChannelTypes.GUILD_STAGE
|
|
|
|
) {
|
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"));
|
2020-08-05 22:54:14 +03:00
|
|
|
const channelAge = humanizeDuration(Date.now() - channel.createdAt, {
|
|
|
|
largest: 2,
|
|
|
|
round: true,
|
|
|
|
});
|
|
|
|
|
2021-05-06 19:25:17 +01:00
|
|
|
const showMention = channel.type !== Constants.ChannelTypes.GUILD_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-05-06 19:25:17 +01:00
|
|
|
if (channel.type === Constants.ChannelTypes.GUILD_VOICE || channel.type === Constants.ChannelTypes.GUILD_STAGE) {
|
2020-08-05 22:54:14 +03:00
|
|
|
const voiceMembers = Array.from(channel.voiceMembers.values());
|
|
|
|
const muted = voiceMembers.filter(vm => vm.voiceState.mute || vm.voiceState.selfMute);
|
|
|
|
const deafened = voiceMembers.filter(vm => vm.voiceState.deaf || vm.voiceState.selfDeaf);
|
2021-05-06 19:25:17 +01:00
|
|
|
const voiceOrStage = channel.type === Constants.ChannelTypes.GUILD_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)}**
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel.type === Constants.ChannelTypes.GUILD_CATEGORY) {
|
|
|
|
const textChannels = pluginData.guild.channels.filter(
|
|
|
|
ch => ch.parentID === channel.id && ch.type !== Constants.ChannelTypes.GUILD_VOICE,
|
|
|
|
);
|
|
|
|
const voiceChannels = pluginData.guild.channels.filter(
|
2021-05-06 19:25:17 +01:00
|
|
|
ch =>
|
|
|
|
ch.parentID === channel.id &&
|
|
|
|
(ch.type === Constants.ChannelTypes.GUILD_VOICE || ch.type === Constants.ChannelTypes.GUILD_STAGE),
|
2020-08-05 22:54:14 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
embed.fields.push({
|
|
|
|
name: preEmbedPadding + "Category information",
|
|
|
|
value: trimLines(`
|
|
|
|
Text channels: **${textChannels.length}**
|
|
|
|
Voice channels: **${voiceChannels.length}**
|
|
|
|
`),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return embed;
|
|
|
|
}
|