@zygout/eris 0.0.0-stage → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +80 -3
  3. package/esm.mjs +72 -0
  4. package/index.d.ts +4397 -0
  5. package/index.js +75 -0
  6. package/lib/Client.js +4683 -0
  7. package/lib/Constants.d.ts +988 -0
  8. package/lib/Constants.js +1161 -0
  9. package/lib/command/Command.js +552 -0
  10. package/lib/command/CommandClient.js +460 -0
  11. package/lib/errors/DiscordHTTPError.js +63 -0
  12. package/lib/errors/DiscordRESTError.js +77 -0
  13. package/lib/gateway/Shard.js +3029 -0
  14. package/lib/gateway/ShardManager.js +164 -0
  15. package/lib/rest/Endpoints.js +172 -0
  16. package/lib/rest/RequestHandler.js +459 -0
  17. package/lib/structures/ApplicationCommand.js +125 -0
  18. package/lib/structures/AutoModerationRule.js +143 -0
  19. package/lib/structures/AutocompleteInteraction.js +80 -0
  20. package/lib/structures/Base.js +81 -0
  21. package/lib/structures/CategoryChannel.js +77 -0
  22. package/lib/structures/Channel.js +98 -0
  23. package/lib/structures/CommandInteraction.js +473 -0
  24. package/lib/structures/ComponentInteraction.js +538 -0
  25. package/lib/structures/DMChannel.js +278 -0
  26. package/lib/structures/Entitlement.js +77 -0
  27. package/lib/structures/ExtendedUser.js +46 -0
  28. package/lib/structures/ForumChannel.js +24 -0
  29. package/lib/structures/GroupChannel.js +106 -0
  30. package/lib/structures/Guild.js +1677 -0
  31. package/lib/structures/GuildAuditLogEntry.js +190 -0
  32. package/lib/structures/GuildChannel.js +128 -0
  33. package/lib/structures/GuildIntegration.js +99 -0
  34. package/lib/structures/GuildPreview.js +97 -0
  35. package/lib/structures/GuildScheduledEvent.js +176 -0
  36. package/lib/structures/GuildTemplate.js +76 -0
  37. package/lib/structures/GuildTextableChannel.js +260 -0
  38. package/lib/structures/Interaction.js +133 -0
  39. package/lib/structures/Invite.js +143 -0
  40. package/lib/structures/Lobby.js +108 -0
  41. package/lib/structures/LobbyMember.js +59 -0
  42. package/lib/structures/MediaChannel.js +216 -0
  43. package/lib/structures/Member.js +371 -0
  44. package/lib/structures/Message.js +708 -0
  45. package/lib/structures/ModalSubmitInteraction.js +478 -0
  46. package/lib/structures/NewsChannel.js +34 -0
  47. package/lib/structures/NewsThreadChannel.js +12 -0
  48. package/lib/structures/Permission.js +80 -0
  49. package/lib/structures/PermissionOverwrite.js +32 -0
  50. package/lib/structures/PingInteraction.js +39 -0
  51. package/lib/structures/PrivateThreadChannel.js +25 -0
  52. package/lib/structures/PublicThreadChannel.js +26 -0
  53. package/lib/structures/Role.js +163 -0
  54. package/lib/structures/SKU.js +80 -0
  55. package/lib/structures/SoundboardSound.js +79 -0
  56. package/lib/structures/StageChannel.js +59 -0
  57. package/lib/structures/StageInstance.js +55 -0
  58. package/lib/structures/Subscription.js +69 -0
  59. package/lib/structures/TextChannel.js +202 -0
  60. package/lib/structures/ThreadChannel.js +143 -0
  61. package/lib/structures/ThreadMember.js +59 -0
  62. package/lib/structures/UnavailableGuild.js +26 -0
  63. package/lib/structures/UnknownInteraction.js +502 -0
  64. package/lib/structures/User.js +296 -0
  65. package/lib/structures/VoiceChannel.js +166 -0
  66. package/lib/structures/VoiceState.js +84 -0
  67. package/lib/util/BrowserWebSocket.js +76 -0
  68. package/lib/util/Bucket.js +89 -0
  69. package/lib/util/Collection.js +202 -0
  70. package/lib/util/MultipartData.js +70 -0
  71. package/lib/util/Opus.js +33 -0
  72. package/lib/util/SequentialBucket.js +83 -0
  73. package/lib/util/emitDeprecation.js +26 -0
  74. package/lib/voice/Piper.js +300 -0
  75. package/lib/voice/SharedStream.js +238 -0
  76. package/lib/voice/VoiceConnection.js +1440 -0
  77. package/lib/voice/VoiceConnectionManager.js +151 -0
  78. package/lib/voice/VoiceDataStream.js +22 -0
  79. package/lib/voice/streams/BaseTransformer.js +40 -0
  80. package/lib/voice/streams/DCAOpusTransformer.js +78 -0
  81. package/lib/voice/streams/FFmpegDuplex.js +192 -0
  82. package/lib/voice/streams/FFmpegOggTransformer.js +35 -0
  83. package/lib/voice/streams/FFmpegPCMTransformer.js +26 -0
  84. package/lib/voice/streams/OggOpusTransformer.js +108 -0
  85. package/lib/voice/streams/PCMOpusTransformer.js +61 -0
  86. package/lib/voice/streams/VolumeTransformer.js +50 -0
  87. package/lib/voice/streams/WebmOpusTransformer.js +258 -0
  88. package/package.json +82 -6
