3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Add new multi-use !info command

This commit is contained in:
Dragory 2020-08-06 01:10:40 +03:00
parent 7ad2458fd9
commit 106a959b4d
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
7 changed files with 184 additions and 61 deletions

View file

@ -30,6 +30,7 @@ import { Message } from "eris";
import { InviteInfoCmd } from "./commands/InviteInfoCmd";
import { ChannelInfoCmd } from "./commands/ChannelInfoCmd";
import { MessageInfoCmd } from "./commands/MessageInfoCmd";
import { InfoCmd } from "./commands/InfoCmd";
const defaultOptions: PluginOptions<UtilityPluginType> = {
config: {
@ -39,9 +40,9 @@ const defaultOptions: PluginOptions<UtilityPluginType> = {
can_clean: false,
can_info: false,
can_server: false,
can_invite: false,
can_channel: false,
can_message: false,
can_inviteinfo: false,
can_channelinfo: false,
can_messageinfo: false,
can_userinfo: false,
can_reload_guild: false,
can_nickname: false,
@ -66,9 +67,9 @@ const defaultOptions: PluginOptions<UtilityPluginType> = {
can_clean: true,
can_info: true,
can_server: true,
can_invite: true,
can_channel: true,
can_message: true,
can_inviteinfo: true,
can_channelinfo: true,
can_messageinfo: true,
can_userinfo: true,
can_nickname: true,
can_vcmove: true,
@ -122,6 +123,7 @@ export const UtilityPlugin = zeppelinPlugin<UtilityPluginType>()("utility", {
InviteInfoCmd,
ChannelInfoCmd,
MessageInfoCmd,
InfoCmd,
],
onLoad(pluginData) {

View file

@ -0,0 +1,99 @@
import { utilityCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage } from "../../../pluginUtils";
import { getInviteInfoEmbed } from "../functions/getInviteInfoEmbed";
import { parseInviteCodeInput, resolveInvite, resolveUser } from "../../../utils";
import { getUserInfoEmbed } from "../functions/getUserInfoEmbed";
import { resolveMessageTarget } from "../../../utils/resolveMessageTarget";
import { canReadChannel } from "../../../utils/canReadChannel";
import { getMessageInfoEmbed } from "../functions/getMessageInfoEmbed";
import { getChannelInfoEmbed } from "../functions/getChannelInfoEmbed";
import { getServerInfoEmbed } from "../functions/getServerInfoEmbed";
import { getChannelId } from "knub/dist/utils";
import { getGuildPreview } from "../functions/getGuildPreview";
export const InfoCmd = utilityCmd({
trigger: "info",
description: "Show information about the specified thing",
usage: "!info",
permission: "can_info",
signature: {
value: ct.string({ required: false }),
compact: ct.switchOption({ shortcut: "c" }),
},
async run({ message, args, pluginData }) {
const value = args.value || message.author.id;
// 1. Channel
const channelId = getChannelId(value);
const channel = channelId && pluginData.guild.channels.get(channelId);
if (channel) {
const embed = await getChannelInfoEmbed(pluginData, channelId);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
// 2. Server
const guild = pluginData.client.guilds.get(value);
if (guild) {
const embed = await getServerInfoEmbed(pluginData, value);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
// 3. User
const user = await resolveUser(pluginData.client, value);
if (user) {
const embed = await getUserInfoEmbed(pluginData, user.id, Boolean(args.compact));
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
// 4. Message
const messageTarget = await resolveMessageTarget(pluginData, value);
if (messageTarget) {
if (canReadChannel(messageTarget.channel, message.author.id)) {
const embed = await getMessageInfoEmbed(pluginData, messageTarget.channel.id, messageTarget.messageId);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
// 5. Invite
const inviteCode = await parseInviteCodeInput(value);
if (inviteCode) {
const invite = await resolveInvite(pluginData.client, inviteCode, true);
if (invite) {
const embed = await getInviteInfoEmbed(pluginData, inviteCode);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
// 6. Server again (fallback for discovery servers)
const serverPreview = getGuildPreview(pluginData.client, value).catch(() => null);
if (serverPreview) {
const embed = await getServerInfoEmbed(pluginData, value);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
// 7. No can do
sendErrorMessage(pluginData, message.channel, "Could not find anything with that value");
},
});

View file

@ -0,0 +1,9 @@
import { Client, GuildPreview } from "eris";
import { memoize, MINUTES } from "../../../utils";
/**
* Memoized getGuildPreview
*/
export function getGuildPreview(client: Client, guildId: string): Promise<GuildPreview | null> {
return memoize(() => client.getGuildPreview(guildId).catch(() => null), `getGuildPreview_${guildId}`, 10 * MINUTES);
}

View file

@ -4,6 +4,7 @@ import { embedPadding, formatNumber, memoize, MINUTES, preEmbedPadding, resolveU
import { CategoryChannel, EmbedOptions, Guild, RESTChannelInvite, TextChannel, VoiceChannel } from "eris";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { getGuildPreview } from "./getGuildPreview";
export async function getServerInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
@ -14,11 +15,7 @@ export async function getServerInfoEmbed(
thisServer
? memoize(() => pluginData.client.getRESTGuild(serverId), `getRESTGuild_${serverId}`, 10 * MINUTES)
: null,
memoize(
() => pluginData.client.getGuildPreview(serverId).catch(() => null),
`getGuildPreview_${serverId}`,
10 * MINUTES,
),
getGuildPreview(pluginData.client, serverId),
]);
if (!restGuild && !guildPreview) {

View file

@ -13,9 +13,9 @@ export const ConfigSchema = t.type({
can_clean: t.boolean,
can_info: t.boolean,
can_server: t.boolean,
can_invite: t.boolean,
can_channel: t.boolean,
can_message: t.boolean,
can_inviteinfo: t.boolean,
can_channelinfo: t.boolean,
can_messageinfo: t.boolean,
can_userinfo: t.boolean,
can_reload_guild: t.boolean,
can_nickname: t.boolean,