-
Notifications
You must be signed in to change notification settings - Fork 13
/
CommandDecorators.ts
343 lines (310 loc) · 8.8 KB
/
CommandDecorators.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* eslint-disable func-names */
/* eslint-disable no-param-reassign */
import { PermissionResolvable, MessageOptions } from 'discord.js';
import { ArgOpts } from '../types/ArgOpts';
import { Command } from './Command';
import { CompactModeHelper } from './CompactModeHelper';
import { Lang } from '../localization/Lang';
import { Message } from '../types/Message';
import { MiddlewareFunction } from '../types/MiddlewareFunction';
import { ResourceProxy } from '../types/ResourceProxy';
import { Util } from '../util/Util';
/**
* Grouping of static decorator methods for the {@link Command}
* class and {@link Command#action} method
*
* >**Note:** This is a TypeScript feature. JavaScript users are limited to
* using CommandInfo to define their commands and {@link Command#use} for
* assigning middleware functions to commands
* @module CommandDecorators
*/
/**
* Apply a middleware function to the action method of a Command.
* Identical to {@link Command#use} but used as a Command action
* method decorator
* @static
* @method using
* @param {MiddlewareFunction} func Middleware function to use
* for the decorated Command action
* @returns {MethodDecorator}
*/
export function using(func: MiddlewareFunction): MethodDecorator
{
return function(target: object, key: PropertyKey, descriptor: PropertyDescriptor): PropertyDescriptor
{
if (!target)
throw new Error('@using must be used as a method decorator for a Command action method.');
if (key !== 'action')
throw new Error(
`"${target.constructor.name}#${key as string}" is not a valid method target for @using.`
);
if (!descriptor) descriptor = Object.getOwnPropertyDescriptor(target, key)!;
const original: any = descriptor.value;
descriptor.value = async function(this: Command, message: Message, args: any[]): Promise<any>
{
// Send the middleware result to the channel, utilizing compact mode if enabled
const sendMiddlewareResult: (result: string, options?: MessageOptions) => Promise<any> =
async (result, options) =>
{
if (await message.guild?.storage?.settings.get('compact') || this.client.compact)
{
if (message.reactions.cache.size > 0) await message.reactions.removeAll();
return CompactModeHelper.registerButton(
message,
this.client.buttons.fail,
async () => message.channel.send(result, options!)
);
}
return message.channel.send(result);
};
// Run the given middleware function
let middlewarePassed: boolean = true;
try
{
let result: Promise<[Message, any[]]> | [Message, any[]] =
func.call(this, message, args);
if (result instanceof Promise)
result = await result;
if (!(result instanceof Array))
{
if (typeof result === 'string')
sendMiddlewareResult(result);
middlewarePassed = false;
}
if (middlewarePassed)
// eslint-disable-next-line require-atomic-updates
[message, args] = result;
}
catch (err)
{
sendMiddlewareResult(err.toString(), { split: true });
middlewarePassed = false;
}
if (middlewarePassed)
// eslint-disable-next-line no-return-await
return await original.apply(this, [message, args]);
};
return descriptor;
};
}
/**
* Creates a {@link ResourceProxy} object using the localization
* language for the command call and passes it as the first argument
* for that command call.
*
* Identical to [localize]{@link module:Middleware.localize} but used
* as a Command action method decorator.
*
* Like the `localize` middleware, you will want to use this after
* any other usages of middleware via [@using()]{@link module:CommandDecorators.using}.
* Middleware added via {@link Command#use} is evaluated before
* middleware added via `@using()`.
* @deprecated in favor of [localize]{@link module:Middleware.localize}. Will be removed in a future release
* @static
* @name localizable
* @type {MethodDecorator}
*/
export function localizable(target: Command, key: string, descriptor: PropertyDescriptor): PropertyDescriptor
{
Util.emitDeprecationWarning(localizable,
'`CommandDecorators.localizable` is deprecated. Use `Middleware.localize` instead');
if (!target)
throw new Error('@localizable must be used as a method decorator for a Command action method.');
if (key !== 'action')
throw new Error(
`"${target.constructor.name}#${key}" is not a valid method target for @localizable.`
);
if (!descriptor) descriptor = Object.getOwnPropertyDescriptor(target, key)!;
const original: any = descriptor.value;
descriptor.value = async function(this: Command, message: Message, args: any[]): Promise<any>
{
const lang: string = await Lang.getLangFromMessage(message);
const res: ResourceProxy = Lang.createResourceProxy(lang);
// eslint-disable-next-line no-return-await
return await original.apply(this, [message, [res, ...args]]);
};
return descriptor;
}
/**
* Set an arbitrary value to an arbitrary key on a command class
* @private
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
function _setMetaData(key: string, value: any): ClassDecorator
{
return function<T extends Function>(target: T): T
{
Object.defineProperty(target.prototype, key, {
value,
configurable: true,
enumerable: true,
writable: true
});
return target;
};
}
/**
* Set a boolean flag metadata on a command class
* @private
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
function _setFlagMetaData<T extends Function>(target: T, flag: string): T
{
Object.defineProperty(target.prototype, flag, {
value: true,
enumerable: true,
});
return target;
}
/**
* Set `name` metadata
* @static
* @method name
* @param {string} value Value to set
* @returns {ClassDecorator}
*/
export function name(value: string): ClassDecorator
{
return _setMetaData('name', value);
}
/**
* Set `aliases` metadata
* @static
* @method aliases
* @param {...string} values Values to set
* @returns {ClassDecorator}
*/
export function aliases(...values: string[]): ClassDecorator
{
return _setMetaData('aliases', values);
}
/**
* Set `desc` metadata
* @static
* @method desc
* @param {string} value Value to set
* @returns {ClassDecorator}
*/
export function desc(value: string): ClassDecorator
{
return _setMetaData('desc', value);
}
/**
* Set `usage` metadata
* @static
* @method usage
* @param {string} value Value to set
* @returns {ClassDecorator}
*/
export function usage(value: string): ClassDecorator
{
return _setMetaData('usage', value);
}
/**
* Set `info` metadata
* @static
* @method info
* @param {string} value Value to set
* @returns {ClassDecorator}
*/
export function info(value: string): ClassDecorator
{
return _setMetaData('info', value);
}
/**
* Set `group` metadata
* @static
* @method group
* @param {string} value Value to set
* @returns {ClassDecorator}
*/
export function group(value: string): ClassDecorator
{
return _setMetaData('group', value);
}
/**
* Set `argOpts` metadata
* @static
* @method argOpts
* @param {ArgOpts} value Value to set
* @returns {ClassDecorator}
*/
export function argOpts(value: ArgOpts): ClassDecorator
{
return _setMetaData('usage', value);
}
/**
* Set `callerPermissions` metadata
* @static
* @method callerPermissions
* @param {...external:PermissionResolvable} values Values to set
* @returns {ClassDecorator}
*/
export function callerPermissions(...values: PermissionResolvable[]): ClassDecorator
{
return _setMetaData('callerPermissions', values);
}
/**
* Set `clientPermissions` metadata
* @static
* @method clientPermissions
* @param {...external:PermissionResolvable} values Values to set
* @returns {ClassDecorator}
*/
export function clientPermissions(...values: PermissionResolvable[]): ClassDecorator
{
return _setMetaData('clientPermissions', values);
}
/**
* Set `roles` metadata
* @static
* @method roles
* @param {...string} values Values to set
* @returns {ClassDecorator}
*/
export function roles(...values: string[]): ClassDecorator
{
return _setMetaData('roles', values);
}
/**
* Set `ratelimit` metadata
* @static
* @method ratelimit
* @param {string} value Value to set
* @returns {ClassDecorator}
*/
export function ratelimit(value: string): ClassDecorator
{
Util.parseRateLimit(value);
return _setMetaData('_ratelimit', value);
}
/**
* Set `owneronly` flag metadata
* @static
* @name ownerOnly
* @type {ClassDecorator}
*/
export function ownerOnly<T extends Function>(target: T): T
{
return _setFlagMetaData(target, 'ownerOnly');
}
/**
* Set `guildOnly` flag metadata
* @static
* @name guildOnly
* @type {ClassDecorator}
*/
export function guildOnly<T extends Function>(target: T): T
{
return _setFlagMetaData(target, 'guildOnly');
}
/**
* Set `hidden` flag metadata
* @static
* @name hidden
* @type {ClassDecorator}
*/
export function hidden<T extends Function>(target: T): T
{
return _setFlagMetaData(target, 'hidden');
}