Add !channel/!channelinfo

This commit is contained in:
Dragory 2020-08-05 22:54:14 +03:00
parent a1a5952e44
commit 14af94e7a3
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
10 changed files with 144 additions and 0 deletions

View file

@ -28,6 +28,7 @@ import { AvatarCmd } from "./commands/AvatarCmd";
import { CleanCmd } from "./commands/CleanCmd";
import { Message } from "eris";
import { InviteInfoCmd } from "./commands/InviteInfoCmd";
import { ChannelInfoCmd } from "./commands/ChannelInfoCmd";
const defaultOptions: PluginOptions<UtilityPluginType> = {
config: {
@ -38,6 +39,7 @@ const defaultOptions: PluginOptions<UtilityPluginType> = {
can_info: false,
can_server: false,
can_invite: false,
can_channel: false,
can_reload_guild: false,
can_nickname: false,
can_ping: false,
@ -62,6 +64,7 @@ const defaultOptions: PluginOptions<UtilityPluginType> = {
can_info: true,
can_server: true,
can_invite: true,
can_channel: true,
can_nickname: true,
can_vcmove: true,
can_help: true,
@ -112,6 +115,7 @@ export const UtilityPlugin = zeppelinPlugin<UtilityPluginType>()("utility", {
AvatarCmd,
CleanCmd,
InviteInfoCmd,
ChannelInfoCmd,
],
onLoad(pluginData) {

View file

@ -0,0 +1,25 @@
import { utilityCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage } from "../../../pluginUtils";
import { getChannelInfoEmbed } from "../functions/getChannelInfoEmbed";
export const ChannelInfoCmd = utilityCmd({
trigger: ["channel", "channelinfo"],
description: "Show information about a channel",
usage: "!channel 534722016549404673",
permission: "can_channel",
signature: {
channel: ct.channelId({ required: false }),
},
async run({ message, args, pluginData }) {
const embed = await getChannelInfoEmbed(pluginData, args.channel);
if (!embed) {
sendErrorMessage(pluginData, message.channel, "Unknown channel");
return;
}
message.channel.createMessage({ embed });
},
});

View file

@ -0,0 +1,111 @@
import { PluginData } from "knub";
import { UtilityPluginType } from "../types";
import { Constants, EmbedOptions } from "eris";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { formatNumber, preEmbedPadding, trimLines } from "../../../utils";
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";
export async function getChannelInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
channelId: string,
): Promise<EmbedOptions | null> {
const channel = pluginData.guild.channels.get(channelId);
if (!channel) {
return null;
}
const embed: EmbedOptions = {
fields: [],
};
let icon;
if (channel.type === Constants.ChannelTypes.GUILD_VOICE) {
icon = VOICE_CHANNEL_ICON;
} else if (channel.type === Constants.ChannelTypes.GUILD_NEWS) {
icon = ANNOUNCEMENT_CHANNEL_ICON;
} else {
icon = TEXT_CHANNEL_ICON;
}
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",
}[channel.type] || "Channel";
embed.author = {
name: `${channelType}: ${channel.name}`,
icon_url: icon,
};
let channelName;
if (channel.type === Constants.ChannelTypes.GUILD_VOICE || channel.type === Constants.ChannelTypes.GUILD_CATEGORY) {
channelName = channel.name;
} else {
channelName = `#${channel.name}`;
}
const createdAt = moment(channel.createdAt, "x");
const channelAge = humanizeDuration(Date.now() - channel.createdAt, {
largest: 2,
round: true,
});
const showMention =
channel.type !== Constants.ChannelTypes.GUILD_VOICE && channel.type !== Constants.ChannelTypes.GUILD_CATEGORY;
embed.fields.push({
name: preEmbedPadding + "Channel information",
value: trimLines(`
Name: **${channelName}**
ID: \`${channel.id}\`
Created: **${channelAge} ago** (\`${createdAt.format("MMM D, YYYY [at] H:mm [UTC]")}\`)
Type: **${channelType}**
${showMention ? `Mention: <#${channel.id}>` : ""}
`),
});
if (channel.type === Constants.ChannelTypes.GUILD_VOICE) {
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);
embed.fields.push({
name: preEmbedPadding + "Voice information",
value: trimLines(`
Users on voice channel: **${formatNumber(voiceMembers.length)}**
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(
ch => ch.parentID === channel.id && ch.type === Constants.ChannelTypes.GUILD_VOICE,
);
embed.fields.push({
name: preEmbedPadding + "Category information",
value: trimLines(`
Text channels: **${textChannels.length}**
Voice channels: **${voiceChannels.length}**
`),
});
}
return embed;
}

View file

@ -14,6 +14,7 @@ export const ConfigSchema = t.type({
can_info: t.boolean,
can_server: t.boolean,
can_invite: t.boolean,
can_channel: t.boolean,
can_reload_guild: t.boolean,
can_nickname: t.boolean,
can_ping: t.boolean,