2022-06-13 21:19:56 +03:00
|
|
|
import { GuildChannel, GuildMember, Snowflake, Util, User, GuildTextBasedChannel } from "discord.js";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { baseCommandParameterTypeHelpers, baseTypeConverters, CommandContext, TypeConversionError } from "knub";
|
|
|
|
import { createTypeHelper } from "knub-command-manager";
|
2020-08-05 23:57:09 +03:00
|
|
|
import {
|
2021-06-08 02:23:30 +02:00
|
|
|
channelMentionRegex,
|
|
|
|
convertDelayStringToMS,
|
|
|
|
isValidSnowflake,
|
|
|
|
resolveMember,
|
|
|
|
resolveUser,
|
|
|
|
resolveUserId,
|
|
|
|
roleMentionRegex,
|
|
|
|
UnknownUser,
|
2020-08-05 23:57:09 +03:00
|
|
|
} from "./utils";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { isValidTimezone } from "./utils/isValidTimezone";
|
2020-08-06 01:10:40 +03:00
|
|
|
import { MessageTarget, resolveMessageTarget } from "./utils/resolveMessageTarget";
|
2020-08-10 01:09:45 +03:00
|
|
|
import { inputPatternToRegExp } from "./validatorUtils";
|
2022-06-13 21:19:56 +03:00
|
|
|
import { getChannelId } from "knub/dist/utils";
|
|
|
|
import { disableCodeBlocks } from "knub/dist/helpers";
|
2019-04-20 19:03:30 +03:00
|
|
|
|
2020-07-06 01:51:48 +03:00
|
|
|
export const commandTypes = {
|
|
|
|
...baseTypeConverters,
|
|
|
|
|
2019-04-20 19:03:30 +03:00
|
|
|
delay(value) {
|
|
|
|
const result = convertDelayStringToMS(value);
|
|
|
|
if (result == null) {
|
2019-09-22 17:06:22 +03:00
|
|
|
throw new TypeConversionError(`Could not convert ${value} to a delay`);
|
2019-04-20 19:03:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2020-07-05 05:00:54 +03:00
|
|
|
async resolvedUser(value, context: CommandContext<any>) {
|
|
|
|
const result = await resolveUser(context.pluginData.client, value);
|
2019-04-20 19:03:30 +03:00
|
|
|
if (result == null || result instanceof UnknownUser) {
|
2021-07-29 17:35:27 +01:00
|
|
|
throw new TypeConversionError(`User \`${Util.escapeCodeBlock(value)}\` was not found`);
|
2019-04-20 19:03:30 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2020-07-05 05:00:54 +03:00
|
|
|
async resolvedUserLoose(value, context: CommandContext<any>) {
|
|
|
|
const result = await resolveUser(context.pluginData.client, value);
|
2019-04-20 19:03:30 +03:00
|
|
|
if (result == null) {
|
2021-07-29 17:35:27 +01:00
|
|
|
throw new TypeConversionError(`Invalid user: \`${Util.escapeCodeBlock(value)}\``);
|
2019-04-20 19:03:30 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2020-07-05 05:00:54 +03:00
|
|
|
async resolvedMember(value, context: CommandContext<any>) {
|
2020-11-09 20:03:57 +02:00
|
|
|
if (!(context.message.channel instanceof GuildChannel)) {
|
|
|
|
throw new TypeConversionError(`Cannot resolve member for non-guild channels`);
|
|
|
|
}
|
2019-04-20 19:03:30 +03:00
|
|
|
|
2020-07-05 05:00:54 +03:00
|
|
|
const result = await resolveMember(context.pluginData.client, context.message.channel.guild, value);
|
2019-04-20 19:03:30 +03:00
|
|
|
if (result == null) {
|
2019-09-22 17:06:22 +03:00
|
|
|
throw new TypeConversionError(
|
2021-07-29 17:35:27 +01:00
|
|
|
`Member \`${Util.escapeCodeBlock(value)}\` was not found or they have left the server`,
|
2019-04-20 20:43:47 +03:00
|
|
|
);
|
2019-04-20 19:03:30 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2020-08-05 23:57:09 +03:00
|
|
|
|
|
|
|
async messageTarget(value: string, context: CommandContext<any>) {
|
|
|
|
value = String(value).trim();
|
|
|
|
|
2020-08-06 01:10:40 +03:00
|
|
|
const result = await resolveMessageTarget(context.pluginData, value);
|
|
|
|
if (!result) {
|
2021-07-29 17:35:27 +01:00
|
|
|
throw new TypeConversionError(`Unknown message \`${Util.escapeInlineCode(value)}\``);
|
2020-08-05 23:57:09 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:10:40 +03:00
|
|
|
return result;
|
2020-08-05 23:57:09 +03:00
|
|
|
},
|
2020-08-09 17:28:21 +03:00
|
|
|
|
|
|
|
async anyId(value: string, context: CommandContext<any>) {
|
|
|
|
const userId = resolveUserId(context.pluginData.client, value);
|
2021-07-27 04:19:11 +02:00
|
|
|
if (userId) return userId as Snowflake;
|
2020-08-09 17:28:21 +03:00
|
|
|
|
|
|
|
const channelIdMatch = value.match(channelMentionRegex);
|
2021-07-27 04:19:11 +02:00
|
|
|
if (channelIdMatch) return channelIdMatch[1] as Snowflake;
|
2020-08-09 17:28:21 +03:00
|
|
|
|
|
|
|
const roleIdMatch = value.match(roleMentionRegex);
|
2021-07-27 04:19:11 +02:00
|
|
|
if (roleIdMatch) return roleIdMatch[1] as Snowflake;
|
2020-08-09 17:28:21 +03:00
|
|
|
|
|
|
|
if (isValidSnowflake(value)) {
|
2021-07-27 04:19:11 +02:00
|
|
|
return value as Snowflake;
|
2020-08-09 17:28:21 +03:00
|
|
|
}
|
|
|
|
|
2021-07-29 17:35:27 +01:00
|
|
|
throw new TypeConversionError(`Could not parse ID: \`${Util.escapeInlineCode(value)}\``);
|
2020-08-09 17:28:21 +03:00
|
|
|
},
|
2020-08-10 01:09:45 +03:00
|
|
|
|
|
|
|
regex(value: string, context: CommandContext<any>): RegExp {
|
|
|
|
try {
|
|
|
|
return inputPatternToRegExp(value);
|
|
|
|
} catch (e) {
|
2021-07-29 17:35:27 +01:00
|
|
|
throw new TypeConversionError(`Could not parse RegExp: \`${Util.escapeInlineCode(e.message)}\``);
|
2020-08-10 01:09:45 +03:00
|
|
|
}
|
|
|
|
},
|
2020-08-19 00:19:12 +03:00
|
|
|
|
|
|
|
timezone(value: string) {
|
|
|
|
if (!isValidTimezone(value)) {
|
2021-07-29 17:35:27 +01:00
|
|
|
throw new TypeConversionError(`Invalid timezone: ${Util.escapeInlineCode(value)}`);
|
2020-08-19 00:19:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
2022-06-13 21:19:56 +03:00
|
|
|
|
|
|
|
guildTextBasedChannel(value: string, context: CommandContext<any>) {
|
|
|
|
// FIXME: Remove once Knub's types have been fixed
|
|
|
|
return baseTypeConverters.textChannel(value, context) as GuildTextBasedChannel;
|
|
|
|
},
|
2019-04-20 19:03:30 +03:00
|
|
|
};
|
2020-07-05 05:00:54 +03:00
|
|
|
|
2020-07-06 01:51:48 +03:00
|
|
|
export const commandTypeHelpers = {
|
2021-05-23 14:35:16 +03:00
|
|
|
...baseCommandParameterTypeHelpers,
|
2020-07-06 01:51:48 +03:00
|
|
|
|
|
|
|
delay: createTypeHelper<number>(commandTypes.delay),
|
|
|
|
resolvedUser: createTypeHelper<Promise<User>>(commandTypes.resolvedUser),
|
|
|
|
resolvedUserLoose: createTypeHelper<Promise<User | UnknownUser>>(commandTypes.resolvedUserLoose),
|
2021-05-31 21:12:24 +02:00
|
|
|
resolvedMember: createTypeHelper<Promise<GuildMember>>(commandTypes.resolvedMember),
|
2020-08-05 23:57:09 +03:00
|
|
|
messageTarget: createTypeHelper<Promise<MessageTarget>>(commandTypes.messageTarget),
|
2021-07-27 04:19:11 +02:00
|
|
|
anyId: createTypeHelper<Promise<Snowflake>>(commandTypes.anyId),
|
2020-08-10 01:09:45 +03:00
|
|
|
regex: createTypeHelper<RegExp>(commandTypes.regex),
|
2020-08-19 00:19:12 +03:00
|
|
|
timezone: createTypeHelper<string>(commandTypes.timezone),
|
2022-06-13 21:19:56 +03:00
|
|
|
guildTextBasedChannel: createTypeHelper<GuildTextBasedChannel>(commandTypes.guildTextBasedChannel),
|
2020-07-05 05:00:54 +03:00
|
|
|
};
|