@@ -0,0 +1,552 @@
1
+ "use strict";
2
+
3
+ const Base = require("../structures/Base");
4
+
5
+ /**
6
+ * Represents an command framework command
7
+ * @prop {Array<String>} aliases An array of command aliases
8
+ * @prop {Boolean} argsRequired If arguments are required or not
9
+ * @prop {Boolean} caseInsensitive Whether the command label (and aliases) is case insensitive or not
10
+ * @prop {Number} cooldown The cooldown between command usage in milliseconds
11
+ * @prop {Object} cooldownExclusions A set of factors that limit where cooldowns are active
12
+ * @prop {Function | String} cooldownMessage A string or a function that returns a string to show when the command is on cooldown.
13
+ * @prop {Number} cooldownReturns Number of times to return a message when the command is used during it's cooldown.
14
+ * @prop {Object} defaultSubcommandOptions Default subcommand options.
15
+ * @prop {Boolean} deleteCommand Whether to delete the user command message or not
16
+ * @prop {String} description A short description of the command
17
+ * @prop {Boolean} dmOnly Whether the command is prevented from being used in guilds or not
18
+ * @prop {Function | String} errorMessage A string or a function that returns a string to show if the execution of the command handler somehow fails. The function is passed the Message object as a parameter.
19
+ * @prop {String} fullDescription A detailed description of the command
20
+ * @prop {String} fullLabel The full command label
21
+ * @prop {Boolean} guildOnly Whether the command is prevented from being used in Direct Messages or not
22
+ * @prop {Boolean} hidden Whether or not the command is hidden from the default help command list.
23
+ * @prop {Object} hooks A set of functions to be executed at different times throughout the command's processing
24
+ * @prop {Function | String} invalidUsageMessage A string or a function that returns a string to show when a command was improperly used.
25
+ * @prop {String} label The command label
26
+ * @prop {Command?} parentCommand If this command is also a subcommand, this will refer to its parent Command
27
+ * @prop {Function | String} permissionMessage A string or a function that returns a string to show when the user doesn't have permissions to use the command.
28
+ * @prop {Object?} reactionButtons An array of objects specifying reaction buttons, or null if no reaction buttons.
29
+ * @prop {Number} reactionButtonTimeout Time (in milliseconds) to wait before invalidating the command's reaction buttons
30
+ * @prop {Object} requirements A set of factors that limit who can call the command
31
+ * @prop {Boolean} restartCooldown Whether or not to restart a command's cooldown every time it's used
32
+ * @prop {Object} subcommands Object mapping subcommand labels to Command objects
33
+ * @prop {Object} subcommandAliases Object mapping subcommand aliases to their respective subcommand label
34
+ * @prop {String} usage Details on how to call the command to show in the default help command
35
+ */
36
+ class Command {
37
+ /**
38
+ * Register a command
39
+ * @arg {String} label The command label
40
+ * @arg {Function | String | Array<Function | String>} generator A response string, array of functions or strings, or function that generates a string or array of strings when called.
41
+ * If a function is passed, the function will be passed a Message object and an array of command arguments. The Message object will have an additional property `prefix`, which is the prefix used in the command.
42
+ * `generator(msg, args)`
43
+ * @arg {Object} [options] Command options
44
+ * @arg {Array<String>} [options.aliases] An array of command aliases
45
+ * @arg {Boolean} [options.argsRequired=false] If arguments are required or not
46
+ * @arg {Boolean} [options.caseInsensitive=false] Whether the command label (and aliases) is case insensitive or not
47
+ * @arg {Number} [options.cooldown] The cooldown between command usage in milliseconds
48
+ * @arg {Object} [options.cooldownExclusions={}] A set of factors that limit where cooldowns are active
49
+ * @arg {Array<String>} [options.cooldownExclusions.channelIDs] An array of channel IDs representing channels that are not affected by cooldowns.
50
+ * @arg {Array<String>} [options.cooldownExclusions.guildIDs] An array of guild IDs representing guilds that are not affected by cooldowns.
51
+ * @arg {Array<String>} [options.cooldownExclusions.userIDs] An array of user IDs representing users that are not affected by cooldowns.
52
+ * @arg {Function | String | Object} [options.cooldownMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the command is on cooldown. The function is passed the Message object as a parameter.
53
+ * @arg {Number} [option.cooldownReturns=0] Number of times to return a message when the command is used during it's cooldown. Once the cooldown expires this is reset. Set this to 0 to always return a message.
54
+ * @arg {Object} [options.defaultSubcommandOptions={}] Default subcommand options. This object takes the same options as a normal Command
55
+ * @arg {Boolean} [options.deleteCommand=false] Whether to delete the user command message or not
56
+ * @arg {String} [options.description="No description"] A short description of the command to show in the default help command
57
+ * @arg {Boolean} [options.dmOnly=false] Whether to prevent the command from being used in guilds or not
58
+ * @arg {Function | String | Object} [options.errorMessage] A string or `createMessage()` content object, or a function that returns either of those, to show if the execution of the command handler somehow fails. The function is passed the Message object as a parameter.
59
+ * @arg {String} [options.fullDescription="No full description"] A detailed description of the command to show in the default help command
60
+ * @arg {Boolean} [options.guildOnly=false] Whether to prevent the command from being used in Direct Messages or not
61
+ * @arg {Boolean} [options.hidden=false] Whether or not the command should be hidden from the default help command list.
62
+ * @arg {Object} [options.hooks] A set of functions to be executed at different times throughout the command's processing
63
+ * @arg {Function} [options.hooks.postCheck] A function that is executed after all checks have cleared, but before the command is executed. The function is passed the command message, arguments, and if command checks were passed as parameters.
64
+ * @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
65
+ * @arg {Function} [options.hooks.postExecution] A function that is executed after the command is executed, regardless of the final failed state of the command. The function is passed the command message, arguments, and if execution succeeded as parameters.
66
+ * @arg {Function} [options.hooks.preCommand] A function that is executed before any permission or cooldown checks is made. The function is passed the command message and arguments as parameters.
67
+ * @arg {Function | String | Object} [options.invalidUsageMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when a command was improperly used. The function is passed the Message object as a parameter.
68
+ * @arg {Function | String | Object} [options.permissionMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the user doesn't have permissions to use the command. The function is passed the Message object as a parameter.
69
+ * @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>), filter: Function}>} [options.reactionButtons] An array of objects specifying reaction buttons
70
+ * `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
71
+ * `type` specifies the type of the reaction button, either "edit" or "cancel"
72
+ * `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
73
+ * `filter` specifies a function (`function(msg, emoji, userID)`) that filters message reactions. If the function returns false, the reaction is not treated as a valid reaction button response
74
+ * @arg {Number} [options.reactionButtonTimeout=60000] Time (in milliseconds) to wait before invalidating the command's reaction buttons
75
+ * @arg {Object} [options.requirements] A set of factors that limit who can call the command
76
+ * @arg {Function} [options.requirements.custom] A function that accepts a message and returns true if the command should be run
77
+ * @arg {Function | Object} [options.requirements.permissions] An object or a function that returns an object containing permission keys the user must match to use the command. The function is passed the Message object as a parameter.
78
+ * i.e.:
79
+ * ```
80
+ * {
81
+ * "administrator": false,
82
+ * "manageMessages": true
83
+ * }
84
+ * ```
85
+ * In the above example, the user must not have administrator permissions, but must have manageMessages to use the command
86
+ * @arg {Function | Array<String>} [options.requirements.roleIDs] An array or a function that returns an array of role IDs that would allow a user to use the command. The function is passed the Message object as a parameter.
87
+ * @arg {Function | Array<String>} [options.requirements.roleNames] An array or a function that returns an array of role names that would allow a user to use the command. The function is passed the Message object as a parameter.
88
+ * @arg {Function | Array<String>} [options.requirements.userIDs] An array or a function that returns an array of user IDs representing users that can call the command. The function is passed the Message object as a parameter.
89
+ * @arg {Boolean} [option.restartCooldown=false] Whether or not to restart a command's cooldown every time it's used.
90
+ * @arg {String} [options.usage] Details on how to call the command to show in the default help command
91
+ */
92
+ constructor(label, generator, options, parentCommand) {
93
+ this.parentCommand = parentCommand;
94
+ this.label = label;
95
+ this.description = options.description || "No description";
96
+ this.fullDescription = options.fullDescription || "No full description";
97
+ this.usage = options.usage || "";
98
+ this.aliases = options.aliases || [];
99
+ this.caseInsensitive = !!options.caseInsensitive;
100
+ this.hooks = options.hooks || {};
101
+ this.requirements = options.requirements || {};
102
+ if (!this.requirements.userIDs) {
103
+ this.requirements.userIDs = [];
104
+ }
105
+ if (!this.requirements.permissions) {
106
+ this.requirements.permissions = {};
107
+ }
108
+ this.deleteCommand = !!options.deleteCommand;
109
+ this.argsRequired = !!options.argsRequired;
110
+ this.guildOnly = !!options.guildOnly;
111
+ this.dmOnly = !!options.dmOnly;
112
+ this.cooldown = options.cooldown || 0;
113
+ this.cooldownExclusions = options.cooldownExclusions || {};
114
+ if (!this.cooldownExclusions.userIDs) {
115
+ this.cooldownExclusions.userIDs = [];
116
+ }
117
+ if (!this.cooldownExclusions.guildIDs) {
118
+ this.cooldownExclusions.guildIDs = [];
119
+ }
120
+ if (!this.cooldownExclusions.channelIDs) {
121
+ this.cooldownExclusions.channelIDs = [];
122
+ }
123
+ this.restartCooldown = !!options.restartCooldown;
124
+ this.cooldownReturns = options.cooldownReturns || 0;
125
+ this.cooldownMessage = options.cooldownMessage || false;
126
+ this.invalidUsageMessage = options.invalidUsageMessage || false;
127
+ this.permissionMessage = options.permissionMessage || false;
128
+ this.errorMessage = options.errorMessage || "";
129
+ this.reactionButtons = options.reactionButtons
130
+ ? options.reactionButtons.map((button, index) => {
131
+ if (typeof button.response === "string") {
132
+ button.execute = () => button.response;
133
+ return button;
134
+ } else if (Array.isArray(button.response)) {
135
+ button.responses = button.response.map((item, otherIndex) => {
136
+ if (typeof item === "string") {
137
+ return () => item;
138
+ } else if (typeof item === "function") {
139
+ return item;
140
+ } else {
141
+ throw new Error(`Invalid reaction button response generator (index ${index}:${otherIndex})`);
142
+ }
143
+ });
144
+ button.execute = () => button.responses[Math.floor(Math.random() * button.responses.length)]();
145
+ return button;
146
+ } else if (typeof button.response === "function") {
147
+ button.execute = button.response;
148
+ return button;
149
+ } else if (button.type === "cancel") {
150
+ return button;
151
+ } else {
152
+ throw new Error(`Invalid reaction button response generator (index ${index})`);
153
+ }
154
+ })
155
+ : null;
156
+ this.reactionButtonTimeout = options.reactionButtonTimeout || 60000;
157
+ if (this.cooldown !== 0) {
158
+ this.usersOnCooldown = new Set();
159
+ if (this.restartCooldown) {
160
+ this.cooldownTimeouts = {};
161
+ }
162
+ if (this.cooldownReturns) {
163
+ this.cooldownAmounts = {};
164
+ }
165
+ }
166
+ if (typeof generator === "string") {
167
+ this.response = generator;
168
+ this.execute = () => this.response;
169
+ } else if (Array.isArray(generator)) {
170
+ this.responses = generator.map((item, index) => {
171
+ if (typeof item === "string") {
172
+ return () => item;
173
+ } else if (typeof item === "function") {
174
+ return item;
175
+ } else {
176
+ throw new Error(`Invalid command response generator (index ${index})`);
177
+ }
178
+ });
179
+ this.execute = () => this.responses[Math.floor(Math.random() * this.responses.length)]();
180
+ } else if (typeof generator === "function") {
181
+ this.execute = generator;
182
+ } else {
183
+ throw new Error("Invalid command response generator");
184
+ }
185
+ this.defaultSubcommandOptions = options.defaultSubcommandOptions || {};
186
+
187
+ this.subcommands = {};
188
+ this.subcommandAliases = {};
189
+ this.hidden = !!options.hidden;
190
+ }
191
+
192
+ get fullLabel() {
193
+ return `${this.parentCommand ? this.parentCommand.fullLabel + " " : ""}${this.label}`;
194
+ }
195
+
196
+ cooldownCheck(msg) {
197
+ // Verify the msg isn't excluded from cooldown checks
198
+ if (this.cooldownExclusionCheck(msg)) {
199
+ return true;
200
+ }
201
+
202
+ const userID = msg.author.id;
203
+ if (this.usersOnCooldown.has(userID)) {
204
+ if (this.cooldownReturns) {
205
+ this.cooldownAmounts[userID]++;
206
+ }
207
+ if (this.restartCooldown) {
208
+ clearTimeout(this.cooldownTimeouts[userID]);
209
+ this.cooldownTimeouts[userID] = setTimeout(() => {
210
+ this.usersOnCooldown.delete(userID);
211
+ }, this.cooldown);
212
+ }
213
+ return false;
214
+ }
215
+ if (this.cooldownReturns) {
216
+ this.cooldownAmounts[userID] = 0;
217
+ }
218
+ this.usersOnCooldown.add(userID);
219
+ if (this.restartCooldown) {
220
+ this.cooldownTimeouts[userID] = setTimeout(() => {
221
+ this.usersOnCooldown.delete(userID);
222
+ }, this.cooldown);
223
+ } else {
224
+ setTimeout(() => {
225
+ this.usersOnCooldown.delete(userID);
226
+ }, this.cooldown);
227
+ }
228
+ return true;
229
+ }
230
+
231
+ cooldownExclusionCheck(msg) {
232
+ return this.cooldownExclusions.channelIDs.includes(msg.channel.id) || this.cooldownExclusions.userIDs.includes(msg.author.id) || (msg.channel.guild && this.cooldownExclusions.guildIDs.includes(msg.channel.guild.id));
233
+ }
234
+
235
+ async executeCommand(msg, args) {
236
+ if (this.hooks.postCheck) {
237
+ const response = await this.hooks.postCheck(msg, args, true);
238
+ if (response) {
239
+ msg = response.msg || msg;
240
+ args = response.args || args;
241
+ }
242
+ }
243
+
244
+ const ret = this.execute(msg, args);
245
+
246
+ if (this.hooks.postExecution) {
247
+ this.hooks.postExecution(msg, args, true);
248
+ }
249
+
250
+ return ret;
251
+ }
252
+
253
+ async permissionCheck(msg) {
254
+ if (this.requirements.custom) {
255
+ if (typeof this.requirements.custom !== "function") {
256
+ throw new Error("Custom requirement is not a function");
257
+ }
258
+ if (!(await this.requirements.custom(msg))) {
259
+ return false;
260
+ }
261
+ }
262
+ if (this.requirements.userIDs) {
263
+ const userIDs = typeof this.requirements.userIDs === "function" ? await this.requirements.userIDs(msg) : this.requirements.userIDs;
264
+ if (!Array.isArray(userIDs)) {
265
+ throw new Error("User IDs requirement is not an array");
266
+ }
267
+ if (userIDs.length > 0 && !userIDs.includes(msg.author.id)) {
268
+ return false;
269
+ }
270
+ }
271
+ if (msg.channel.guild) {
272
+ if (this.dmOnly) {
273
+ return false;
274
+ }
275
+ if (this.requirements.permissions) {
276
+ const requiredPermissions = Object.keys(typeof this.requirements.permissions === "function" ? await this.requirements.permissions(msg) : this.requirements.permissions);
277
+ if (requiredPermissions.length > 0) {
278
+ const permissions = msg.channel.permissionsOf(msg.author.id);
279
+ for (const permission of requiredPermissions) {
280
+ if (!permissions.has(permission)) {
281
+ return false;
282
+ }
283
+ }
284
+ }
285
+ }
286
+ const roleIDs = msg.member.roles || [];
287
+ if (this.requirements.roleIDs) {
288
+ const requiredRoleIDs = typeof this.requirements.roleIDs === "function" ? await this.requirements.roleIDs(msg) : this.requirements.roleIDs;
289
+ if (!Array.isArray(requiredRoleIDs)) {
290
+ throw new Error("Role IDs requirement is not an array");
291
+ }
292
+ for (const roleID of requiredRoleIDs) {
293
+ if (!roleIDs.includes(roleID)) {
294
+ return false;
295
+ }
296
+ }
297
+ }
298
+ if (this.requirements.roleNames) {
299
+ const roleNames = roleIDs.map((roleID) => msg.channel.guild.roles.get(roleID).name);
300
+ const requiredRoleNames = typeof this.requirements.roleNames === "function" ? await this.requirements.roleNames(msg) : this.requirements.roleNames;
301
+ if (!Array.isArray(roleNames)) {
302
+ throw new Error("Role names requirement is not an array");
303
+ }
304
+ for (const roleName of requiredRoleNames) {
305
+ if (!roleNames.includes(roleName)) {
306
+ return false;
307
+ }
308
+ }
309
+ }
310
+ } else if (this.guildOnly) {
311
+ return false;
312
+ }
313
+ return true;
314
+ }
315
+
316
+ async process(args, msg) {
317
+ const shouldDelete = this.deleteCommand && msg.channel.guild && msg.channel.permissionsOf(msg._client.user.id).has("manageMessages");
318
+
319
+ if (this.hooks.preCommand) {
320
+ const response = await this.hooks.preCommand(msg, args);
321
+ if (response) {
322
+ msg = response.msg || msg;
323
+ args = response.args || args;
324
+ }
325
+ }
326
+
327
+ let reply;
328
+ if (this.cooldown !== 0 && !this.cooldownCheck(msg)) {
329
+ if (this.hooks.postCheck) {
330
+ const response = await this.hooks.postCheck(msg, args, true);
331
+ if (response) {
332
+ msg = response.msg || msg;
333
+ args = response.args || args;
334
+ }
335
+ }
336
+ if (this.cooldownMessage && (!this.cooldownReturns || this.cooldownAmounts[msg.author.id] <= this.cooldownReturns)) {
337
+ reply = typeof this.cooldownMessage === "function" ? await this.cooldownMessage(msg) : this.cooldownMessage;
338
+ if (reply) {
339
+ msg.channel.createMessage(reply);
340
+ }
341
+ }
342
+ return;
343
+ }
344
+ if (!await this.permissionCheck(msg)) {
345
+ if (this.hooks.postCheck) {
346
+ const response = await this.hooks.postCheck(msg, args, false);
347
+ if (response) {
348
+ msg = response.msg || msg;
349
+ args = response.args || args;
350
+ }
351
+ }
352
+
353
+ if (shouldDelete) {
354
+ msg.delete();
355
+ }
356
+ reply = typeof this.permissionMessage === "function" ? await this.permissionMessage(msg) : this.permissionMessage;
357
+ if (reply) {
358
+ msg.channel.createMessage(reply);
359
+ }
360
+ return;
361
+ }
362
+ if (args.length === 0) {
363
+ if (shouldDelete) {
364
+ msg.delete();
365
+ }
366
+ if (this.argsRequired) {
367
+ if (this.hooks.postCheck) {
368
+ const response = await this.hooks.postCheck(msg, args, true);
369
+ if (response) {
370
+ msg = response.msg || msg;
371
+ args = response.args || args;
372
+ }
373
+ }
374
+ reply = typeof this.invalidUsageMessage === "function" ? await this.invalidUsageMessage(msg) : this.invalidUsageMessage;
375
+ if (reply) {
376
+ if (typeof reply === "string") {
377
+ reply = {
378
+ content: reply,
379
+ };
380
+ }
381
+ if (reply.content) {
382
+ reply.content = reply.content.replace(/%prefix%/g, msg.prefix).replace(/%label%/g, this.fullLabel);
383
+ }
384
+ msg.channel.createMessage(reply);
385
+ }
386
+ return;
387
+ }
388
+ return this.executeCommand(msg, args);
389
+ }
390
+ const label = this.subcommandAliases[args[0]] || args[0];
391
+ let subcommand;
392
+ if ((subcommand = this.subcommands[label]) !== undefined || ((subcommand = this.subcommands[label.toLowerCase()]) !== undefined && subcommand.caseInsensitive)) {
393
+ msg.command = subcommand; // eslint-disable-line require-atomic-updates
394
+ return subcommand.process(args.slice(1), msg);
395
+ } else {
396
+ if (shouldDelete) {
397
+ msg.delete();
398
+ }
399
+ return this.executeCommand(msg, args);
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Register a subcommand
405
+ * @arg {String} label The subcommand label
406
+ * @arg {Function | String | Array<Function | String>} generator A response string, array of functions or strings, or function that generates a string or array of strings when called.
407
+ * If a function is passed, the function will be passed a Message object and an array of subcommand arguments. The Message object will have an additional property `prefix`, which is the prefix used in the subcommand.
408
+ * `generator(msg, args)`
409
+ * @arg {Object} [options] Command options
410
+ * @arg {Array<String>} [options.aliases] An array of command aliases
411
+ * @arg {Boolean} [options.argsRequired=false] If arguments are required or not
412
+ * @arg {Boolean} [options.caseInsensitive=false] Whether the command label (and aliases) is case insensitive or not
413
+ * @arg {Number} [options.cooldown] The cooldown between command usage in milliseconds
414
+ * @arg {Object} [options.cooldownExclusions={}] A set of factors that limit where cooldowns are active
415
+ * @arg {Array<String>} [options.cooldownExclusions.channelIDs] An array of channel IDs representing channels that are not affected by cooldowns.
416
+ * @arg {Array<String>} [options.cooldownExclusions.guildIDs] An array of guild IDs representing guilds that are not affected by cooldowns.
417
+ * @arg {Array<String>} [options.cooldownExclusions.userIDs] An array of user IDs representing users that are not affected by cooldowns.
418
+ * @arg {Function | String | Object} [options.cooldownMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the command is on cooldown
419
+ * @arg {Number} [option.cooldownReturns=0] Number of times to return a message when the command is used during it's cooldown. Once the cooldown expires this is reset. Set this to 0 to always return a message.
420
+ * @arg {Object} [options.defaultSubcommandOptions={}] Default subcommand options. This object takes the same options as a normal Command
421
+ * @arg {Boolean} [options.deleteCommand=false] Whether to delete the user command message or not
422
+ * @arg {String} [options.description="No description"] A short description of the command to show in the default help command
423
+ * @arg {Boolean} [options.dmOnly=false] Whether to prevent the command from being used in guilds or not
424
+ * @arg {Function | String | Object} [options.errorMessage] A string or `createMessage()` content object, or a function that returns either of those, to show if the execution of the command handler somehow fails.
425
+ * @arg {String} [options.fullDescription="No full description"] A detailed description of the command to show in the default help command
426
+ * @arg {Boolean} [options.guildOnly=false] Whether to prevent the command from being used in Direct Messages or not
427
+ * @arg {Boolean} [options.hidden=false] Whether or not the command should be hidden from the default help command list
428
+ * @arg {Object} [options.hooks] A set of functions to be executed at different times throughout the command's processing
429
+ * @arg {Function} [options.hooks.postCheck] A function that is executed after all checks have cleared, but before the command is executed. The function is passed the command message, arguments, and if command checks were passed as parameters.
430
+ * @arg {Function} [options.hooks.postCommand] A function that is executed after a response has been posted, and the command has finished processing. The function is passed the command message, arguments, and the response message (if applicable) as parameters.
431
+ * @arg {Function} [options.hooks.postExecution] A function that is executed after the command is executed, regardless of the final failed state of the command. The function is passed the command message, arguments, and if execution succeeded as parameters.
432
+ * @arg {Function} [options.hooks.preCommand] A function that is executed before any permission or cooldown checks is made. The function is passed the command message and arguments as parameters.
433
+ * @arg {Function | String | Object} [options.invalidUsageMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when a command was improperly used
434
+ * @arg {Function | String | Object} [options.permissionMessage] A string or `createMessage()` content object, or a function that returns either of those, to show when the user doesn't have permissions to use the command
435
+ * @arg {Array<{emoji: String, type: String, response: (Function | String | Array<Function | String>)}>} [options.reactionButtons] An array of objects specifying reaction buttons
436
+ * `emoji` specifies the button emoji. Custom emojis should be in format `emojiName:emojiID`
437
+ * `type` specifies the type of the reaction button, either "edit" or "cancel"
438
+ * `response` specifies the content to edit the message to when the reaction button is pressed. This accepts the same arguments as the `generator` parameter of this function, but with an extra userID parameter for generator functions (`function(msg, args, userID)`) describing the user that made the reaction
439
+ * `filter` specifies a function (`function(msg, emoji, userID)`) that filters message reactions. If the function returns false, the reaction is not treated as a valid reaction button response
440
+ * @arg {Number} [options.reactionButtonTimeout=60000] Time (in milliseconds) to wait before invalidating the command's reaction buttons
441
+ * @arg {Object} [options.requirements] A set of factors that limit who can call the command
442
+ * @arg {Function} [options.requirements.custom] A function that accepts a message and returns true if the command should be run
443
+ * @arg {Function | Object} [options.requirements.permissions] An object or a function that returns an object containing permission keys the user must match to use the command
444
+ * i.e.:
445
+ * ```
446
+ * {
447
+ * "administrator": false,
448
+ * "manageMessages": true
449
+ * }
450
+ * ```
451
+ * In the above example, the user must not have administrator permissions, but must have manageMessages to use the command
452
+ * @arg {Function | Array<String>} [options.requirements.roleIDs] An array or a function that returns an array of role IDs that would allow a user to use the command
453
+ * @arg {Function | Array<String>} [options.requirements.roleNames] An array or a function that returns an array of role names that would allow a user to use the command
454
+ * @arg {Function | Array<String>} [options.requirements.userIDs] An array or a function that returns an array of user IDs representing users that can call the command
455
+ * @arg {Boolean} [option.restartCooldown=false] Whether or not to restart a command's cooldown every time it's used.
456
+ * @arg {String} [options.usage] Details on how to call the command to show in the default help command
457
+ * @returns {Command}
458
+ */
459
+ registerSubcommand(label, generator, options = {}) {
460
+ if (label.includes(" ")) {
461
+ throw new Error("Subcommand label may not have spaces");
462
+ }
463
+ if (this.subcommands[label]) {
464
+ throw new Error("You have already registered a subcommand for " + label);
465
+ }
466
+ options.defaultSubcommandOptions = options.defaultSubcommandOptions || {};
467
+ for (const key in this.defaultSubcommandOptions) {
468
+ if (this.defaultSubcommandOptions.hasOwnProperty(key) && options[key] === undefined) {
469
+ options[key] = this.defaultSubcommandOptions[key];
470
+ options.defaultSubcommandOptions[key] = this.defaultSubcommandOptions[key];
471
+ }
472
+ }
473
+ label = options.caseInsensitive === true ? label.toLowerCase() : label;
474
+ this.subcommands[label] = new Command(label, generator, options, this);
475
+ if (options.aliases) {
476
+ options.aliases.forEach((alias) => {
477
+ this.subcommandAliases[alias] = label;
478
+ });
479
+ }
480
+ return this.subcommands[label];
481
+ }
482
+
483
+ /**
484
+ * Register an alias for a subcommand
485
+ * @arg {String} alias The alias
486
+ * @arg {String} label The original subcommand label
487
+ */
488
+ registerSubcommandAlias(alias, label) {
489
+ if (!this.subcommands[label]) {
490
+ throw new Error("No subcommand registered for " + label);
491
+ }
492
+ if (this.subcommandAliases[alias]) {
493
+ throw new Error(`Alias ${label} already registered`);
494
+ }
495
+ this.subcommandAliases[alias] = label;
496
+ this.subcommands[label].aliases.push(alias);
497
+ }
498
+
499
+ /**
500
+ * Unregister a subcommand
501
+ * @arg {String} label The subcommand label
502
+ */
503
+ unregisterSubcommand(label) {
504
+ const original = this.subcommandAliases[label];
505
+ if (original) {
506
+ this.subcommands[original].aliases.splice(this.subcommands[original].aliases.indexOf(label), 1);
507
+ delete this.subcommandAliases[label];
508
+ } else {
509
+ delete this.subcommands[label];
510
+ }
511
+ }
512
+
513
+ toString() {
514
+ return `[Command ${this.label}]`;
515
+ }
516
+
517
+ toJSON(props = []) {
518
+ return Base.prototype.toJSON.call(this, [
519
+ "aliases",
520
+ "argsRequired",
521
+ "caseInsensitive",
522
+ "cooldown",
523
+ "cooldownExclusions",
524
+ "cooldownMessage",
525
+ "cooldownReturns",
526
+ "defaultSubcommandOptions",
527
+ "deleteCommand",
528
+ "description",
529
+ "dmOnly",
530
+ "errorMessage",
531
+ "execute",
532
+ "fullDescription",
533
+ "guildOnly",
534
+ "hidden",
535
+ "hooks",
536
+ "invalidUsageMessage",
537
+ "label",
538
+ "parentCommand",
539
+ "permissionMessage",
540
+ "reactionButtons",
541
+ "reactionButtonTimeout",
542
+ "requirements",
543
+ "restartCooldown",
544
+ "subcommands",
545
+ "subcommandAliases",
546
+ "usage",
547
+ ...props,
548
+ ]);
549
+ }
550
+ }
551
+
552
+ module.exports = Command;