djs-selfbot-v13 3.1.7 → 3.1.8

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 (149) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +18 -45
  3. package/package.json +8 -37
  4. package/src/client/BaseClient.js +2 -3
  5. package/src/client/Client.js +187 -539
  6. package/src/client/actions/Action.js +18 -13
  7. package/src/client/actions/ActionsManager.js +7 -1
  8. package/src/client/actions/AutoModerationActionExecution.js +1 -0
  9. package/src/client/actions/AutoModerationRuleCreate.js +1 -0
  10. package/src/client/actions/AutoModerationRuleDelete.js +1 -0
  11. package/src/client/actions/AutoModerationRuleUpdate.js +1 -0
  12. package/src/client/actions/MessageCreate.js +0 -4
  13. package/src/client/actions/PresenceUpdate.js +17 -16
  14. package/src/client/websocket/WebSocketManager.js +11 -31
  15. package/src/client/websocket/WebSocketShard.js +39 -38
  16. package/src/client/websocket/handlers/CALL_CREATE.js +3 -3
  17. package/src/client/websocket/handlers/CALL_DELETE.js +2 -2
  18. package/src/client/websocket/handlers/CALL_UPDATE.js +2 -2
  19. package/src/client/websocket/handlers/CHANNEL_RECIPIENT_ADD.js +16 -13
  20. package/src/client/websocket/handlers/CHANNEL_RECIPIENT_REMOVE.js +11 -11
  21. package/src/client/websocket/handlers/GUILD_CREATE.js +7 -0
  22. package/src/client/websocket/handlers/INTERACTION_MODAL_CREATE.js +1 -0
  23. package/src/client/websocket/handlers/READY.js +47 -137
  24. package/src/client/websocket/handlers/RELATIONSHIP_ADD.js +7 -5
  25. package/src/client/websocket/handlers/RELATIONSHIP_REMOVE.js +6 -4
  26. package/src/client/websocket/handlers/RELATIONSHIP_UPDATE.js +32 -9
  27. package/src/client/websocket/handlers/USER_GUILD_SETTINGS_UPDATE.js +2 -8
  28. package/src/client/websocket/handlers/USER_NOTE_UPDATE.js +1 -1
  29. package/src/client/websocket/handlers/USER_REQUIRED_ACTION_UPDATE.js +78 -0
  30. package/src/client/websocket/handlers/USER_SETTINGS_UPDATE.js +1 -5
  31. package/src/client/websocket/handlers/VOICE_CHANNEL_STATUS_UPDATE.js +12 -0
  32. package/src/client/websocket/handlers/index.js +15 -20
  33. package/src/errors/Messages.js +24 -69
  34. package/src/index.js +12 -43
  35. package/src/managers/ApplicationCommandManager.js +9 -12
  36. package/src/managers/ApplicationCommandPermissionsManager.js +3 -11
  37. package/src/managers/ChannelManager.js +3 -4
  38. package/src/managers/ClientUserSettingManager.js +161 -279
  39. package/src/managers/GuildBanManager.js +1 -1
  40. package/src/managers/GuildChannelManager.js +2 -0
  41. package/src/managers/GuildForumThreadManager.js +22 -28
  42. package/src/managers/GuildMemberManager.js +40 -216
  43. package/src/managers/GuildSettingManager.js +22 -15
  44. package/src/managers/MessageManager.js +42 -44
  45. package/src/managers/PermissionOverwriteManager.js +1 -1
  46. package/src/managers/ReactionUserManager.js +5 -5
  47. package/src/managers/RelationshipManager.js +81 -74
  48. package/src/managers/ThreadManager.js +12 -45
  49. package/src/managers/ThreadMemberManager.js +1 -1
  50. package/src/managers/UserManager.js +6 -10
  51. package/src/managers/UserNoteManager.js +53 -0
  52. package/src/rest/APIRequest.js +42 -20
  53. package/src/rest/DiscordAPIError.js +17 -16
  54. package/src/rest/RESTManager.js +1 -21
  55. package/src/rest/RequestHandler.js +35 -21
  56. package/src/structures/ApplicationCommand.js +19 -456
  57. package/src/structures/ApplicationRoleConnectionMetadata.js +3 -0
  58. package/src/structures/AutoModerationRule.js +5 -5
  59. package/src/structures/AutocompleteInteraction.js +1 -0
  60. package/src/structures/BaseGuildTextChannel.js +10 -12
  61. package/src/structures/BaseGuildVoiceChannel.js +16 -18
  62. package/src/structures/{Call.js → CallState.js} +17 -12
  63. package/src/structures/CategoryChannel.js +2 -0
  64. package/src/structures/Channel.js +2 -3
  65. package/src/structures/ClientPresence.js +12 -8
  66. package/src/structures/ClientUser.js +117 -336
  67. package/src/structures/ContextMenuInteraction.js +1 -1
  68. package/src/structures/DMChannel.js +29 -92
  69. package/src/structures/ForumChannel.js +0 -10
  70. package/src/structures/GroupDMChannel.js +387 -0
  71. package/src/structures/Guild.js +135 -271
  72. package/src/structures/GuildAuditLogs.js +0 -5
  73. package/src/structures/GuildChannel.js +16 -2
  74. package/src/structures/GuildMember.js +27 -145
  75. package/src/structures/Interaction.js +1 -62
  76. package/src/structures/Invite.js +35 -52
  77. package/src/structures/Message.js +228 -202
  78. package/src/structures/MessageAttachment.js +11 -0
  79. package/src/structures/MessageButton.js +1 -67
  80. package/src/structures/MessageEmbed.js +1 -1
  81. package/src/structures/MessageMentions.js +3 -2
  82. package/src/structures/MessagePayload.js +4 -46
  83. package/src/structures/MessageReaction.js +1 -1
  84. package/src/structures/MessageSelectMenu.js +1 -252
  85. package/src/structures/Modal.js +75 -180
  86. package/src/structures/Presence.js +2 -2
  87. package/src/structures/RichPresence.js +14 -34
  88. package/src/structures/Role.js +18 -2
  89. package/src/structures/SelectMenuInteraction.js +2 -151
  90. package/src/structures/Team.js +0 -49
  91. package/src/structures/TextInputComponent.js +0 -70
  92. package/src/structures/ThreadChannel.js +0 -19
  93. package/src/structures/User.js +117 -345
  94. package/src/structures/UserContextMenuInteraction.js +2 -2
  95. package/src/structures/VoiceState.js +74 -39
  96. package/src/structures/WebEmbed.js +38 -52
  97. package/src/structures/Webhook.js +17 -11
  98. package/src/structures/interfaces/Application.js +146 -23
  99. package/src/structures/interfaces/TextBasedChannel.js +411 -256
  100. package/src/util/ApplicationFlags.js +1 -1
  101. package/src/util/AttachmentFlags.js +38 -0
  102. package/src/util/Constants.js +106 -284
  103. package/src/util/Formatters.js +16 -2
  104. package/src/util/InviteFlags.js +29 -0
  105. package/src/util/LimitedCollection.js +1 -1
  106. package/src/util/Options.js +48 -68
  107. package/src/util/Permissions.js +5 -0
  108. package/src/util/PurchasedFlags.js +2 -0
  109. package/src/util/RemoteAuth.js +221 -356
  110. package/src/util/RoleFlags.js +37 -0
  111. package/src/util/Sweepers.js +1 -1
  112. package/src/util/Util.js +76 -36
  113. package/typings/enums.d.ts +18 -73
  114. package/typings/index.d.ts +873 -1225
  115. package/typings/rawDataTypes.d.ts +68 -9
  116. package/src/client/actions/InteractionCreate.js +0 -115
  117. package/src/client/websocket/handlers/APPLICATION_COMMAND_AUTOCOMPLETE_RESPONSE.js +0 -23
  118. package/src/client/websocket/handlers/GUILD_APPLICATION_COMMANDS_UPDATE.js +0 -11
  119. package/src/client/websocket/handlers/GUILD_MEMBER_LIST_UPDATE.js +0 -55
  120. package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUNDS_UPDATE.js +0 -0
  121. package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_CREATE.js +0 -0
  122. package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_DELETE.js +0 -0
  123. package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_UPDATE.js +0 -0
  124. package/src/client/websocket/handlers/INTERACTION_CREATE.js +0 -16
  125. package/src/client/websocket/handlers/INTERACTION_FAILURE.js +0 -18
  126. package/src/client/websocket/handlers/INTERACTION_SUCCESS.js +0 -30
  127. package/src/client/websocket/handlers/MESSAGE_ACK.js +0 -16
  128. package/src/client/websocket/handlers/SOUNDBOARD_SOUNDS.js +0 -0
  129. package/src/client/websocket/handlers/VOICE_CHANNEL_EFFECT_SEND.js +0 -0
  130. package/src/managers/DeveloperPortalManager.js +0 -104
  131. package/src/managers/GuildApplicationCommandManager.js +0 -28
  132. package/src/managers/GuildFolderManager.js +0 -24
  133. package/src/managers/SessionManager.js +0 -57
  134. package/src/rest/CaptchaSolver.js +0 -132
  135. package/src/structures/ClientApplication.js +0 -204
  136. package/src/structures/DeveloperPortalApplication.js +0 -520
  137. package/src/structures/GuildFolder.js +0 -75
  138. package/src/structures/InteractionResponse.js +0 -114
  139. package/src/structures/PartialGroupDMChannel.js +0 -433
  140. package/src/structures/Session.js +0 -81
  141. package/src/util/Voice.js +0 -1456
  142. package/src/util/arRPC/index.js +0 -229
  143. package/src/util/arRPC/process/detectable.json +0 -1
  144. package/src/util/arRPC/process/index.js +0 -102
  145. package/src/util/arRPC/process/native/index.js +0 -5
  146. package/src/util/arRPC/process/native/linux.js +0 -37
  147. package/src/util/arRPC/process/native/win32.js +0 -25
  148. package/src/util/arRPC/transports/ipc.js +0 -281
  149. package/src/util/arRPC/transports/websocket.js +0 -128
