Add !roleinfo and !emojiinfo (#198)

This commit is contained in:
Almeida 2021-05-06 19:19:57 +01:00 committed by GitHub
parent fb4f70a29c
commit 519cb4ece2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 247 additions and 46 deletions

View file

@ -2,16 +2,18 @@ import { utilityCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage } from "../../../pluginUtils";
import { getInviteInfoEmbed } from "../functions/getInviteInfoEmbed";
import { isValidSnowflake, parseInviteCodeInput, resolveInvite, resolveUser } from "../../../utils";
import { customEmojiRegex, isValidSnowflake, 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 { getChannelId, getRoleId } from "knub/dist/utils";
import { getGuildPreview } from "../functions/getGuildPreview";
import { getSnowflakeInfoEmbed } from "../functions/getSnowflakeInfoEmbed";
import { getRoleInfoEmbed } from "../functions/getRoleInfoEmbed";
import { getEmojiInfoEmbed } from "../functions/getEmojiInfoEmbed";
export const InfoCmd = utilityCmd({
trigger: "info",
@ -34,59 +36,81 @@ export const InfoCmd = utilityCmd({
});
// 1. Channel
const channelId = getChannelId(value);
const channel = channelId && pluginData.guild.channels.get(channelId);
if (channel && userCfg.can_channelinfo) {
const embed = await getChannelInfoEmbed(pluginData, channelId!, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
if (userCfg.can_channelinfo) {
const channelId = getChannelId(value);
const channel = channelId && pluginData.guild.channels.get(channelId);
if (channel) {
const embed = await getChannelInfoEmbed(pluginData, channelId!, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
// 2. Server
const guild = pluginData.client.guilds.get(value);
if (guild && userCfg.can_server) {
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
if (userCfg.can_server) {
const guild = pluginData.client.guilds.get(value);
if (guild) {
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
// 3. User
const user = await resolveUser(pluginData.client, value);
if (user && userCfg.can_userinfo) {
const embed = await getUserInfoEmbed(pluginData, user.id, Boolean(args.compact), message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
if (userCfg.can_userinfo) {
const user = await resolveUser(pluginData.client, value);
if (user && userCfg.can_userinfo) {
const embed = await getUserInfoEmbed(pluginData, user.id, Boolean(args.compact), message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
// 4. Message
const messageTarget = await resolveMessageTarget(pluginData, value);
if (messageTarget && userCfg.can_messageinfo) {
if (canReadChannel(messageTarget.channel, message.member)) {
const embed = await getMessageInfoEmbed(
pluginData,
messageTarget.channel.id,
messageTarget.messageId,
message.author.id,
);
if (embed) {
message.channel.createMessage({ embed });
return;
if (userCfg.can_messageinfo) {
const messageTarget = await resolveMessageTarget(pluginData, value);
if (messageTarget) {
if (canReadChannel(messageTarget.channel, message.member)) {
const embed = await getMessageInfoEmbed(
pluginData,
messageTarget.channel.id,
messageTarget.messageId,
message.author.id,
);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
}
// 5. Invite
const inviteCode = parseInviteCodeInput(value) ?? value;
if (inviteCode) {
const invite = await resolveInvite(pluginData.client, inviteCode, true);
if (invite && userCfg.can_inviteinfo) {
const embed = await getInviteInfoEmbed(pluginData, inviteCode);
if (userCfg.can_inviteinfo) {
const inviteCode = parseInviteCodeInput(value) ?? 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)
if (userCfg.can_server) {
const serverPreview = getGuildPreview(pluginData.client, value).catch(() => null);
if (serverPreview) {
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
@ -94,24 +118,37 @@ export const InfoCmd = utilityCmd({
}
}
// 6. Server again (fallback for discovery servers)
const serverPreview = getGuildPreview(pluginData.client, value).catch(() => null);
if (serverPreview && userCfg.can_server) {
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
// 7. Role
if (userCfg.can_roleinfo) {
const roleId = getRoleId(value);
const role = roleId && pluginData.guild.roles.get(roleId);
if (role) {
const embed = await getRoleInfoEmbed(pluginData, role, message.author.id);
message.channel.createMessage({ embed });
return;
}
}
// 7. Arbitrary ID
// 8. Emoji
if (userCfg.can_emojiinfo) {
const emojiIdMatch = value.match(customEmojiRegex);
if (emojiIdMatch?.[2]) {
const embed = await getEmojiInfoEmbed(pluginData, emojiIdMatch[2]);
if (embed) {
message.channel.createMessage({ embed });
return;
}
}
}
// 9. Arbitrary ID
if (isValidSnowflake(value) && userCfg.can_snowflake) {
const embed = await getSnowflakeInfoEmbed(pluginData, value, true, message.author.id);
message.channel.createMessage({ embed });
return;
}
// 7. No can do
// 10. No can do
sendErrorMessage(
pluginData,
message.channel,