forked from ZeppelinBot/Zeppelin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandTypes.ts
More file actions
132 lines (111 loc) · 4.23 KB
/
commandTypes.ts
File metadata and controls
132 lines (111 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
escapeCodeBlock,
escapeInlineCode,
GuildChannel,
GuildMember,
GuildTextBasedChannel,
Snowflake,
User,
} from "discord.js";
import {
baseCommandParameterTypeHelpers,
CommandContext,
messageCommandBaseTypeConverters,
TypeConversionError,
} from "vety";
import { createTypeHelper } from "knub-command-manager";
import {
channelMentionRegex,
convertDelayStringToMS,
inputPatternToRegExp,
isValidSnowflake,
resolveMember,
resolveUser,
resolveUserId,
roleMentionRegex,
UnknownUser,
} from "./utils.js";
import { isValidTimezone } from "./utils/isValidTimezone.js";
import { MessageTarget, resolveMessageTarget } from "./utils/resolveMessageTarget.js";
export const commandTypes = {
...messageCommandBaseTypeConverters,
delay(value) {
const result = convertDelayStringToMS(value);
if (result == null) {
throw new TypeConversionError(`Could not convert ${value} to a delay`);
}
return result;
},
async resolvedUser(value, context: CommandContext<any>) {
const result = await resolveUser(context.pluginData.client, value, "commandTypes:resolvedUser");
if (result == null || result instanceof UnknownUser) {
throw new TypeConversionError(`User \`${escapeCodeBlock(value)}\` was not found`);
}
return result;
},
async resolvedUserLoose(value, context: CommandContext<any>) {
const result = await resolveUser(context.pluginData.client, value, "commandTypes:resolvedUserLoose");
if (result == null) {
throw new TypeConversionError(`Invalid user: \`${escapeCodeBlock(value)}\``);
}
return result;
},
async resolvedMember(value, context: CommandContext<any>) {
if (!(context.message.channel instanceof GuildChannel)) {
throw new TypeConversionError(`Cannot resolve member for non-guild channels`);
}
const result = await resolveMember(context.pluginData.client, context.message.channel.guild, value);
if (result == null) {
throw new TypeConversionError(`Member \`${escapeCodeBlock(value)}\` was not found or they have left the server`);
}
return result;
},
async messageTarget(value: string, context: CommandContext<any>) {
value = String(value).trim();
const result = await resolveMessageTarget(context.pluginData, value);
if (!result) {
throw new TypeConversionError(`Unknown message \`${escapeInlineCode(value)}\``);
}
return result;
},
async anyId(value: string, context: CommandContext<any>) {
const userId = resolveUserId(context.pluginData.client, value);
if (userId) return userId as Snowflake;
const channelIdMatch = value.match(channelMentionRegex);
if (channelIdMatch) return channelIdMatch[1] as Snowflake;
const roleIdMatch = value.match(roleMentionRegex);
if (roleIdMatch) return roleIdMatch[1] as Snowflake;
if (isValidSnowflake(value)) {
return value as Snowflake;
}
throw new TypeConversionError(`Could not parse ID: \`${escapeInlineCode(value)}\``);
},
regex(value: string): RegExp {
try {
return inputPatternToRegExp(value);
} catch (e) {
throw new TypeConversionError(`Could not parse RegExp: \`${escapeInlineCode(e.message)}\``);
}
},
timezone(value: string) {
if (!isValidTimezone(value)) {
throw new TypeConversionError(`Invalid timezone: ${escapeInlineCode(value)}`);
}
return value;
},
guildTextBasedChannel(value: string, context: CommandContext<any>) {
return messageCommandBaseTypeConverters.textChannel(value, context);
},
};
export const commandTypeHelpers = {
...baseCommandParameterTypeHelpers,
delay: createTypeHelper<number>(commandTypes.delay),
resolvedUser: createTypeHelper<Promise<User>>(commandTypes.resolvedUser),
resolvedUserLoose: createTypeHelper<Promise<User | UnknownUser>>(commandTypes.resolvedUserLoose),
resolvedMember: createTypeHelper<Promise<GuildMember>>(commandTypes.resolvedMember),
messageTarget: createTypeHelper<Promise<MessageTarget>>(commandTypes.messageTarget),
anyId: createTypeHelper<Promise<Snowflake>>(commandTypes.anyId),
regex: createTypeHelper<RegExp>(commandTypes.regex),
timezone: createTypeHelper<string>(commandTypes.timezone),
guildTextBasedChannel: createTypeHelper<GuildTextBasedChannel>(commandTypes.guildTextBasedChannel),
};