@@ -1,17 +1,14 @@
1
1
  'use strict';
2
2
 
3
3
  /* eslint-disable import/order */
4
- const InteractionManager = require('../../managers/InteractionManager');
5
4
  const MessageCollector = require('../MessageCollector');
6
5
  const MessagePayload = require('../MessagePayload');
6
+ const { InteractionTypes, ApplicationCommandOptionTypes, Events } = require('../../util/Constants');
7
+ const { Error } = require('../../errors');
7
8
  const SnowflakeUtil = require('../../util/SnowflakeUtil');
8
- const { Collection } = require('@discordjs/collection');
9
- const { InteractionTypes, MaxBulkDeletableMessageAge } = require('../../util/Constants');
10
- const { TypeError, Error } = require('../../errors');
11
- const InteractionCollector = require('../InteractionCollector');
12
- const { lazy, getAttachments, uploadFile } = require('../../util/Util');
13
- const Message = lazy(() => require('../Message').Message);
9
+ const { setTimeout } = require('node:timers');
14
10
  const { s } = require('@sapphire/shapeshift');
11
+ const Util = require('../../util/Util');
15
12
  const validateName = stringName =>
16
13
  s.string
17
14
  .lengthGreaterThanOrEqual(1)
@@ -32,12 +29,6 @@ class TextBasedChannel {
32
29
  */
33
30
  this.messages = new MessageManager(this);
34
31
 
35
- /**
36
- * A manager of the interactions sent to this channel
37
- * @type {InteractionManager}
38
- */
39
- this.interactions = new InteractionManager(this);
40
-
41
32
  /**
42
33
  * The channel's last message id, if one was sent
43
34
  * @type {?Snowflake}
@@ -76,7 +67,7 @@ class TextBasedChannel {
76
67
  * @property {boolean} [tts=false] Whether or not the message should be spoken aloud
77
68
  * @property {string} [nonce=''] The nonce for the message
78
69
  * @property {string} [content=''] The content for the message
79
- * @property {Array<(MessageEmbed|APIEmbed|WebEmbed)>} [embeds] The embeds for the message
70
+ * @property {Array<(MessageEmbed|APIEmbed)>} [embeds] The embeds for the message
80
71
  * (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details)
81
72
  * @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
82
73
  * (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details)
@@ -84,7 +75,6 @@ class TextBasedChannel {
84
75
  * @property {Array<(MessageActionRow|MessageActionRowOptions)>} [components]
85
76
  * Action rows containing interactive components for the message (buttons, select menus)
86
77
  * @property {MessageAttachment[]} [attachments] Attachments to send in the message
87
- * @property {boolean} [usingNewAttachmentAPI] Whether to use the new attachment API (`channels/:id/attachments`)
88
78
  */
89
79
 
90
80
  /**
@@ -168,39 +158,195 @@ class TextBasedChannel {
168
158
  let messagePayload;
169
159
 
170
160
  if (options instanceof MessagePayload) {
171
- messagePayload = await options.resolveData();
161
+ messagePayload = options.resolveData();
172
162
  } else {
173
- messagePayload = await MessagePayload.create(this, options).resolveData();
163
+ messagePayload = MessagePayload.create(this, options).resolveData();
174
164
  }
175
165
 
176
- let { data, files } = await messagePayload.resolveFiles();
166
+ const { data, files } = await messagePayload.resolveFiles();
167
+ // New API
168
+ const attachments = await Util.getUploadURL(this.client, this.id, files);
169
+ const requestPromises = attachments.map(async attachment => {
170
+ await Util.uploadFile(files[attachment.id].file, attachment.upload_url);
171
+ return {
172
+ id: attachment.id,
173
+ filename: files[attachment.id].name,
174
+ uploaded_filename: attachment.upload_filename,
175
+ description: files[attachment.id].description,
176
+ duration_secs: files[attachment.id].duration_secs,
177
+ waveform: files[attachment.id].waveform,
178
+ };
179
+ });
180
+ const attachmentsData = await Promise.all(requestPromises);
181
+ attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
182
+ data.attachments = attachmentsData;
183
+ // Empty Files
184
+ const d = await this.client.api.channels[this.id].messages.post({ data });
177
185
 
178
- if (typeof options == 'object' && typeof options.usingNewAttachmentAPI !== 'boolean') {
179
- options.usingNewAttachmentAPI = this.client.options.usingNewAttachmentAPI;
180
- }
186
+ return this.messages.cache.get(d.id) ?? this.messages._add(d);
187
+ }
181
188
 
182
- if (options?.usingNewAttachmentAPI === true) {
183
- const attachments = await getAttachments(this.client, this.id, ...files);
184
- const requestPromises = attachments.map(async attachment => {
185
- await uploadFile(files[attachment.id].file, attachment.upload_url);
186
- return {
187
- id: attachment.id,
188
- filename: files[attachment.id].name,
189
- uploaded_filename: attachment.upload_filename,
190
- description: files[attachment.id].description,
191
- duration_secs: files[attachment.id].duration_secs,
192
- waveform: files[attachment.id].waveform,
193
- };
194
- });
195
- const attachmentsData = await Promise.all(requestPromises);
196
- attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
197
- data.attachments = attachmentsData;
198
- files = [];
199
- }
189
+ searchInteraction() {
190
+ // Support Slash / ContextMenu
191
+ // API https://canary.discord.com/api/v9/guilds/:id/application-command-index // Guild
192
+ // https://canary.discord.com/api/v9/channels/:id/application-command-index // DM Channel
193
+ // Updated: 07/01/2023
194
+ return this.client.api[this.guild ? 'guilds' : 'channels'][this.guild?.id || this.id][
195
+ 'application-command-index'
196
+ ].get();
197
+ }
200
198
 
201
- const d = await this.client.api.channels[this.id].messages.post({ data, files });
199
+ async sendSlash(botOrApplicationId, commandNameString, ...args) {
200
+ // Parse commandName /role add user
201
+ const cmd = commandNameString.trim().split(' ');
202
+ // Ex: role add user => [role, add, user]
203
+ // Parse: name, subGr, sub
204
+ const commandName = validateName(cmd[0]);
205
+ // Parse: role
206
+ const sub = cmd.slice(1);
207
+ // Parse: [add, user]
208
+ for (let i = 0; i < sub.length; i++) {
209
+ if (sub.length > 2) {
210
+ throw new Error('INVALID_COMMAND_NAME', cmd);
211
+ }
212
+ validateName(sub[i]);
213
+ }
214
+ // Search all
215
+ const data = await this.searchInteraction();
216
+ // Find command...
217
+ const filterCommand = data.application_commands.filter(obj =>
218
+ // Filter: name | name_default
219
+ [obj.name, obj.name_default].includes(commandName),
220
+ );
221
+ // Filter Bot
222
+ botOrApplicationId = this.client.users.resolveId(botOrApplicationId);
223
+ const application = data.applications.find(
224
+ obj => obj.id == botOrApplicationId || obj.bot?.id == botOrApplicationId,
225
+ );
226
+ // Find Command with application
227
+ const command = filterCommand.find(command => command.application_id == application.id);
202
228
 
203
- return this.messages.cache.get(d.id) ?? this.messages._add(d);
229
+ args = args.flat(2);
230
+ let optionFormat = [];
231
+ let attachments = [];
232
+ let optionsMaxdepth, subGroup, subCommand;
233
+ if (sub.length == 2) {
234
+ // Subcommand Group > Subcommand
235
+ // Find Sub group
236
+ subGroup = command.options.find(
237
+ obj =>
238
+ obj.type == ApplicationCommandOptionTypes.SUB_COMMAND_GROUP && [obj.name, obj.name_default].includes(sub[0]),
239
+ );
240
+ if (!subGroup) throw new Error('SLASH_COMMAND_SUB_COMMAND_GROUP_INVALID', sub[0]);
241
+ // Find Sub
242
+ subCommand = subGroup.options.find(
243
+ obj => obj.type == ApplicationCommandOptionTypes.SUB_COMMAND && [obj.name, obj.name_default].includes(sub[1]),
244
+ );
245
+ if (!subCommand) throw new Error('SLASH_COMMAND_SUB_COMMAND_INVALID', sub[1]);
246
+ // Options
247
+ optionsMaxdepth = subCommand.options;
248
+ } else if (sub.length == 1) {
249
+ // Subcommand
250
+ subCommand = command.options.find(
251
+ obj => obj.type == ApplicationCommandOptionTypes.SUB_COMMAND && [obj.name, obj.name_default].includes(sub[0]),
252
+ );
253
+ if (!subCommand) throw new Error('SLASH_COMMAND_SUB_COMMAND_INVALID', sub[0]);
254
+ // Options
255
+ optionsMaxdepth = subCommand.options;
256
+ } else {
257
+ optionsMaxdepth = command.options;
258
+ }
259
+ const valueRequired = optionsMaxdepth?.filter(o => o.required).length || 0;
260
+ for (let i = 0; i < Math.min(args.length, optionsMaxdepth?.length || 0); i++) {
261
+ const optionInput = optionsMaxdepth[i];
262
+ const value = args[i];
263
+ const parseData = await parseOption(
264
+ this.client,
265
+ optionInput,
266
+ value,
267
+ optionFormat,
268
+ attachments,
269
+ command,
270
+ application.id,
271
+ this.guild?.id,
272
+ this.id,
273
+ subGroup,
274
+ subCommand,
275
+ );
276
+ optionFormat = parseData.optionFormat;
277
+ attachments = parseData.attachments;
278
+ }
279
+ if (valueRequired > args.length) {
280
+ throw new Error('SLASH_COMMAND_REQUIRED_OPTIONS_MISSING', valueRequired, optionFormat.length);
281
+ }
282
+ // Post
283
+ let postData;
284
+ if (subGroup) {
285
+ postData = [
286
+ {
287
+ type: ApplicationCommandOptionTypes.SUB_COMMAND_GROUP,
288
+ name: subGroup.name,
289
+ options: [
290
+ {
291
+ type: ApplicationCommandOptionTypes.SUB_COMMAND,
292
+ name: subCommand.name,
293
+ options: optionFormat,
294
+ },
295
+ ],
296
+ },
297
+ ];
298
+ } else if (subCommand) {
299
+ postData = [
300
+ {
301
+ type: ApplicationCommandOptionTypes.SUB_COMMAND,
302
+ name: subCommand.name,
303
+ options: optionFormat,
304
+ },
305
+ ];
306
+ } else {
307
+ postData = optionFormat;
308
+ }
309
+ const nonce = SnowflakeUtil.generate();
310
+ const body = createPostData(
311
+ this.client,
312
+ false,
313
+ application.id,
314
+ nonce,
315
+ this.guild?.id,
316
+ Boolean(command.guild_id),
317
+ this.id,
318
+ command.version,
319
+ command.id,
320
+ command.name_default || command.name,
321
+ command.type,
322
+ postData,
323
+ attachments,
324
+ );
325
+ this.client.api.interactions.post({
326
+ data: body,
327
+ usePayloadJSON: true,
328
+ });
329
+ return new Promise((resolve, reject) => {
330
+ const timeoutMs = 5_000;
331
+ // Waiting for MsgCreate / ModalCreate
332
+ const handler = data => {
333
+ if (data.nonce !== nonce) return;
334
+ clearTimeout(timeout);
335
+ this.client.removeListener(Events.MESSAGE_CREATE, handler);
336
+ this.client.removeListener(Events.INTERACTION_MODAL_CREATE, handler);
337
+ this.client.decrementMaxListeners();
338
+ resolve(data);
339
+ };
340
+ const timeout = setTimeout(() => {
341
+ this.client.removeListener(Events.MESSAGE_CREATE, handler);
342
+ this.client.removeListener(Events.INTERACTION_MODAL_CREATE, handler);
343
+ this.client.decrementMaxListeners();
344
+ reject(new Error('INTERACTION_FAILED'));
345
+ }, timeoutMs).unref();
346
+ this.client.incrementMaxListeners();
347
+ this.client.on(Events.MESSAGE_CREATE, handler);
348
+ this.client.on(Events.INTERACTION_MODAL_CREATE, handler);
349
+ });
204
350
  }
205
351
 
206
352
  /**
@@ -261,101 +407,6 @@ class TextBasedChannel {
261
407
  });
262
408
  }
263
409
 
264
- /**
265
- * Creates a component interaction collector.
266
- * @param {MessageComponentCollectorOptions} [options={}] Options to send to the collector
267
- * @returns {InteractionCollector}
268
- * @example
269
- * // Create a button interaction collector
270
- * const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
271
- * const collector = channel.createMessageComponentCollector({ filter, time: 15_000 });
272
- * collector.on('collect', i => console.log(`Collected ${i.customId}`));
273
- * collector.on('end', collected => console.log(`Collected ${collected.size} items`));
274
- */
275
- createMessageComponentCollector(options = {}) {
276
- return new InteractionCollector(this.client, {
277
- ...options,
278
- interactionType: InteractionTypes.MESSAGE_COMPONENT,
279
- channel: this,
280
- });
281
- }
282
-
283
- /**
284
- * Collects a single component interaction that passes the filter.
285
- * The Promise will reject if the time expires.
286
- * @param {AwaitMessageComponentOptions} [options={}] Options to pass to the internal collector
287
- * @returns {Promise<MessageComponentInteraction>}
288
- * @example
289
- * // Collect a message component interaction
290
- * const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
291
- * channel.awaitMessageComponent({ filter, time: 15_000 })
292
- * .then(interaction => console.log(`${interaction.customId} was clicked!`))
293
- * .catch(console.error);
294
- */
295
- awaitMessageComponent(options = {}) {
296
- const _options = { ...options, max: 1 };
297
- return new Promise((resolve, reject) => {
298
- const collector = this.createMessageComponentCollector(_options);
299
- collector.once('end', (interactions, reason) => {
300
- const interaction = interactions.first();
301
- if (interaction) resolve(interaction);
302
- else reject(new Error('INTERACTION_COLLECTOR_ERROR', reason));
303
- });
304
- });
305
- }
306
-
307
- /**
308
- * Bulk deletes given messages that are newer than two weeks.
309
- * @param {Collection<Snowflake, Message>|MessageResolvable[]|number} messages
310
- * Messages or number of messages to delete
311
- * @param {boolean} [filterOld=false] Filter messages to remove those which are older than two weeks automatically
312
- * @returns {Promise<Collection<Snowflake, Message|undefined>>} Returns the deleted messages
313
- * @example
314
- * // Bulk delete messages
315
- * channel.bulkDelete(5)
316
- * .then(messages => console.log(`Bulk deleted ${messages.size} messages`))
317
- * .catch(console.error);
318
- */
319
- async bulkDelete(messages, filterOld = false) {
320
- if (!this.client.user.bot) throw new Error('INVALID_USER_METHOD');
321
- if (Array.isArray(messages) || messages instanceof Collection) {
322
- let messageIds = messages instanceof Collection ? [...messages.keys()] : messages.map(m => m.id ?? m);
323
- if (filterOld) {
324
- messageIds = messageIds.filter(id => Date.now() - SnowflakeUtil.timestampFrom(id) < MaxBulkDeletableMessageAge);
325
- }
326
- if (messageIds.length === 0) return new Collection();
327
- if (messageIds.length === 1) {
328
- await this.client.api.channels(this.id).messages(messageIds[0]).delete();
329
- const message = this.client.actions.MessageDelete.getMessage(
330
- {
331
- message_id: messageIds[0],
332
- },
333
- this,
334
- );
335
- return message ? new Collection([[message.id, message]]) : new Collection();
336
- }
337
- await this.client.api.channels[this.id].messages['bulk-delete'].post({ data: { messages: messageIds } });
338
- return messageIds.reduce(
339
- (col, id) =>
340
- col.set(
341
- id,
342
- this.client.actions.MessageDeleteBulk.getMessage(
343
- {
344
- message_id: id,
345
- },
346
- this,
347
- ),
348
- ),
349
- new Collection(),
350
- );
351
- }
352
- if (!isNaN(messages)) {
353
- const msgs = await this.messages.fetch({ limit: messages });
354
- return this.bulkDelete(msgs, filterOld);
355
- }
356
- throw new TypeError('MESSAGE_BULK_DELETE_TYPE');
357
- }
358
-
359
410
  /**
360
411
  * Fetches all webhooks for the channel.
361
412
  * @returns {Promise<Collection<Snowflake, Webhook>>}
@@ -414,139 +465,21 @@ class TextBasedChannel {
414
465
  return this.edit({ nsfw }, reason);
415
466
  }
416
467
 
417
- /**
418
- * Search Slash Command (return raw data)
419
- * @param {Snowflake} applicationId Application ID
420
- * @param {?ApplicationCommandType} type Command Type
421
- * @returns {Object}
422
- */
423
- searchInteraction(applicationId, type = 'CHAT_INPUT') {
424
- switch (type) {
425
- case 'USER':
426
- case 2:
427
- type = 2;
428
- break;
429
- case 'MESSAGE':
430
- case 3:
431
- type = 3;
432
- break;
433
- default:
434
- type = 1;
435
- break;
436
- }
437
- return this.client.api.channels[this.id]['application-commands'].search.get({
438
- query: {
439
- type,
440
- application_id: applicationId,
441
- },
442
- });
443
- }
444
-
445
- /**
446
- * Send Slash to this channel
447
- * @param {UserResolvable} bot Bot user (BotID, not applicationID)
448
- * @param {string} commandString Command name (and sub / group formats)
449
- * @param {...?any|any[]} args Command arguments
450
- * @returns {Promise<InteractionResponse>}
451
- * @example
452
- * // Send a basic slash
453
- * channel.sendSlash('botid', 'ping')
454
- * .then(console.log)
455
- * .catch(console.error);
456
- * @example
457
- * // Send a remote file
458
- * channel.sendSlash('botid', 'emoji upload', 'https://cdn.discordapp.com/icons/222078108977594368/6e1019b3179d71046e463a75915e7244.png?size=2048', 'test')
459
- * .then(console.log)
460
- * .catch(console.error);
461
- * @see {@link https://github.com/aiko-chan-ai/discord.js-selfbot-v13/blob/main/Document/SlashCommand.md}
462
- */
463
- async sendSlash(bot, commandString, ...args) {
464
- const perms =
465
- this.type != 'DM'
466
- ? this.permissionsFor(this.client.user).toArray()
467
- : ['USE_APPLICATION_COMMANDS', `${this.recipient.relationships == 'BLOCKED' ? '' : 'SEND_MESSAGES'}`];
468
- if (!perms.includes('SEND_MESSAGES')) {
469
- throw new Error(
470
- 'INTERACTION_SEND_FAILURE',
471
- `Cannot send Slash to ${this.toString()} ${
472
- this.recipient ? 'because bot has been blocked' : 'due to missing SEND_MESSAGES permission'
473
- }`,
474
- );
475
- }
476
- if (!perms.includes('USE_APPLICATION_COMMANDS')) {
477
- throw new Error(
478
- 'INTERACTION_SEND_FAILURE',
479
- `Cannot send Slash to ${this.toString()} due to missing USE_APPLICATION_COMMANDS permission`,
480
- );
481
- }
482
- args = args.flat(2);
483
- const cmd = commandString.trim().split(' ');
484
- // Validate CommandName
485
- const commandName = validateName(cmd[0]);
486
- const sub = cmd.slice(1);
487
- for (let i = 0; i < sub.length; i++) {
488
- if (sub.length > 2) {
489
- throw new Error('INVALID_COMMAND_NAME', cmd);
490
- }
491
- validateName(sub[i]);
492
- }
493
- if (!bot) throw new Error('MUST_SPECIFY_BOT');
494
- const botId = this.client.users.resolveId(bot);
495
- const user = await this.client.users.fetch(botId).catch(() => {});
496
- if (!user || !user.bot || !user.application) {
497
- throw new Error('botId is not a bot or does not have an application slash command');
498
- }
499
- if (user._partial) await user.getProfile().catch(() => {});
500
- if (!commandName || typeof commandName !== 'string') throw new Error('Command name is required');
501
- const data = await this.searchInteraction(user.application?.id ?? user.id, 'CHAT_INPUT');
502
- for (const command of data.application_commands) {
503
- if (user.id == command.application_id || user.application.id == command.application_id) {
504
- user.application?.commands?._add(command, true);
505
- }
506
- }
507
- // Remove
508
- const commandTarget = user.application?.commands?.cache.find(
509
- c => c.name === commandName && c.type === 'CHAT_INPUT',
510
- );
511
- if (!commandTarget) {
512
- throw new Error(
513
- 'INTERACTION_SEND_FAILURE',
514
- `SlashCommand ${commandName} is not found (With search)\nDebug:\n+ botId: ${botId} (ApplicationId: ${
515
- user.application?.id
516
- })\n+ args: ${args.join(' | ') || null}`,
517
- );
518
- }
519
- return commandTarget.sendSlashCommand(
520
- new (Message())(this.client, {
521
- channel_id: this.id,
522
- guild_id: this.guild?.id || null,
523
- author: this.client.user,
524
- content: '',
525
- id: this.client.user.id,
526
- }),
527
- sub && sub.length > 0 ? sub : [],
528
- args && args.length ? args : [],
529
- );
530
- }
531
-
532
468
  static applyToClass(structure, full = false, ignore = []) {
533
469
  const props = ['send'];
534
470
  if (full) {
535
471
  props.push(
472
+ 'sendSlash',
473
+ 'searchInteraction',
536
474
  'lastMessage',
537
475
  'lastPinAt',
538
- 'bulkDelete',
539
476
  'sendTyping',
540
477
  'createMessageCollector',
541
478
  'awaitMessages',
542
- 'createMessageComponentCollector',
543
- 'awaitMessageComponent',
544
479
  'fetchWebhooks',
545
480
  'createWebhook',
546
481
  'setRateLimitPerUser',
547
482
  'setNSFW',
548
- 'sendSlash',
549
- 'searchInteraction',
550
483
  );
551
484
  }
552
485
  for (const prop of props) {
@@ -564,3 +497,225 @@ module.exports = TextBasedChannel;
564
497
 
565
498
  // Fixes Circular
566
499
  const MessageManager = require('../../managers/MessageManager');
500
+
501
+ // Utils
502
+ function parseChoices(parent, list_choices, value) {
503
+ if (value !== undefined) {
504
+ if (Array.isArray(list_choices) && list_choices.length) {
505
+ const choice = list_choices.find(c => [c.name, c.value].includes(value));
506
+ if (choice) {
507
+ return choice.value;
508
+ } else {
509
+ throw new Error('INVALID_SLASH_COMMAND_CHOICES', parent, value);
510
+ }
511
+ } else {
512
+ return value;
513
+ }
514
+ } else {
515
+ return undefined;
516
+ }
517
+ }
518
+
519
+ async function addDataFromAttachment(value, client, channelId, attachments) {
520
+ value = await MessagePayload.resolveFile(value);
521
+ if (!value?.file) {
522
+ throw new TypeError('The attachment data must be a BufferResolvable or Stream or FileOptions of MessageAttachment');
523
+ }
524
+ const data = await Util.getUploadURL(client, channelId, [value]);
525
+ await Util.uploadFile(value.file, data[0].upload_url);
526
+ const id = attachments.length;
527
+ attachments.push({
528
+ id,
529
+ filename: value.name,
530
+ uploaded_filename: data[0].upload_filename,
531
+ });
532
+ return {
533
+ id,
534
+ attachments,
535
+ };
536
+ }
537
+
538
+ async function parseOption(
539
+ client,
540
+ optionCommand,
541
+ value,
542
+ optionFormat,
543
+ attachments,
544
+ command,
545
+ applicationId,
546
+ guildId,
547
+ channelId,
548
+ subGroup,
549
+ subCommand,
550
+ ) {
551
+ const data = {
552
+ type: optionCommand.type,
553
+ name: optionCommand.name,
554
+ };
555
+ if (value !== undefined) {
556
+ switch (optionCommand.type) {
557
+ case ApplicationCommandOptionTypes.BOOLEAN:
558
+ case 'BOOLEAN': {
559
+ data.value = Boolean(value);
560
+ break;
561
+ }
562
+ case ApplicationCommandOptionTypes.INTEGER:
563
+ case 'INTEGER': {
564
+ data.value = Number(value);
565
+ break;
566
+ }
567
+ case ApplicationCommandOptionTypes.ATTACHMENT:
568
+ case 'ATTACHMENT': {
569
+ const parseData = await addDataFromAttachment(value, client, channelId, attachments);
570
+ data.value = parseData.id;
571
+ attachments = parseData.attachments;
572
+ break;
573
+ }
574
+ case ApplicationCommandOptionTypes.SUB_COMMAND_GROUP:
575
+ case 'SUB_COMMAND_GROUP': {
576
+ break;
577
+ }
578
+ default: {
579
+ value = parseChoices(optionCommand.name, optionCommand.choices, value);
580
+ if (optionCommand.autocomplete) {
581
+ const nonce = SnowflakeUtil.generate();
582
+ // Post
583
+ let postData;
584
+ if (subGroup) {
585
+ postData = [
586
+ {
587
+ type: ApplicationCommandOptionTypes.SUB_COMMAND_GROUP,
588
+ name: subGroup.name,
589
+ options: [
590
+ {
591
+ type: ApplicationCommandOptionTypes.SUB_COMMAND,
592
+ name: subCommand.name,
593
+ options: [
594
+ {
595
+ type: optionCommand.type,
596
+ name: optionCommand.name,
597
+ value,
598
+ focused: true,
599
+ },
600
+ ],
601
+ },
602
+ ],
603
+ },
604
+ ];
605
+ } else if (subCommand) {
606
+ postData = [
607
+ {
608
+ type: ApplicationCommandOptionTypes.SUB_COMMAND,
609
+ name: subCommand.name,
610
+ options: [
611
+ {
612
+ type: optionCommand.type,
613
+ name: optionCommand.name,
614
+ value,
615
+ focused: true,
616
+ },
617
+ ],
618
+ },
619
+ ];
620
+ } else {
621
+ postData = [
622
+ {
623
+ type: optionCommand.type,
624
+ name: optionCommand.name,
625
+ value,
626
+ focused: true,
627
+ },
628
+ ];
629
+ }
630
+ const body = createPostData(
631
+ client,
632
+ true,
633
+ applicationId,
634
+ nonce,
635
+ guildId,
636
+ Boolean(command.guild_id),
637
+ channelId,
638
+ command.version,
639
+ command.id,
640
+ command.name_default || command.name,
641
+ command.type,
642
+ postData,
643
+ [],
644
+ );
645
+ await client.api.interactions.post({
646
+ data: body,
647
+ });
648
+ data.value = await awaitAutocomplete(client, nonce, value);
649
+ } else {
650
+ data.value = value;
651
+ }
652
+ }
653
+ }
654
+ optionFormat.push(data);
655
+ }
656
+ return {
657
+ optionFormat,
658
+ attachments,
659
+ };
660
+ }
661
+
662
+ function awaitAutocomplete(client, nonce, defaultValue) {
663
+ return new Promise(resolve => {
664
+ const handler = data => {
665
+ if (data.t !== 'APPLICATION_COMMAND_AUTOCOMPLETE_RESPONSE') return;
666
+ if (data.d?.nonce !== nonce) return;
667
+ clearTimeout(timeout);
668
+ client.removeListener(Events.UNHANDLED_PACKET, handler);
669
+ client.decrementMaxListeners();
670
+ if (data.d.choices.length >= 1) {
671
+ resolve(data.d.choices[0].value);
672
+ } else {
673
+ resolve(defaultValue);
674
+ }
675
+ };
676
+ const timeout = setTimeout(() => {
677
+ client.removeListener(Events.UNHANDLED_PACKET, handler);
678
+ client.decrementMaxListeners();
679
+ resolve(defaultValue);
680
+ }, 5_000).unref();
681
+ client.incrementMaxListeners();
682
+ client.on(Events.UNHANDLED_PACKET, handler);
683
+ });
684
+ }
685
+
686
+ function createPostData(
687
+ client,
688
+ isAutocomplete = false,
689
+ applicationId,
690
+ nonce,
691
+ guildId,
692
+ isGuildCommand,
693
+ channelId,
694
+ commandVersion,
695
+ commandId,
696
+ commandName,
697
+ commandType,
698
+ postData,
699
+ attachments = [],
700
+ ) {
701
+ const data = {
702
+ type: isAutocomplete ? InteractionTypes.APPLICATION_COMMAND_AUTOCOMPLETE : InteractionTypes.APPLICATION_COMMAND,
703
+ application_id: applicationId,
704
+ guild_id: guildId,
705
+ channel_id: channelId,
706
+ session_id: client.sessionId,
707
+ data: {
708
+ version: commandVersion,
709
+ id: commandId,
710
+ name: commandName,
711
+ type: commandType,
712
+ options: postData,
713
+ attachments: attachments,
714
+ },
715
+ nonce,
716
+ };
717
+ if (isGuildCommand) {
718
+ data.data.guild_id = guildId;
719
+ }
720
+ return data;
721
+ }