-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
CommandInteractionOptionResolver.js
159 lines (146 loc) · 5.73 KB
/
CommandInteractionOptionResolver.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
/**
* A resolver for command interaction options.
*/
class CommandInteractionOptionResolver {
constructor(options) {
/**
* The interaction options array.
* @type {CommandInteractionOption[]}
* @private
*/
this.options = options ?? [];
}
/**
* Gets an option by its name.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {?CommandInteractionOption} The option, if found.
*/
get(name, required = false) {
const option = this.options.find(opt => opt.name === name);
if (option) {
return option;
} else if (required) {
throw new Error(`Missing required option "${name}"`);
} else {
return null;
}
}
/**
* Gets an option by name and property and checks its type.
* @param {string} name The name of the option.
* @param {ApplicationCommandOptionType[]} types The type of the option.
* @param {string[]} properties The property to check for for `required`.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {*|null} The value of `property`.
* @private
*/
_getTypedOption(name, types, properties, required) {
const option = this.get(name, required);
if (option) {
if (!types.includes(option.type)) {
throw new Error(`Option "${name}" is of type "${option.type}"; expected ${types.join('|')}.`);
} else if (required && properties.every(prop => option[prop] === null || typeof option[prop] === 'undefined')) {
throw new Error(`Option "${name}" of type "${option.type}" is required, but is empty.`);
} else {
return option;
}
} else {
return option;
}
}
/**
* Gets a sub-command or sub-command group.
* @param {string} name The name of the sub-command or sub-command group.
* @returns {CommandInteractionOptionResolver|null}
* A new resolver for the sub-command/group's options, or null if empty
*/
getSubCommand(name) {
const option = this._getTypedOption(name, ['SUB_COMMAND', 'SUB_COMMAND_GROUP'], ['options'], false);
return option ? new CommandInteractionOptionResolver(option.options) : null;
}
/**
* Gets a boolean option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {boolean|null} The value of the option, or null if not set and not required.
*/
getBoolean(name, required = false) {
const option = this._getTypedOption(name, ['BOOLEAN'], ['value'], required);
return option?.value ?? null;
}
/**
* Gets a channel option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {GuildChannel|APIInteractionDataResolvedChannel|null}
* The value of the option, or null if not set and not required.
*/
getChannel(name, required = false) {
const option = this._getTypedOption(name, ['CHANNEL'], ['channel'], required);
return option?.channel ?? null;
}
/**
* Gets a string option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {string|null} The value of the option, or null if not set and not required.
*/
getString(name, required = false) {
const option = this._getTypedOption(name, ['STRING'], ['value'], required);
return option?.value ?? null;
}
/**
* Gets an integer option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {number|null} The value of the option, or null if not set and not required.
*/
getInteger(name, required = false) {
const option = this._getTypedOption(name, ['INTEGER'], ['value'], required);
return option?.value ?? null;
}
/**
* Gets a user option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {User|null} The value of the option, or null if not set and not required.
*/
getUser(name, required = false) {
const option = this._getTypedOption(name, ['USER'], ['user'], required);
return option?.user ?? null;
}
/**
* Gets a member option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {GuildMember|APIInteractionDataResolvedGuildMember|null}
* The value of the option, or null if not set and not required.
*/
getMember(name, required = false) {
const option = this._getTypedOption(name, ['MEMBER'], ['member'], required);
return option?.member ?? null;
}
/**
* Gets a role option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {Role|APIRole|null} The value of the option, or null if not set and not required.
*/
getRole(name, required = false) {
const option = this._getTypedOption(name, ['ROLE'], ['role'], required);
return option?.role ?? null;
}
/**
* Gets a mentionable option.
* @param {string} name The name of the option.
* @param {boolean} required Whether to throw an error if the option is not found.
* @returns {User|GuildMember|Role|null} The value of the option, or null if not set and not required.
*/
getMentionable(name, required = false) {
const option = this._getTypedOption(name, ['MENTIONABLE'], ['user', 'member', 'role'], required);
return option?.user ?? option?.member ?? option?.role ?? null;
}
}
module.exports = CommandInteractionOptionResolver;