djs-selfbot-v13 3.2.2 → 3.7.1

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 (363) hide show
  1. package/README.md +37 -36
  2. package/package.json +89 -85
  3. package/src/WebSocket.js +39 -39
  4. package/src/client/BaseClient.js +86 -86
  5. package/src/client/Client.js +934 -836
  6. package/src/client/WebhookClient.js +61 -61
  7. package/src/client/actions/Action.js +116 -120
  8. package/src/client/actions/ActionsManager.js +80 -78
  9. package/src/client/actions/ApplicationCommandPermissionsUpdate.js +34 -34
  10. package/src/client/actions/AutoModerationActionExecution.js +27 -27
  11. package/src/client/actions/AutoModerationRuleCreate.js +28 -28
  12. package/src/client/actions/AutoModerationRuleDelete.js +32 -32
  13. package/src/client/actions/AutoModerationRuleUpdate.js +30 -30
  14. package/src/client/actions/ChannelCreate.js +23 -23
  15. package/src/client/actions/ChannelDelete.js +39 -39
  16. package/src/client/actions/ChannelUpdate.js +43 -43
  17. package/src/client/actions/GuildAuditLogEntryCreate.js +29 -29
  18. package/src/client/actions/GuildBanAdd.js +20 -20
  19. package/src/client/actions/GuildBanRemove.js +25 -25
  20. package/src/client/actions/GuildChannelsPositionUpdate.js +21 -21
  21. package/src/client/actions/GuildDelete.js +65 -65
  22. package/src/client/actions/GuildEmojiCreate.js +20 -20
  23. package/src/client/actions/GuildEmojiDelete.js +21 -21
  24. package/src/client/actions/GuildEmojiUpdate.js +20 -20
  25. package/src/client/actions/GuildEmojisUpdate.js +34 -34
  26. package/src/client/actions/GuildIntegrationsUpdate.js +19 -19
  27. package/src/client/actions/GuildMemberRemove.js +33 -32
  28. package/src/client/actions/GuildMemberUpdate.js +44 -43
  29. package/src/client/actions/GuildRoleCreate.js +25 -25
  30. package/src/client/actions/GuildRoleDelete.js +31 -31
  31. package/src/client/actions/GuildRoleUpdate.js +39 -39
  32. package/src/client/actions/GuildRolesPositionUpdate.js +21 -21
  33. package/src/client/actions/GuildScheduledEventCreate.js +27 -27
  34. package/src/client/actions/GuildScheduledEventDelete.js +31 -31
  35. package/src/client/actions/GuildScheduledEventUpdate.js +30 -30
  36. package/src/client/actions/GuildScheduledEventUserAdd.js +32 -32
  37. package/src/client/actions/GuildScheduledEventUserRemove.js +32 -32
  38. package/src/client/actions/GuildStickerCreate.js +20 -20
  39. package/src/client/actions/GuildStickerDelete.js +21 -21
  40. package/src/client/actions/GuildStickerUpdate.js +20 -20
  41. package/src/client/actions/GuildStickersUpdate.js +34 -34
  42. package/src/client/actions/GuildUpdate.js +33 -33
  43. package/src/client/actions/InviteCreate.js +28 -28
  44. package/src/client/actions/InviteDelete.js +30 -30
  45. package/src/client/actions/MessageCreate.js +50 -46
  46. package/src/client/actions/MessageDelete.js +32 -32
  47. package/src/client/actions/MessageDeleteBulk.js +46 -46
  48. package/src/client/actions/MessagePollVoteAdd.js +33 -0
  49. package/src/client/actions/MessagePollVoteRemove.js +33 -0
  50. package/src/client/actions/MessageReactionAdd.js +68 -56
  51. package/src/client/actions/MessageReactionRemove.js +50 -45
  52. package/src/client/actions/MessageReactionRemoveAll.js +33 -33
  53. package/src/client/actions/MessageReactionRemoveEmoji.js +28 -28
  54. package/src/client/actions/MessageUpdate.js +26 -26
  55. package/src/client/actions/PresenceUpdate.js +50 -46
  56. package/src/client/actions/StageInstanceCreate.js +28 -28
  57. package/src/client/actions/StageInstanceDelete.js +33 -33
  58. package/src/client/actions/StageInstanceUpdate.js +30 -30
  59. package/src/client/actions/ThreadCreate.js +24 -24
  60. package/src/client/actions/ThreadDelete.js +32 -32
  61. package/src/client/actions/ThreadListSync.js +59 -59
  62. package/src/client/actions/ThreadMemberUpdate.js +30 -30
  63. package/src/client/actions/ThreadMembersUpdate.js +34 -34
  64. package/src/client/actions/TypingStart.js +29 -29
  65. package/src/client/actions/UserUpdate.js +35 -35
  66. package/src/client/actions/VoiceStateUpdate.js +50 -57
  67. package/src/client/actions/WebhooksUpdate.js +20 -20
  68. package/src/client/voice/ClientVoiceManager.js +151 -51
  69. package/src/client/voice/VoiceConnection.js +1249 -0
  70. package/src/client/voice/dispatcher/AnnexBDispatcher.js +120 -0
  71. package/src/client/voice/dispatcher/AudioDispatcher.js +145 -0
  72. package/src/client/voice/dispatcher/BaseDispatcher.js +459 -0
  73. package/src/client/voice/dispatcher/VPxDispatcher.js +54 -0
  74. package/src/client/voice/dispatcher/VideoDispatcher.js +68 -0
  75. package/src/client/voice/networking/VoiceUDPClient.js +173 -0
  76. package/src/client/voice/networking/VoiceWebSocket.js +286 -0
  77. package/src/client/voice/player/MediaPlayer.js +321 -0
  78. package/src/client/voice/player/processing/AnnexBNalSplitter.js +244 -0
  79. package/src/client/voice/player/processing/IvfSplitter.js +106 -0
  80. package/src/client/voice/player/processing/PCMInsertSilence.js +37 -0
  81. package/src/client/voice/receiver/PacketHandler.js +260 -0
  82. package/src/client/voice/receiver/Receiver.js +96 -0
  83. package/src/client/voice/receiver/Recorder.js +173 -0
  84. package/src/client/voice/util/Function.js +116 -0
  85. package/src/client/voice/util/PlayInterface.js +122 -0
  86. package/src/client/voice/util/Secretbox.js +64 -0
  87. package/src/client/voice/util/Silence.js +16 -0
  88. package/src/client/voice/util/Socket.js +62 -0
  89. package/src/client/voice/util/VolumeInterface.js +104 -0
  90. package/src/client/websocket/WebSocketManager.js +392 -392
  91. package/src/client/websocket/WebSocketShard.js +907 -906
  92. package/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js +18 -18
  93. package/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js +20 -20
  94. package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +5 -5
  95. package/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js +20 -20
  96. package/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js +5 -5
  97. package/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js +5 -5
  98. package/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js +5 -5
  99. package/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js +5 -5
  100. package/src/client/websocket/handlers/CALL_CREATE.js +14 -14
  101. package/src/client/websocket/handlers/CALL_DELETE.js +11 -11
  102. package/src/client/websocket/handlers/CALL_UPDATE.js +11 -11
  103. package/src/client/websocket/handlers/CHANNEL_CREATE.js +5 -5
  104. package/src/client/websocket/handlers/CHANNEL_DELETE.js +5 -5
  105. package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +22 -22
  106. package/src/client/websocket/handlers/CHANNEL_RECIPIENT_ADD.js +19 -19
  107. package/src/client/websocket/handlers/CHANNEL_RECIPIENT_REMOVE.js +16 -16
  108. package/src/client/websocket/handlers/CHANNEL_UPDATE.js +16 -16
  109. package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +5 -5
  110. package/src/client/websocket/handlers/GUILD_BAN_ADD.js +5 -5
  111. package/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +5 -5
  112. package/src/client/websocket/handlers/GUILD_CREATE.js +52 -52
  113. package/src/client/websocket/handlers/GUILD_DELETE.js +5 -5
  114. package/src/client/websocket/handlers/GUILD_EMOJIS_UPDATE.js +5 -5
  115. package/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js +5 -5
  116. package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +39 -39
  117. package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +20 -19
  118. package/src/client/websocket/handlers/GUILD_MEMBER_REMOVE.js +5 -5
  119. package/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js +5 -5
  120. package/src/client/websocket/handlers/GUILD_ROLE_CREATE.js +5 -5
  121. package/src/client/websocket/handlers/GUILD_ROLE_DELETE.js +5 -5
  122. package/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js +5 -5
  123. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js +5 -5
  124. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_DELETE.js +5 -5
  125. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js +5 -5
  126. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_ADD.js +5 -5
  127. package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_REMOVE.js +5 -5
  128. package/src/client/websocket/handlers/GUILD_STICKERS_UPDATE.js +5 -5
  129. package/src/client/websocket/handlers/GUILD_UPDATE.js +5 -5
  130. package/src/client/websocket/handlers/INTERACTION_MODAL_CREATE.js +12 -12
  131. package/src/client/websocket/handlers/INVITE_CREATE.js +5 -5
  132. package/src/client/websocket/handlers/INVITE_DELETE.js +5 -5
  133. package/src/client/websocket/handlers/MESSAGE_CREATE.js +5 -5
  134. package/src/client/websocket/handlers/MESSAGE_DELETE.js +5 -5
  135. package/src/client/websocket/handlers/MESSAGE_DELETE_BULK.js +5 -5
  136. package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_ADD.js +5 -22
  137. package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_REMOVE.js +5 -12
  138. package/src/client/websocket/handlers/MESSAGE_REACTION_ADD.js +5 -5
  139. package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE.js +5 -5
  140. package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_ALL.js +5 -5
  141. package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_EMOJI.js +5 -5
  142. package/src/client/websocket/handlers/MESSAGE_UPDATE.js +16 -16
  143. package/src/client/websocket/handlers/PRESENCE_UPDATE.js +5 -5
  144. package/src/client/websocket/handlers/READY.js +121 -120
  145. package/src/client/websocket/handlers/RELATIONSHIP_ADD.js +19 -19
  146. package/src/client/websocket/handlers/RELATIONSHIP_REMOVE.js +17 -17
  147. package/src/client/websocket/handlers/RELATIONSHIP_UPDATE.js +41 -41
  148. package/src/client/websocket/handlers/RESUMED.js +14 -14
  149. package/src/client/websocket/handlers/STAGE_INSTANCE_CREATE.js +5 -5
  150. package/src/client/websocket/handlers/STAGE_INSTANCE_DELETE.js +5 -5
  151. package/src/client/websocket/handlers/STAGE_INSTANCE_UPDATE.js +5 -5
  152. package/src/client/websocket/handlers/THREAD_CREATE.js +5 -5
  153. package/src/client/websocket/handlers/THREAD_DELETE.js +5 -5
  154. package/src/client/websocket/handlers/THREAD_LIST_SYNC.js +5 -5
  155. package/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.js +5 -5
  156. package/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js +5 -5
  157. package/src/client/websocket/handlers/THREAD_UPDATE.js +16 -16
  158. package/src/client/websocket/handlers/TYPING_START.js +5 -5
  159. package/src/client/websocket/handlers/USER_GUILD_SETTINGS_UPDATE.js +6 -6
  160. package/src/client/websocket/handlers/USER_NOTE_UPDATE.js +5 -5
  161. package/src/client/websocket/handlers/USER_REQUIRED_ACTION_UPDATE.js +78 -78
  162. package/src/client/websocket/handlers/USER_SETTINGS_UPDATE.js +5 -5
  163. package/src/client/websocket/handlers/USER_UPDATE.js +5 -5
  164. package/src/client/websocket/handlers/VOICE_CHANNEL_EFFECT_SEND.js +16 -0
  165. package/src/client/websocket/handlers/VOICE_CHANNEL_STATUS_UPDATE.js +12 -12
  166. package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js +6 -6
  167. package/src/client/websocket/handlers/VOICE_STATE_UPDATE.js +5 -5
  168. package/src/client/websocket/handlers/WEBHOOKS_UPDATE.js +5 -5
  169. package/src/client/websocket/handlers/index.js +84 -83
  170. package/src/errors/DJSError.js +61 -61
  171. package/src/errors/Messages.js +217 -183
  172. package/src/errors/index.js +4 -4
  173. package/src/index.js +172 -159
  174. package/src/managers/ApplicationCommandManager.js +264 -264
  175. package/src/managers/ApplicationCommandPermissionsManager.js +417 -417
  176. package/src/managers/AutoModerationRuleManager.js +296 -296
  177. package/src/managers/BaseGuildEmojiManager.js +80 -80
  178. package/src/managers/BaseManager.js +19 -19
  179. package/src/managers/BillingManager.js +66 -66
  180. package/src/managers/CachedManager.js +71 -71
  181. package/src/managers/ChannelManager.js +148 -138
  182. package/src/managers/ClientUserSettingManager.js +372 -372
  183. package/src/managers/DataManager.js +61 -61
  184. package/src/managers/GuildBanManager.js +250 -250
  185. package/src/managers/GuildChannelManager.js +488 -488
  186. package/src/managers/GuildEmojiManager.js +171 -171
  187. package/src/managers/GuildEmojiRoleManager.js +118 -118
  188. package/src/managers/GuildForumThreadManager.js +108 -108
  189. package/src/managers/GuildInviteManager.js +213 -213
  190. package/src/managers/GuildManager.js +338 -304
  191. package/src/managers/GuildMemberManager.js +599 -597
  192. package/src/managers/GuildMemberRoleManager.js +195 -191
  193. package/src/managers/GuildScheduledEventManager.js +314 -296
  194. package/src/managers/GuildSettingManager.js +155 -155
  195. package/src/managers/GuildStickerManager.js +179 -179
  196. package/src/managers/GuildTextThreadManager.js +98 -98
  197. package/src/managers/InteractionManager.js +39 -39
  198. package/src/managers/MessageManager.js +423 -391
  199. package/src/managers/PermissionOverwriteManager.js +164 -166
  200. package/src/managers/PresenceManager.js +71 -58
  201. package/src/managers/ReactionManager.js +67 -67
  202. package/src/managers/ReactionUserManager.js +73 -71
  203. package/src/managers/RelationshipManager.js +278 -265
  204. package/src/managers/RoleManager.js +448 -352
  205. package/src/managers/SessionManager.js +66 -0
  206. package/src/managers/StageInstanceManager.js +162 -162
  207. package/src/managers/ThreadManager.js +175 -174
  208. package/src/managers/ThreadMemberManager.js +186 -186
  209. package/src/managers/UserManager.js +136 -146
  210. package/src/managers/UserNoteManager.js +53 -53
  211. package/src/managers/VoiceStateManager.js +59 -37
  212. package/src/rest/APIRequest.js +154 -160
  213. package/src/rest/APIRouter.js +53 -53
  214. package/src/rest/DiscordAPIError.js +119 -104
  215. package/src/rest/HTTPError.js +62 -62
  216. package/src/rest/RESTManager.js +67 -62
  217. package/src/rest/RateLimitError.js +55 -55
  218. package/src/rest/RequestHandler.js +466 -444
  219. package/src/sharding/Shard.js +444 -443
  220. package/src/sharding/ShardClientUtil.js +279 -275
  221. package/src/sharding/ShardingManager.js +319 -318
  222. package/src/structures/AnonymousGuild.js +98 -98
  223. package/src/structures/ApplicationCommand.js +593 -593
  224. package/src/structures/ApplicationRoleConnectionMetadata.js +48 -48
  225. package/src/structures/AutoModerationActionExecution.js +89 -89
  226. package/src/structures/AutoModerationRule.js +294 -294
  227. package/src/structures/AutocompleteInteraction.js +107 -107
  228. package/src/structures/Base.js +43 -43
  229. package/src/structures/BaseCommandInteraction.js +211 -211
  230. package/src/structures/BaseGuild.js +116 -116
  231. package/src/structures/BaseGuildEmoji.js +56 -56
  232. package/src/structures/BaseGuildTextChannel.js +191 -191
  233. package/src/structures/BaseGuildVoiceChannel.js +241 -241
  234. package/src/structures/BaseMessageComponent.js +181 -114
  235. package/src/structures/ButtonInteraction.js +11 -11
  236. package/src/structures/CallState.js +63 -63
  237. package/src/structures/CategoryChannel.js +85 -85
  238. package/src/structures/Channel.js +284 -270
  239. package/src/structures/ClientPresence.js +77 -85
  240. package/src/structures/ClientUser.js +479 -448
  241. package/src/structures/CommandInteraction.js +41 -41
  242. package/src/structures/CommandInteractionOptionResolver.js +276 -276
  243. package/src/structures/ContainerComponent.js +68 -0
  244. package/src/structures/ContextMenuInteraction.js +65 -65
  245. package/src/structures/DMChannel.js +219 -217
  246. package/src/structures/DirectoryChannel.js +20 -20
  247. package/src/structures/Emoji.js +148 -148
  248. package/src/structures/FileComponent.js +49 -0
  249. package/src/structures/ForumChannel.js +31 -261
  250. package/src/structures/GroupDMChannel.js +394 -387
  251. package/src/structures/Guild.js +1643 -1608
  252. package/src/structures/GuildAuditLogs.js +746 -729
  253. package/src/structures/GuildBan.js +59 -59
  254. package/src/structures/GuildBoost.js +108 -108
  255. package/src/structures/GuildChannel.js +470 -468
  256. package/src/structures/GuildEmoji.js +161 -161
  257. package/src/structures/GuildMember.js +636 -568
  258. package/src/structures/GuildPreview.js +191 -191
  259. package/src/structures/GuildPreviewEmoji.js +27 -27
  260. package/src/structures/GuildScheduledEvent.js +536 -441
  261. package/src/structures/GuildTemplate.js +236 -236
  262. package/src/structures/Integration.js +188 -188
  263. package/src/structures/IntegrationApplication.js +96 -96
  264. package/src/structures/Interaction.js +290 -290
  265. package/src/structures/InteractionCollector.js +248 -248
  266. package/src/structures/InteractionWebhook.js +43 -43
  267. package/src/structures/Invite.js +358 -358
  268. package/src/structures/InviteGuild.js +23 -23
  269. package/src/structures/InviteStageInstance.js +86 -86
  270. package/src/structures/MediaChannel.js +11 -0
  271. package/src/structures/MediaGalleryComponent.js +41 -0
  272. package/src/structures/MediaGalleryItem.js +47 -0
  273. package/src/structures/Message.js +1252 -1227
  274. package/src/structures/MessageActionRow.js +105 -103
  275. package/src/structures/MessageAttachment.js +216 -204
  276. package/src/structures/MessageButton.js +166 -165
  277. package/src/structures/MessageCollector.js +146 -146
  278. package/src/structures/MessageComponentInteraction.js +120 -120
  279. package/src/structures/MessageContextMenuInteraction.js +20 -20
  280. package/src/structures/MessageEmbed.js +596 -586
  281. package/src/structures/MessageMentions.js +273 -273
  282. package/src/structures/MessagePayload.js +354 -318
  283. package/src/structures/MessageReaction.js +181 -171
  284. package/src/structures/MessageSelectMenu.js +141 -140
  285. package/src/structures/Modal.js +161 -161
  286. package/src/structures/ModalSubmitFieldsResolver.js +53 -53
  287. package/src/structures/ModalSubmitInteraction.js +119 -119
  288. package/src/structures/NewsChannel.js +32 -32
  289. package/src/structures/OAuth2Guild.js +28 -28
  290. package/src/structures/PermissionOverwrites.js +198 -196
  291. package/src/structures/Poll.js +108 -0
  292. package/src/structures/PollAnswer.js +88 -0
  293. package/src/structures/Presence.js +1105 -1101
  294. package/src/structures/ReactionCollector.js +229 -229
  295. package/src/structures/ReactionEmoji.js +31 -31
  296. package/src/structures/Role.js +590 -531
  297. package/src/structures/SectionComponent.js +48 -0
  298. package/src/structures/SelectMenuInteraction.js +21 -21
  299. package/src/structures/SeparatorComponent.js +48 -0
  300. package/src/structures/Session.js +81 -0
  301. package/src/structures/StageChannel.js +104 -104
  302. package/src/structures/StageInstance.js +208 -208
  303. package/src/structures/Sticker.js +310 -310
  304. package/src/structures/StickerPack.js +95 -95
  305. package/src/structures/StoreChannel.js +56 -56
  306. package/src/structures/Team.js +118 -118
  307. package/src/structures/TeamMember.js +80 -71
  308. package/src/structures/TextChannel.js +33 -33
  309. package/src/structures/TextDisplayComponent.js +40 -0
  310. package/src/structures/TextInputComponent.js +132 -131
  311. package/src/structures/ThreadChannel.js +605 -607
  312. package/src/structures/ThreadMember.js +105 -105
  313. package/src/structures/ThreadOnlyChannel.js +249 -0
  314. package/src/structures/ThumbnailComponent.js +57 -0
  315. package/src/structures/Typing.js +74 -74
  316. package/src/structures/UnfurledMediaItem.js +29 -0
  317. package/src/structures/User.js +640 -543
  318. package/src/structures/UserContextMenuInteraction.js +29 -29
  319. package/src/structures/VoiceChannel.js +110 -110
  320. package/src/structures/VoiceChannelEffect.js +69 -0
  321. package/src/structures/VoiceRegion.js +53 -53
  322. package/src/structures/VoiceState.js +354 -341
  323. package/src/structures/WebEmbed.js +373 -373
  324. package/src/structures/Webhook.js +478 -467
  325. package/src/structures/WelcomeChannel.js +60 -60
  326. package/src/structures/WelcomeScreen.js +48 -48
  327. package/src/structures/Widget.js +87 -87
  328. package/src/structures/WidgetMember.js +99 -99
  329. package/src/structures/interfaces/Application.js +825 -313
  330. package/src/structures/interfaces/Collector.js +300 -300
  331. package/src/structures/interfaces/InteractionResponses.js +313 -313
  332. package/src/structures/interfaces/TextBasedChannel.js +759 -719
  333. package/src/util/APITypes.js +59 -0
  334. package/src/util/ActivityFlags.js +44 -44
  335. package/src/util/ApplicationFlags.js +76 -76
  336. package/src/util/AttachmentFlags.js +38 -38
  337. package/src/util/BitField.js +170 -170
  338. package/src/util/ChannelFlags.js +45 -45
  339. package/src/util/Constants.js +1914 -1773
  340. package/src/util/DataResolver.js +146 -145
  341. package/src/util/Formatters.js +228 -228
  342. package/src/util/GuildMemberFlags.js +43 -43
  343. package/src/util/Intents.js +74 -74
  344. package/src/util/InviteFlags.js +34 -29
  345. package/src/util/LimitedCollection.js +131 -131
  346. package/src/util/MessageFlags.js +63 -54
  347. package/src/util/Options.js +358 -336
  348. package/src/util/Permissions.js +202 -202
  349. package/src/util/PremiumUsageFlags.js +31 -31
  350. package/src/util/PurchasedFlags.js +33 -33
  351. package/src/util/RemoteAuth.js +382 -379
  352. package/src/util/RoleFlags.js +37 -37
  353. package/src/util/SnowflakeUtil.js +92 -92
  354. package/src/util/Speaking.js +33 -0
  355. package/src/util/Sweepers.js +466 -466
  356. package/src/util/SystemChannelFlags.js +55 -55
  357. package/src/util/ThreadMemberFlags.js +30 -30
  358. package/src/util/UserFlags.js +104 -104
  359. package/src/util/Util.js +1048 -889
  360. package/typings/enums.d.ts +439 -297
  361. package/typings/index.d.ts +8247 -7432
  362. package/typings/rawDataTypes.d.ts +403 -342
  363. package/src/structures/MessagePoll.js +0 -238
@@ -1,836 +1,934 @@
1
- /* eslint-disable no-unreachable */
2
- 'use strict';
3
-
4
- const process = require('node:process');
5
- const { setInterval } = require('node:timers');
6
- const { setTimeout } = require('node:timers');
7
- const { Collection } = require('@discordjs/collection');
8
- const BaseClient = require('./BaseClient');
9
- const ActionsManager = require('./actions/ActionsManager');
10
- const ClientVoiceManager = require('./voice/ClientVoiceManager');
11
- const WebSocketManager = require('./websocket/WebSocketManager');
12
- const { Error, TypeError } = require('../errors');
13
- const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
14
- const BillingManager = require('../managers/BillingManager');
15
- const ChannelManager = require('../managers/ChannelManager');
16
- const ClientUserSettingManager = require('../managers/ClientUserSettingManager');
17
- const GuildManager = require('../managers/GuildManager');
18
- const PresenceManager = require('../managers/PresenceManager');
19
- const RelationshipManager = require('../managers/RelationshipManager');
20
- const UserManager = require('../managers/UserManager');
21
- const UserNoteManager = require('../managers/UserNoteManager');
22
- const VoiceStateManager = require('../managers/VoiceStateManager');
23
- const ShardClientUtil = require('../sharding/ShardClientUtil');
24
- const ClientPresence = require('../structures/ClientPresence');
25
- const GuildPreview = require('../structures/GuildPreview');
26
- const GuildTemplate = require('../structures/GuildTemplate');
27
- const Invite = require('../structures/Invite');
28
- const { Sticker } = require('../structures/Sticker');
29
- const StickerPack = require('../structures/StickerPack');
30
- const VoiceRegion = require('../structures/VoiceRegion');
31
- const Webhook = require('../structures/Webhook');
32
- const Widget = require('../structures/Widget');
33
- const { Events, Status } = require('../util/Constants');
34
- const DataResolver = require('../util/DataResolver');
35
- const Intents = require('../util/Intents');
36
- const Permissions = require('../util/Permissions');
37
- const DiscordAuthWebsocket = require('../util/RemoteAuth');
38
- const Sweepers = require('../util/Sweepers');
39
-
40
- /**
41
- * The main hub for interacting with the Discord API, and the starting point for any bot.
42
- * @extends {BaseClient}
43
- */
44
- class Client extends BaseClient {
45
- /**
46
- * @param {ClientOptions} [options] Options for the client
47
- */
48
- constructor(options) {
49
- super(options);
50
-
51
- this._validateOptions();
52
-
53
- /**
54
- * Functions called when a cache is garbage collected or the Client is destroyed
55
- * @type {Set<Function>}
56
- * @private
57
- */
58
- this._cleanups = new Set();
59
-
60
- /**
61
- * The finalizers used to cleanup items.
62
- * @type {FinalizationRegistry}
63
- * @private
64
- */
65
- this._finalizers = new FinalizationRegistry(this._finalize.bind(this));
66
-
67
- /**
68
- * The WebSocket manager of the client
69
- * @type {WebSocketManager}
70
- */
71
- this.ws = new WebSocketManager(this);
72
-
73
- /**
74
- * The action manager of the client
75
- * @type {ActionsManager}
76
- * @private
77
- */
78
- this.actions = new ActionsManager(this);
79
-
80
- /**
81
- * The voice manager of the client
82
- * @type {ClientVoiceManager}
83
- */
84
- this.voice = new ClientVoiceManager(this);
85
-
86
- /**
87
- * A manager of the voice states of this client (Support DM / Group DM)
88
- * @type {VoiceStateManager}
89
- */
90
- this.voiceStates = new VoiceStateManager({ client: this });
91
-
92
- /**
93
- * Shard helpers for the client (only if the process was spawned from a {@link ShardingManager})
94
- * @type {?ShardClientUtil}
95
- */
96
- this.shard = process.env.SHARDING_MANAGER
97
- ? ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE)
98
- : null;
99
-
100
- /**
101
- * All of the {@link User} objects that have been cached at any point, mapped by their ids
102
- * @type {UserManager}
103
- */
104
- this.users = new UserManager(this);
105
-
106
- /**
107
- * All of the guilds the client is currently handling, mapped by their ids -
108
- * as long as sharding isn't being used, this will be *every* guild the bot is a member of
109
- * @type {GuildManager}
110
- */
111
- this.guilds = new GuildManager(this);
112
-
113
- /**
114
- * All of the {@link Channel}s that the client is currently handling, mapped by their ids -
115
- * as long as sharding isn't being used, this will be *every* channel in *every* guild the bot
116
- * is a member of. Note that DM channels will not be initially cached, and thus not be present
117
- * in the Manager without their explicit fetching or use.
118
- * @type {ChannelManager}
119
- */
120
- this.channels = new ChannelManager(this);
121
-
122
- /**
123
- * The sweeping functions and their intervals used to periodically sweep caches
124
- * @type {Sweepers}
125
- */
126
- this.sweepers = new Sweepers(this, this.options.sweepers);
127
-
128
- /**
129
- * The presence of the Client
130
- * @private
131
- * @type {ClientPresence}
132
- */
133
- this.presence = new ClientPresence(this, this.options.presence);
134
-
135
- /**
136
- * A manager of the presences belonging to this client
137
- * @type {PresenceManager}
138
- */
139
- this.presences = new PresenceManager(this);
140
-
141
- /**
142
- * All of the note that have been cached at any point, mapped by their ids
143
- * @type {UserManager}
144
- */
145
- this.notes = new UserNoteManager(this);
146
-
147
- /**
148
- * All of the relationships {@link User}
149
- * @type {RelationshipManager}
150
- */
151
- this.relationships = new RelationshipManager(this);
152
-
153
- /**
154
- * Manages the API methods
155
- * @type {BillingManager}
156
- */
157
- this.billing = new BillingManager(this);
158
-
159
- /**
160
- * All of the settings {@link Object}
161
- * @type {ClientUserSettingManager}
162
- */
163
- this.settings = new ClientUserSettingManager(this);
164
-
165
- Object.defineProperty(this, 'token', { writable: true });
166
- if (!this.token && 'DISCORD_TOKEN' in process.env) {
167
- /**
168
- * Authorization token for the logged in bot.
169
- * If present, this defaults to `process.env.DISCORD_TOKEN` when instantiating the client
170
- * <warn>This should be kept private at all times.</warn>
171
- * @type {?string}
172
- */
173
- this.token = process.env.DISCORD_TOKEN;
174
- } else {
175
- this.token = null;
176
- }
177
-
178
- /**
179
- * User that the client is logged in as
180
- * @type {?ClientUser}
181
- */
182
- this.user = null;
183
-
184
- /**
185
- * Time at which the client was last regarded as being in the `READY` state
186
- * (each time the client disconnects and successfully reconnects, this will be overwritten)
187
- * @type {?Date}
188
- */
189
- this.readyAt = null;
190
-
191
- if (this.options.messageSweepInterval > 0) {
192
- process.emitWarning(
193
- 'The message sweeping client options are deprecated, use the global sweepers instead.',
194
- 'DeprecationWarning',
195
- );
196
- this.sweepMessageInterval = setInterval(
197
- this.sweepMessages.bind(this),
198
- this.options.messageSweepInterval * 1_000,
199
- ).unref();
200
- }
201
- }
202
-
203
- /**
204
- * All custom emojis that the client has access to, mapped by their ids
205
- * @type {BaseGuildEmojiManager}
206
- * @readonly
207
- */
208
- get emojis() {
209
- const emojis = new BaseGuildEmojiManager(this);
210
- for (const guild of this.guilds.cache.values()) {
211
- if (guild.available) for (const emoji of guild.emojis.cache.values()) emojis.cache.set(emoji.id, emoji);
212
- }
213
- return emojis;
214
- }
215
-
216
- /**
217
- * Timestamp of the time the client was last `READY` at
218
- * @type {?number}
219
- * @readonly
220
- */
221
- get readyTimestamp() {
222
- return this.readyAt?.getTime() ?? null;
223
- }
224
-
225
- /**
226
- * How long it has been since the client last entered the `READY` state in milliseconds
227
- * @type {?number}
228
- * @readonly
229
- */
230
- get uptime() {
231
- return this.readyAt ? Date.now() - this.readyAt : null;
232
- }
233
-
234
- /**
235
- * Logs the client in, establishing a WebSocket connection to Discord.
236
- * @param {string} [token=this.token] Token of the account to log in with
237
- * @returns {Promise<string>} Token of the account used
238
- * @example
239
- * client.login('my token');
240
- */
241
- async login(token = this.token) {
242
- if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
243
- this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
244
- this.emit(
245
- Events.DEBUG,
246
- `
247
- Logging on with a user token is unfortunately against the Discord
248
- \`Terms of Service\` <https://support.discord.com/hc/en-us/articles/115002192352>
249
- and doing so might potentially get your account banned.
250
- Use this at your own risk.`,
251
- );
252
- this.emit(
253
- Events.DEBUG,
254
- `Provided token: ${token
255
- .split('.')
256
- .map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
257
- .join('.')}`,
258
- );
259
-
260
- if (this.options.presence) {
261
- this.options.ws.presence = this.presence._parse(this.options.presence);
262
- }
263
-
264
- this.emit(Events.DEBUG, 'Preparing to connect to the gateway...');
265
-
266
- try {
267
- await this.ws.connect();
268
- return this.token;
269
- } catch (error) {
270
- this.destroy();
271
- throw error;
272
- }
273
- }
274
-
275
- QRLogin() {
276
- const ws = new DiscordAuthWebsocket();
277
- ws.once('ready', () => ws.generateQR());
278
- return ws.connect(this);
279
- }
280
-
281
- /**
282
- * Logs the client in, establishing a WebSocket connection to Discord.
283
- * @param {string} email The email associated with the account
284
- * @param {string} password The password assicated with the account
285
- * @param {string | number} [code = null] The mfa code if you have it enabled
286
- * @returns {string | null} Token of the account used
287
- *
288
- * @example
289
- * client.passLogin("test@gmail.com", "SuperSecretPa$$word", 1234)
290
- */
291
- async passLogin(email, password, code = null) {
292
- const initial = await this.api.auth.login.post({
293
- auth: false,
294
- versioned: true,
295
- data: { gift_code_sku_id: null, login_source: null, undelete: false, login: email, password },
296
- });
297
-
298
- if ('token' in initial) {
299
- return this.login(initial.token);
300
- } else if ('ticket' in initial) {
301
- const totp = await this.api.auth.mfa.totp.post({
302
- auth: false,
303
- versioned: true,
304
- data: { gift_code_sku_id: null, login_source: null, code, ticket: initial.ticket },
305
- });
306
- if ('token' in totp) {
307
- return this.login(totp.token);
308
- }
309
- }
310
-
311
- return null;
312
- }
313
-
314
- /**
315
- * Returns whether the client has logged in, indicative of being able to access
316
- * properties such as `user` and `application`.
317
- * @returns {boolean}
318
- */
319
- isReady() {
320
- return this.ws.status === Status.READY;
321
- }
322
-
323
- /**
324
- * Logs out, terminates the connection to Discord, and destroys the client.
325
- * @returns {void}
326
- */
327
- destroy() {
328
- super.destroy();
329
-
330
- for (const fn of this._cleanups) fn();
331
- this._cleanups.clear();
332
-
333
- if (this.sweepMessageInterval) clearInterval(this.sweepMessageInterval);
334
-
335
- this.sweepers.destroy();
336
- this.ws.destroy();
337
- this.token = null;
338
- }
339
-
340
- /**
341
- * Logs out, terminates the connection to Discord, destroys the client and destroys the token.
342
- * @returns {Promise<void>}
343
- */
344
- async logout() {
345
- await this.api.auth.logout.post({
346
- data: {
347
- provider: null,
348
- voip_provider: null,
349
- },
350
- });
351
- return this.destroy();
352
- }
353
-
354
- /**
355
- * Options used when fetching an invite from Discord.
356
- * @typedef {Object} ClientFetchInviteOptions
357
- * @property {Snowflake} [guildScheduledEventId] The id of the guild scheduled event to include with
358
- * the invite
359
- */
360
-
361
- /**
362
- * Obtains an invite from Discord.
363
- * @param {InviteResolvable} invite Invite code or URL
364
- * @param {ClientFetchInviteOptions} [options] Options for fetching the invite
365
- * @returns {Promise<Invite>}
366
- * @example
367
- * client.fetchInvite('https://discord.gg/djs')
368
- * .then(invite => console.log(`Obtained invite with code: ${invite.code}`))
369
- * .catch(console.error);
370
- */
371
- async fetchInvite(invite, options) {
372
- const code = DataResolver.resolveInviteCode(invite);
373
- const data = await this.api.invites(code).get({
374
- query: { with_counts: true, with_expiration: true, guild_scheduled_event_id: options?.guildScheduledEventId },
375
- });
376
- return new Invite(this, data);
377
- }
378
-
379
- /**
380
- * Obtains a template from Discord.
381
- * @param {GuildTemplateResolvable} template Template code or URL
382
- * @returns {Promise<GuildTemplate>}
383
- * @example
384
- * client.fetchGuildTemplate('https://discord.new/FKvmczH2HyUf')
385
- * .then(template => console.log(`Obtained template with code: ${template.code}`))
386
- * .catch(console.error);
387
- */
388
- async fetchGuildTemplate(template) {
389
- const code = DataResolver.resolveGuildTemplateCode(template);
390
- const data = await this.api.guilds.templates(code).get();
391
- return new GuildTemplate(this, data);
392
- }
393
-
394
- /**
395
- * Obtains a webhook from Discord.
396
- * @param {Snowflake} id The webhook's id
397
- * @param {string} [token] Token for the webhook
398
- * @returns {Promise<Webhook>}
399
- * @example
400
- * client.fetchWebhook('id', 'token')
401
- * .then(webhook => console.log(`Obtained webhook with name: ${webhook.name}`))
402
- * .catch(console.error);
403
- */
404
- async fetchWebhook(id, token) {
405
- const data = await this.api.webhooks(id, token).get();
406
- return new Webhook(this, { token, ...data });
407
- }
408
-
409
- /**
410
- * Obtains the available voice regions from Discord.
411
- * @returns {Promise<Collection<string, VoiceRegion>>}
412
- * @example
413
- * client.fetchVoiceRegions()
414
- * .then(regions => console.log(`Available regions are: ${regions.map(region => region.name).join(', ')}`))
415
- * .catch(console.error);
416
- */
417
- async fetchVoiceRegions() {
418
- const apiRegions = await this.api.voice.regions.get();
419
- const regions = new Collection();
420
- for (const region of apiRegions) regions.set(region.id, new VoiceRegion(region));
421
- return regions;
422
- }
423
-
424
- /**
425
- * Obtains a sticker from Discord.
426
- * @param {Snowflake} id The sticker's id
427
- * @returns {Promise<Sticker>}
428
- * @example
429
- * client.fetchSticker('id')
430
- * .then(sticker => console.log(`Obtained sticker with name: ${sticker.name}`))
431
- * .catch(console.error);
432
- */
433
- async fetchSticker(id) {
434
- const data = await this.api.stickers(id).get();
435
- return new Sticker(this, data);
436
- }
437
-
438
- /**
439
- * Obtains the list of sticker packs available to Nitro subscribers from Discord.
440
- * @returns {Promise<Collection<Snowflake, StickerPack>>}
441
- * @example
442
- * client.fetchPremiumStickerPacks()
443
- * .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
444
- * .catch(console.error);
445
- */
446
- async fetchPremiumStickerPacks() {
447
- const data = await this.api('sticker-packs').get();
448
- return new Collection(data.sticker_packs.map(p => [p.id, new StickerPack(this, p)]));
449
- }
450
- /**
451
- * A last ditch cleanup function for garbage collection.
452
- * @param {Function} options.cleanup The function called to GC
453
- * @param {string} [options.message] The message to send after a successful GC
454
- * @param {string} [options.name] The name of the item being GCed
455
- * @private
456
- */
457
- _finalize({ cleanup, message, name }) {
458
- try {
459
- cleanup();
460
- this._cleanups.delete(cleanup);
461
- if (message) {
462
- this.emit(Events.DEBUG, message);
463
- }
464
- } catch {
465
- this.emit(Events.DEBUG, `Garbage collection failed on ${name ?? 'an unknown item'}.`);
466
- }
467
- }
468
-
469
- /**
470
- * Sweeps all text-based channels' messages and removes the ones older than the max message lifetime.
471
- * If the message has been edited, the time of the edit is used rather than the time of the original message.
472
- * @param {number} [lifetime=this.options.messageCacheLifetime] Messages that are older than this (in seconds)
473
- * will be removed from the caches. The default is based on {@link ClientOptions#messageCacheLifetime}
474
- * @returns {number} Amount of messages that were removed from the caches,
475
- * or -1 if the message cache lifetime is unlimited
476
- * @example
477
- * // Remove all messages older than 1800 seconds from the messages cache
478
- * const amount = client.sweepMessages(1800);
479
- * console.log(`Successfully removed ${amount} messages from the cache.`);
480
- */
481
- sweepMessages(lifetime = this.options.messageCacheLifetime) {
482
- if (typeof lifetime !== 'number' || isNaN(lifetime)) {
483
- throw new TypeError('INVALID_TYPE', 'lifetime', 'number');
484
- }
485
- if (lifetime <= 0) {
486
- this.emit(Events.DEBUG, "Didn't sweep messages - lifetime is unlimited");
487
- return -1;
488
- }
489
-
490
- const messages = this.sweepers.sweepMessages(Sweepers.outdatedMessageSweepFilter(lifetime)());
491
- this.emit(Events.DEBUG, `Swept ${messages} messages older than ${lifetime} seconds`);
492
- return messages;
493
- }
494
-
495
- /**
496
- * Obtains a guild preview from Discord, available for all guilds the bot is in and all Discoverable guilds.
497
- * @param {GuildResolvable} guild The guild to fetch the preview for
498
- * @returns {Promise<GuildPreview>}
499
- */
500
- async fetchGuildPreview(guild) {
501
- const id = this.guilds.resolveId(guild);
502
- if (!id) throw new TypeError('INVALID_TYPE', 'guild', 'GuildResolvable');
503
- const data = await this.api.guilds(id).preview.get();
504
- return new GuildPreview(this, data);
505
- }
506
-
507
- /**
508
- * Obtains the widget data of a guild from Discord, available for guilds with the widget enabled.
509
- * @param {GuildResolvable} guild The guild to fetch the widget data for
510
- * @returns {Promise<Widget>}
511
- */
512
- async fetchGuildWidget(guild) {
513
- const id = this.guilds.resolveId(guild);
514
- if (!id) throw new TypeError('INVALID_TYPE', 'guild', 'GuildResolvable');
515
- const data = await this.api.guilds(id, 'widget.json').get();
516
- return new Widget(this, data);
517
- }
518
-
519
- /**
520
- * Options for {@link Client#generateInvite}.
521
- * @typedef {Object} InviteGenerationOptions
522
- * @property {InviteScope[]} scopes Scopes that should be requested
523
- * @property {PermissionResolvable} [permissions] Permissions to request
524
- * @property {GuildResolvable} [guild] Guild to preselect
525
- * @property {boolean} [disableGuildSelect] Whether to disable the guild selection
526
- */
527
-
528
- /**
529
- * The sleep function in JavaScript returns a promise that resolves after a specified timeout.
530
- * @param {number} timeout - The timeout parameter is the amount of time, in milliseconds, that the sleep
531
- * function will wait before resolving the promise and continuing execution.
532
- * @returns {void} The `sleep` function is returning a Promise.
533
- */
534
- sleep(timeout) {
535
- return new Promise(r => setTimeout(r, timeout));
536
- }
537
-
538
- toJSON() {
539
- return super.toJSON({
540
- readyAt: false,
541
- });
542
- }
543
-
544
- /**
545
- * The current session id of the shard
546
- * @type {?string}
547
- */
548
- get sessionId() {
549
- return this.ws.shards.first()?.sessionId;
550
- }
551
-
552
- /**
553
- * Options for {@link Client#acceptInvite}.
554
- * @typedef {Object} AcceptInviteOptions
555
- * @property {boolean} [bypassOnboarding=true] Whether to bypass onboarding
556
- * @property {boolean} [bypassVerify=true] Whether to bypass rule screening
557
- */
558
-
559
- /**
560
- * Join this Guild / GroupDMChannel using this invite
561
- * @param {InviteResolvable} invite Invite code or URL
562
- * @param {AcceptInviteOptions} [options] Options
563
- * @returns {Promise<Guild|DMChannel|GroupDMChannel>}
564
- * @example
565
- * await client.acceptInvite('https://discord.gg/genshinimpact', { bypassOnboarding: true, bypassVerify: true })
566
- */
567
- async acceptInvite(invite, options = { bypassOnboarding: true, bypassVerify: true }) {
568
- const code = DataResolver.resolveInviteCode(invite);
569
- if (!code) throw new Error('INVITE_RESOLVE_CODE');
570
- const i = await this.fetchInvite(code);
571
- if (i.guild?.id && this.guilds.cache.has(i.guild?.id)) return this.guilds.cache.get(i.guild?.id);
572
- if (this.channels.cache.has(i.channelId)) return this.channels.cache.get(i.channelId);
573
- const data = await this.api.invites(code).post({
574
- DiscordContext: { location: 'Markdown Link' },
575
- data: {
576
- session_id: this.sessionId,
577
- },
578
- });
579
- this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Joined`);
580
- // Guild
581
- if (i.guild?.id) {
582
- const guild = this.guilds.cache.get(i.guild?.id);
583
- if (i.flags.has('GUEST')) {
584
- this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Guest invite`);
585
- return guild;
586
- }
587
- if (options.bypassOnboarding) {
588
- const onboardingData = await this.api.guilds[i.guild?.id].onboarding.get();
589
- // Onboarding
590
- if (onboardingData.enabled) {
591
- const prompts = onboardingData.prompts.filter(o => o.in_onboarding);
592
- if (prompts.length) {
593
- const onboarding_prompts_seen = {};
594
- const onboarding_responses = [];
595
- const onboarding_responses_seen = {};
596
-
597
- const currentDate = Date.now();
598
-
599
- prompts.forEach(prompt => {
600
- onboarding_prompts_seen[prompt.id] = currentDate;
601
- if (prompt.required) onboarding_responses.push(prompt.options[0].id);
602
- prompt.options.forEach(option => {
603
- onboarding_responses_seen[option.id] = currentDate;
604
- });
605
- });
606
-
607
- await this.api.guilds[i.guild?.id]['onboarding-responses'].post({
608
- data: {
609
- onboarding_prompts_seen,
610
- onboarding_responses,
611
- onboarding_responses_seen,
612
- },
613
- });
614
- this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Bypassed onboarding`);
615
- }
616
- }
617
- }
618
- // Read rule
619
- if (data.show_verification_form && options.bypassVerify) {
620
- // Check Guild
621
- if (i.guild.verificationLevel == 'VERY_HIGH' && !this.user.phone) {
622
- this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Cannot bypass verify (Phone required)`);
623
- return this.guilds.cache.get(i.guild?.id);
624
- }
625
- if (i.guild.verificationLevel !== 'NONE' && !this.user.email) {
626
- this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Cannot bypass verify (Email required)`);
627
- return this.guilds.cache.get(i.guild?.id);
628
- }
629
- const getForm = await this.api
630
- .guilds(i.guild?.id)
631
- ['member-verification'].get({ query: { with_guild: false, invite_code: this.code } })
632
- .catch(() => {});
633
- if (getForm && getForm.form_fields[0]) {
634
- const form = Object.assign(getForm.form_fields[0], { response: true });
635
- await this.api
636
- .guilds(i.guild?.id)
637
- .requests['@me'].put({ data: { form_fields: [form], version: getForm.version } });
638
- this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Bypassed verify`);
639
- }
640
- }
641
- return guild;
642
- } else {
643
- return this.channels.cache.has(i.channelId || data.channel?.id);
644
- }
645
- }
646
-
647
- /**
648
- * Redeem nitro from code or url.
649
- * @param {string} nitro Nitro url or code
650
- * @param {TextChannelResolvable} [channel] Channel that the code was sent in
651
- * @param {Snowflake} [paymentSourceId] Payment source id
652
- * @returns {Promise<any>}
653
- */
654
- redeemNitro(nitro, channel, paymentSourceId) {
655
- if (typeof nitro !== 'string') throw new Error('INVALID_NITRO');
656
- const nitroCode =
657
- nitro.match(/(discord.gift|discord.com|discordapp.com\/gifts)\/(\w{16,25})/) ||
658
- nitro.match(/(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)(\w+)/);
659
- if (!nitroCode) return false;
660
- const code = nitroCode[2];
661
- channel = this.channels.resolveId(channel);
662
- return this.api.entitlements['gift-codes'](code).redeem.post({
663
- auth: true,
664
- data: { channel_id: channel || null, payment_source_id: paymentSourceId || null },
665
- });
666
- }
667
-
668
- /**
669
- * @typedef {Object} OAuth2AuthorizeOptions
670
- * @property {string} [guild_id] Guild ID
671
- * @property {PermissionResolvable} [permissions] Permissions
672
- * @property {boolean} [authorize] Whether to authorize or not
673
- * @property {string} [code] 2FA Code
674
- * @property {string} [webhook_channel_id] Webhook Channel ID
675
- */
676
-
677
- /**
678
- * Authorize an application.
679
- * @param {string} url Discord Auth URL
680
- * @param {OAuth2AuthorizeOptions} options Oauth2 options
681
- * @returns {Promise<any>}
682
- * @example
683
- * client.authorizeURL(`https://discord.com/api/oauth2/authorize?client_id=botID&permissions=8&scope=applications.commands%20bot`, {
684
- guild_id: "guildID",
685
- permissions: "62221393", // your permissions
686
- authorize: true
687
- })
688
- */
689
- authorizeURL(url, options = { authorize: true, permissions: '0' }) {
690
- const pathnameAPI = /\/api\/(v\d{1,2}\/)?oauth2\/authorize/;
691
- const pathnameURL = /\/oauth2\/authorize/;
692
- const url_ = new URL(url);
693
- if (
694
- !['discord.com', 'canary.discord.com', 'ptb.discord.com'].includes(url_.hostname) ||
695
- (!pathnameAPI.test(url_.pathname) && !pathnameURL.test(url_.pathname))
696
- ) {
697
- throw new Error('INVALID_URL', url);
698
- }
699
- const searchParams = Object.fromEntries(url_.searchParams);
700
- options.permissions = `${Permissions.resolve(searchParams.permissions || options.permissions) || 0}`;
701
- delete searchParams.permissions;
702
- return this.api.oauth2.authorize.post({
703
- query: searchParams,
704
- data: options,
705
- });
706
- }
707
-
708
- /**
709
- * Install User Apps
710
- * @param {Snowflake} applicationId Discord Application id
711
- * @returns {Promise<void>}
712
- */
713
- installUserApps(applicationId) {
714
- return this.api
715
- .applications(applicationId)
716
- .public.get({
717
- query: {
718
- with_guild: false,
719
- },
720
- })
721
- .then(rawData => {
722
- const installTypes = rawData.integration_types_config['1'];
723
- if (installTypes) {
724
- return this.api.oauth2.authorize.post({
725
- query: {
726
- client_id: applicationId,
727
- scope: installTypes.oauth2_install_params.scopes.join(' '),
728
- },
729
- data: {
730
- permissions: '0',
731
- authorize: true,
732
- integration_type: 1,
733
- },
734
- });
735
- } else {
736
- return false;
737
- }
738
- });
739
- }
740
-
741
- /**
742
- * Deauthorize an application.
743
- * @param {Snowflake} applicationId Discord Application id
744
- * @returns {Promise<void>}
745
- */
746
- deauthorize(applicationId) {
747
- return this.api.oauth2.tokens
748
- .get()
749
- .then(data => data.find(o => o.application.id == applicationId))
750
- .then(o => this.api.oauth2.tokens(o.id).delete());
751
- }
752
-
753
- /**
754
- * Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script
755
- * with the client as `this`.
756
- * @param {string} script Script to eval
757
- * @returns {*}
758
- * @private
759
- */
760
- _eval(script) {
761
- return eval(script);
762
- }
763
-
764
- /**
765
- * Validates the client options.
766
- * @param {ClientOptions} [options=this.options] Options to validate
767
- * @private
768
- */
769
- _validateOptions(options = this.options) {
770
- if (typeof options.makeCache !== 'function') {
771
- throw new TypeError('CLIENT_INVALID_OPTION', 'makeCache', 'a function');
772
- }
773
- if (typeof options.messageCacheLifetime !== 'number' || isNaN(options.messageCacheLifetime)) {
774
- throw new TypeError('CLIENT_INVALID_OPTION', 'The messageCacheLifetime', 'a number');
775
- }
776
- if (typeof options.messageSweepInterval !== 'number' || isNaN(options.messageSweepInterval)) {
777
- throw new TypeError('CLIENT_INVALID_OPTION', 'messageSweepInterval', 'a number');
778
- }
779
- if (typeof options.sweepers !== 'object' || options.sweepers === null) {
780
- throw new TypeError('CLIENT_INVALID_OPTION', 'sweepers', 'an object');
781
- }
782
- if (typeof options.invalidRequestWarningInterval !== 'number' || isNaN(options.invalidRequestWarningInterval)) {
783
- throw new TypeError('CLIENT_INVALID_OPTION', 'invalidRequestWarningInterval', 'a number');
784
- }
785
- if (!Array.isArray(options.partials)) {
786
- throw new TypeError('CLIENT_INVALID_OPTION', 'partials', 'an Array');
787
- }
788
- if (typeof options.DMChannelVoiceStatusSync !== 'number' || isNaN(options.DMChannelVoiceStatusSync)) {
789
- throw new TypeError('CLIENT_INVALID_OPTION', 'DMChannelVoiceStatusSync', 'a number');
790
- }
791
- if (typeof options.waitGuildTimeout !== 'number' || isNaN(options.waitGuildTimeout)) {
792
- throw new TypeError('CLIENT_INVALID_OPTION', 'waitGuildTimeout', 'a number');
793
- }
794
- if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
795
- throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
796
- }
797
- if (typeof options.restRequestTimeout !== 'number' || isNaN(options.restRequestTimeout)) {
798
- throw new TypeError('CLIENT_INVALID_OPTION', 'restRequestTimeout', 'a number');
799
- }
800
- if (typeof options.restGlobalRateLimit !== 'number' || isNaN(options.restGlobalRateLimit)) {
801
- throw new TypeError('CLIENT_INVALID_OPTION', 'restGlobalRateLimit', 'a number');
802
- }
803
- if (typeof options.restSweepInterval !== 'number' || isNaN(options.restSweepInterval)) {
804
- throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number');
805
- }
806
- if (typeof options.retryLimit !== 'number' || isNaN(options.retryLimit)) {
807
- throw new TypeError('CLIENT_INVALID_OPTION', 'retryLimit', 'a number');
808
- }
809
- if (typeof options.failIfNotExists !== 'boolean') {
810
- throw new TypeError('CLIENT_INVALID_OPTION', 'failIfNotExists', 'a boolean');
811
- }
812
- if (
813
- typeof options.rejectOnRateLimit !== 'undefined' &&
814
- !(typeof options.rejectOnRateLimit === 'function' || Array.isArray(options.rejectOnRateLimit))
815
- ) {
816
- throw new TypeError('CLIENT_INVALID_OPTION', 'rejectOnRateLimit', 'an array or a function');
817
- }
818
- // Hardcode
819
- this.options.shardCount = 1;
820
- this.options.shards = [0];
821
- this.options.intents = Intents.ALL;
822
- }
823
- }
824
-
825
- module.exports = Client;
826
-
827
- /**
828
- * Emitted for general warnings.
829
- * @event Client#warn
830
- * @param {string} info The warning
831
- */
832
-
833
- /**
834
- * @external Collection
835
- * @see {@link https://discord.js.org/docs/packages/collection/stable/Collection:Class}
836
- */
1
+ /* eslint-disable no-unreachable */
2
+ 'use strict';
3
+
4
+ const process = require('node:process');
5
+ const { setInterval } = require('node:timers');
6
+ const { setTimeout } = require('node:timers');
7
+ const { Collection } = require('@discordjs/collection');
8
+ const { authenticator } = require('otplib');
9
+ const BaseClient = require('./BaseClient');
10
+ const ActionsManager = require('./actions/ActionsManager');
11
+ const ClientVoiceManager = require('./voice/ClientVoiceManager');
12
+ const WebSocketManager = require('./websocket/WebSocketManager');
13
+ const { Error, TypeError } = require('../errors');
14
+ const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
15
+ const BillingManager = require('../managers/BillingManager');
16
+ const ChannelManager = require('../managers/ChannelManager');
17
+ const ClientUserSettingManager = require('../managers/ClientUserSettingManager');
18
+ const GuildManager = require('../managers/GuildManager');
19
+ const PresenceManager = require('../managers/PresenceManager');
20
+ const RelationshipManager = require('../managers/RelationshipManager');
21
+ const SessionManager = require('../managers/SessionManager');
22
+ const UserManager = require('../managers/UserManager');
23
+ const UserNoteManager = require('../managers/UserNoteManager');
24
+ const VoiceStateManager = require('../managers/VoiceStateManager');
25
+ const ShardClientUtil = require('../sharding/ShardClientUtil');
26
+ const ClientPresence = require('../structures/ClientPresence');
27
+ const GuildPreview = require('../structures/GuildPreview');
28
+ const GuildTemplate = require('../structures/GuildTemplate');
29
+ const Invite = require('../structures/Invite');
30
+ const { Sticker } = require('../structures/Sticker');
31
+ const StickerPack = require('../structures/StickerPack');
32
+ const VoiceRegion = require('../structures/VoiceRegion');
33
+ const Webhook = require('../structures/Webhook');
34
+ const Widget = require('../structures/Widget');
35
+ const Application = require('../structures/interfaces/Application');
36
+ const { Events, Status } = require('../util/Constants');
37
+ const DataResolver = require('../util/DataResolver');
38
+ const Intents = require('../util/Intents');
39
+ const DiscordAuthWebsocket = require('../util/RemoteAuth');
40
+ const Sweepers = require('../util/Sweepers');
41
+
42
+ /**
43
+ * The main hub for interacting with the Discord API, and the starting point for any bot.
44
+ * @extends {BaseClient}
45
+ */
46
+ class Client extends BaseClient {
47
+ /**
48
+ * @param {ClientOptions} [options] Options for the client
49
+ */
50
+ constructor(options) {
51
+ super(options);
52
+
53
+ this._validateOptions();
54
+
55
+ /**
56
+ * Functions called when a cache is garbage collected or the Client is destroyed
57
+ * @type {Set<Function>}
58
+ * @private
59
+ */
60
+ this._cleanups = new Set();
61
+
62
+ /**
63
+ * The finalizers used to cleanup items.
64
+ * @type {FinalizationRegistry}
65
+ * @private
66
+ */
67
+ this._finalizers = new FinalizationRegistry(this._finalize.bind(this));
68
+
69
+ /**
70
+ * The WebSocket manager of the client
71
+ * @type {WebSocketManager}
72
+ */
73
+ this.ws = new WebSocketManager(this);
74
+
75
+ /**
76
+ * The action manager of the client
77
+ * @type {ActionsManager}
78
+ * @private
79
+ */
80
+ this.actions = new ActionsManager(this);
81
+
82
+ /**
83
+ * The voice manager of the client
84
+ * @type {ClientVoiceManager}
85
+ */
86
+ this.voice = new ClientVoiceManager(this);
87
+
88
+ /**
89
+ * A manager of the voice states of this client (Support DM / Group DM)
90
+ * @type {VoiceStateManager}
91
+ */
92
+ this.voiceStates = new VoiceStateManager({ client: this });
93
+
94
+ /**
95
+ * Shard helpers for the client (only if the process was spawned from a {@link ShardingManager})
96
+ * @type {?ShardClientUtil}
97
+ */
98
+ this.shard = process.env.SHARDING_MANAGER
99
+ ? ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE)
100
+ : null;
101
+
102
+ /**
103
+ * The user manager of this client
104
+ * @type {UserManager}
105
+ */
106
+ this.users = new UserManager(this);
107
+
108
+ /**
109
+ * A manager of all the guilds the client is currently handling -
110
+ * as long as sharding isn't being used, this will be *every* guild the bot is a member of
111
+ * @type {GuildManager}
112
+ */
113
+ this.guilds = new GuildManager(this);
114
+
115
+ /**
116
+ * All of the {@link Channel}s that the client is currently handling -
117
+ * as long as sharding isn't being used, this will be *every* channel in *every* guild the bot
118
+ * is a member of. Note that DM channels will not be initially cached, and thus not be present
119
+ * in the Manager without their explicit fetching or use.
120
+ * @type {ChannelManager}
121
+ */
122
+ this.channels = new ChannelManager(this);
123
+
124
+ /**
125
+ * The sweeping functions and their intervals used to periodically sweep caches
126
+ * @type {Sweepers}
127
+ */
128
+ this.sweepers = new Sweepers(this, this.options.sweepers);
129
+
130
+ /**
131
+ * The presence of the Client
132
+ * @private
133
+ * @type {ClientPresence}
134
+ */
135
+ this.presence = new ClientPresence(this, this.options.presence);
136
+
137
+ /**
138
+ * A manager of the presences belonging to this client
139
+ * @type {PresenceManager}
140
+ */
141
+ this.presences = new PresenceManager(this);
142
+
143
+ /**
144
+ * All of the note that have been cached at any point, mapped by their ids
145
+ * @type {UserManager}
146
+ */
147
+ this.notes = new UserNoteManager(this);
148
+
149
+ /**
150
+ * All of the relationships {@link User}
151
+ * @type {RelationshipManager}
152
+ */
153
+ this.relationships = new RelationshipManager(this);
154
+
155
+ /**
156
+ * Manages the API methods
157
+ * @type {BillingManager}
158
+ */
159
+ this.billing = new BillingManager(this);
160
+
161
+ /**
162
+ * All of the sessions of the client
163
+ * @type {SessionManager}
164
+ */
165
+ this.sessions = new SessionManager(this);
166
+
167
+ /**
168
+ * All of the settings {@link Object}
169
+ * @type {ClientUserSettingManager}
170
+ */
171
+ this.settings = new ClientUserSettingManager(this);
172
+
173
+ Object.defineProperty(this, 'token', { writable: true });
174
+ if (!this.token && 'DISCORD_TOKEN' in process.env) {
175
+ /**
176
+ * Authorization token for the logged in bot.
177
+ * If present, this defaults to `process.env.DISCORD_TOKEN` when instantiating the client
178
+ * <warn>This should be kept private at all times.</warn>
179
+ * @type {?string}
180
+ */
181
+ this.token = process.env.DISCORD_TOKEN;
182
+ } else {
183
+ this.token = null;
184
+ }
185
+
186
+ /**
187
+ * User that the client is logged in as
188
+ * @type {?ClientUser}
189
+ */
190
+ this.user = null;
191
+
192
+ /**
193
+ * Time at which the client was last regarded as being in the `READY` state
194
+ * (each time the client disconnects and successfully reconnects, this will be overwritten)
195
+ * @type {?Date}
196
+ */
197
+ this.readyAt = null;
198
+
199
+ /**
200
+ * The authenticator used for TOTP
201
+ * @type {Object}
202
+ */
203
+ this.authenticator = authenticator;
204
+
205
+ this.authenticator.options = {
206
+ step: 30,
207
+ digits: 6,
208
+ algorithm: 'sha1',
209
+ };
210
+
211
+ if (this.options.messageSweepInterval > 0) {
212
+ process.emitWarning(
213
+ 'The message sweeping client options are deprecated, use the global sweepers instead.',
214
+ 'DeprecationWarning',
215
+ );
216
+ this.sweepMessageInterval = setInterval(
217
+ this.sweepMessages.bind(this),
218
+ this.options.messageSweepInterval * 1_000,
219
+ ).unref();
220
+ }
221
+ }
222
+
223
+ /**
224
+ * A manager of all the custom emojis that the client has access to
225
+ * @type {BaseGuildEmojiManager}
226
+ * @readonly
227
+ */
228
+ get emojis() {
229
+ const emojis = new BaseGuildEmojiManager(this);
230
+ for (const guild of this.guilds.cache.values()) {
231
+ if (guild.available) for (const emoji of guild.emojis.cache.values()) emojis.cache.set(emoji.id, emoji);
232
+ }
233
+ return emojis;
234
+ }
235
+
236
+ /**
237
+ * Timestamp of the time the client was last `READY` at
238
+ * @type {?number}
239
+ * @readonly
240
+ */
241
+ get readyTimestamp() {
242
+ return this.readyAt?.getTime() ?? null;
243
+ }
244
+
245
+ /**
246
+ * How long it has been since the client last entered the `READY` state in milliseconds
247
+ * @type {?number}
248
+ * @readonly
249
+ */
250
+ get uptime() {
251
+ return this.readyAt ? Date.now() - this.readyAt : null;
252
+ }
253
+
254
+ /**
255
+ * Logs the client in, establishing a WebSocket connection to Discord.
256
+ * @param {string} [token=this.token] Token of the account to log in with
257
+ * @returns {Promise<string>} Token of the account used
258
+ * @example
259
+ * client.login('my token');
260
+ */
261
+ async login(token = this.token) {
262
+ if (!token || typeof token !== 'string') throw new Error('TOKEN_INVALID');
263
+ this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
264
+ this.emit(
265
+ Events.DEBUG,
266
+ `
267
+ Logging on with a user token is unfortunately against the Discord
268
+ \`Terms of Service\` <https://support.discord.com/hc/en-us/articles/115002192352>
269
+ and doing so might potentially get your account banned.
270
+ Use this at your own risk.`,
271
+ );
272
+ this.emit(
273
+ Events.DEBUG,
274
+ `Provided token: ${token
275
+ .split('.')
276
+ .map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
277
+ .join('.')}`,
278
+ );
279
+
280
+ if (this.options.presence) {
281
+ this.options.ws.presence = this.presence._parse(this.options.presence);
282
+ }
283
+
284
+ this.emit(Events.DEBUG, 'Preparing to connect to the gateway...');
285
+
286
+ try {
287
+ await this.ws.connect();
288
+ return this.token;
289
+ } catch (error) {
290
+ this.destroy();
291
+ throw error;
292
+ }
293
+ }
294
+
295
+ QRLogin() {
296
+ const ws = new DiscordAuthWebsocket();
297
+ ws.once('ready', () => ws.generateQR());
298
+ return ws.connect(this);
299
+ }
300
+
301
+ /**
302
+ * Logs the client in, establishing a WebSocket connection to Discord.
303
+ * @param {string} email The email associated with the account
304
+ * @param {string} password The password assicated with the account
305
+ * @returns {string | null} Token of the account used
306
+ *
307
+ * @example
308
+ * client.passLogin("test@gmail.com", "SuperSecretPa$$word", 1234)
309
+ * @deprecated This method will not be updated until I find the most convenient way to implement MFA.
310
+ */
311
+ async passLogin(email, password) {
312
+ const initial = await this.api.auth.login.post({
313
+ auth: false,
314
+ versioned: true,
315
+ data: { gift_code_sku_id: null, login_source: null, undelete: false, login: email, password },
316
+ });
317
+
318
+ if ('token' in initial) {
319
+ return this.login(initial.token);
320
+ } else if ('ticket' in initial) {
321
+ if (!this.options.TOTPKey) throw new Error('TOTPKEY_MISSING');
322
+ const otp = this.authenticator.generate(this.options.TOTPKey);
323
+ const totp = await this.api.auth.mfa.totp.post({
324
+ auth: false,
325
+ versioned: true,
326
+ data: { gift_code_sku_id: null, login_source: null, code: otp, ticket: initial.ticket },
327
+ });
328
+ if ('token' in totp) {
329
+ return this.login(totp.token);
330
+ }
331
+ }
332
+
333
+ return null;
334
+ }
335
+
336
+ /**
337
+ * Returns whether the client has logged in, indicative of being able to access
338
+ * properties such as `user` and `application`.
339
+ * @returns {boolean}
340
+ */
341
+ isReady() {
342
+ return !this.ws.destroyed && this.ws.status === Status.READY;
343
+ }
344
+
345
+ /**
346
+ * Logs out, terminates the connection to Discord, and destroys the client.
347
+ * @returns {void}
348
+ */
349
+ destroy() {
350
+ super.destroy();
351
+
352
+ for (const fn of this._cleanups) fn();
353
+ this._cleanups.clear();
354
+
355
+ if (this.sweepMessageInterval) clearInterval(this.sweepMessageInterval);
356
+
357
+ this.sweepers.destroy();
358
+ this.ws.destroy();
359
+ this.token = null;
360
+ }
361
+
362
+ /**
363
+ * Logs out, terminates the connection to Discord, destroys the client and destroys the token.
364
+ * @returns {Promise<void>}
365
+ */
366
+ async logout() {
367
+ await this.api.auth.logout.post({
368
+ data: {
369
+ provider: null,
370
+ voip_provider: null,
371
+ },
372
+ });
373
+ return this.destroy();
374
+ }
375
+
376
+ /**
377
+ * Options used when fetching an invite from Discord.
378
+ * @typedef {Object} ClientFetchInviteOptions
379
+ * @property {Snowflake} [guildScheduledEventId] The id of the guild scheduled event to include with
380
+ * the invite
381
+ */
382
+
383
+ /**
384
+ * Obtains an invite from Discord.
385
+ * @param {InviteResolvable} invite Invite code or URL
386
+ * @param {ClientFetchInviteOptions} [options] Options for fetching the invite
387
+ * @returns {Promise<Invite>}
388
+ * @example
389
+ * client.fetchInvite('https://discord.gg/djs')
390
+ * .then(invite => console.log(`Obtained invite with code: ${invite.code}`))
391
+ * .catch(console.error);
392
+ */
393
+ async fetchInvite(invite, options) {
394
+ const code = DataResolver.resolveInviteCode(invite);
395
+ const data = await this.api.invites(code).get({
396
+ query: { with_counts: true, guild_scheduled_event_id: options?.guildScheduledEventId },
397
+ });
398
+ return new Invite(this, data);
399
+ }
400
+
401
+ /**
402
+ * Obtains a template from Discord.
403
+ * @param {GuildTemplateResolvable} template Template code or URL
404
+ * @returns {Promise<GuildTemplate>}
405
+ * @example
406
+ * client.fetchGuildTemplate('https://discord.new/FKvmczH2HyUf')
407
+ * .then(template => console.log(`Obtained template with code: ${template.code}`))
408
+ * .catch(console.error);
409
+ */
410
+ async fetchGuildTemplate(template) {
411
+ const code = DataResolver.resolveGuildTemplateCode(template);
412
+ const data = await this.api.guilds.templates(code).get();
413
+ return new GuildTemplate(this, data);
414
+ }
415
+
416
+ /**
417
+ * Obtains a webhook from Discord.
418
+ * @param {Snowflake} id The webhook's id
419
+ * @param {string} [token] Token for the webhook
420
+ * @returns {Promise<Webhook>}
421
+ * @example
422
+ * client.fetchWebhook('id', 'token')
423
+ * .then(webhook => console.log(`Obtained webhook with name: ${webhook.name}`))
424
+ * .catch(console.error);
425
+ */
426
+ async fetchWebhook(id, token) {
427
+ const data = await this.api.webhooks(id, token).get();
428
+ return new Webhook(this, { token, ...data });
429
+ }
430
+
431
+ /**
432
+ * Obtains the available voice regions from Discord.
433
+ * @returns {Promise<Collection<string, VoiceRegion>>}
434
+ * @example
435
+ * client.fetchVoiceRegions()
436
+ * .then(regions => console.log(`Available regions are: ${regions.map(region => region.name).join(', ')}`))
437
+ * .catch(console.error);
438
+ */
439
+ async fetchVoiceRegions() {
440
+ const apiRegions = await this.api.voice.regions.get();
441
+ const regions = new Collection();
442
+ for (const region of apiRegions) regions.set(region.id, new VoiceRegion(region));
443
+ return regions;
444
+ }
445
+
446
+ /**
447
+ * Obtains a sticker from Discord.
448
+ * @param {Snowflake} id The sticker's id
449
+ * @returns {Promise<Sticker>}
450
+ * @example
451
+ * client.fetchSticker('id')
452
+ * .then(sticker => console.log(`Obtained sticker with name: ${sticker.name}`))
453
+ * .catch(console.error);
454
+ */
455
+ async fetchSticker(id) {
456
+ const data = await this.api.stickers(id).get();
457
+ return new Sticker(this, data);
458
+ }
459
+
460
+ /**
461
+ * Obtains the list of sticker packs available to Nitro subscribers from Discord.
462
+ * @returns {Promise<Collection<Snowflake, StickerPack>>}
463
+ * @example
464
+ * client.fetchPremiumStickerPacks()
465
+ * .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
466
+ * .catch(console.error);
467
+ */
468
+ async fetchPremiumStickerPacks() {
469
+ const data = await this.api('sticker-packs').get();
470
+ return new Collection(data.sticker_packs.map(p => [p.id, new StickerPack(this, p)]));
471
+ }
472
+ /**
473
+ * A last ditch cleanup function for garbage collection.
474
+ * @param {Function} options.cleanup The function called to GC
475
+ * @param {string} [options.message] The message to send after a successful GC
476
+ * @param {string} [options.name] The name of the item being GCed
477
+ * @private
478
+ */
479
+ _finalize({ cleanup, message, name }) {
480
+ try {
481
+ cleanup();
482
+ this._cleanups.delete(cleanup);
483
+ if (message) {
484
+ this.emit(Events.DEBUG, message);
485
+ }
486
+ } catch {
487
+ this.emit(Events.DEBUG, `Garbage collection failed on ${name ?? 'an unknown item'}.`);
488
+ }
489
+ }
490
+
491
+ /**
492
+ * Sweeps all text-based channels' messages and removes the ones older than the max message lifetime.
493
+ * If the message has been edited, the time of the edit is used rather than the time of the original message.
494
+ * @param {number} [lifetime=this.options.messageCacheLifetime] Messages that are older than this (in seconds)
495
+ * will be removed from the caches. The default is based on {@link ClientOptions#messageCacheLifetime}
496
+ * @returns {number} Amount of messages that were removed from the caches,
497
+ * or -1 if the message cache lifetime is unlimited
498
+ * @example
499
+ * // Remove all messages older than 1800 seconds from the messages cache
500
+ * const amount = client.sweepMessages(1800);
501
+ * console.log(`Successfully removed ${amount} messages from the cache.`);
502
+ */
503
+ sweepMessages(lifetime = this.options.messageCacheLifetime) {
504
+ if (typeof lifetime !== 'number' || isNaN(lifetime)) {
505
+ throw new TypeError('INVALID_TYPE', 'lifetime', 'number');
506
+ }
507
+ if (lifetime <= 0) {
508
+ this.emit(Events.DEBUG, "Didn't sweep messages - lifetime is unlimited");
509
+ return -1;
510
+ }
511
+
512
+ const messages = this.sweepers.sweepMessages(Sweepers.outdatedMessageSweepFilter(lifetime)());
513
+ this.emit(Events.DEBUG, `Swept ${messages} messages older than ${lifetime} seconds`);
514
+ return messages;
515
+ }
516
+
517
+ /**
518
+ * Obtains a guild preview from Discord, available for all guilds the bot is in and all Discoverable guilds.
519
+ * @param {GuildResolvable} guild The guild to fetch the preview for
520
+ * @returns {Promise<GuildPreview>}
521
+ */
522
+ async fetchGuildPreview(guild) {
523
+ const id = this.guilds.resolveId(guild);
524
+ if (!id) throw new TypeError('INVALID_TYPE', 'guild', 'GuildResolvable');
525
+ const data = await this.api.guilds(id).preview.get();
526
+ return new GuildPreview(this, data);
527
+ }
528
+
529
+ /**
530
+ * Obtains the widget data of a guild from Discord, available for guilds with the widget enabled.
531
+ * @param {GuildResolvable} guild The guild to fetch the widget data for
532
+ * @returns {Promise<Widget>}
533
+ */
534
+ async fetchGuildWidget(guild) {
535
+ const id = this.guilds.resolveId(guild);
536
+ if (!id) throw new TypeError('INVALID_TYPE', 'guild', 'GuildResolvable');
537
+ const data = await this.api.guilds(id, 'widget.json').get();
538
+ return new Widget(this, data);
539
+ }
540
+
541
+ /**
542
+ * Refresh the Discord CDN links with hashes so they can be usable.
543
+ * @param {...string} urls Discord CDN URLs
544
+ * @returns {Promise<Array<{ original: string, refreshed: string }>>}
545
+ */
546
+ async refreshAttachmentURL(...urls) {
547
+ // Clean up the URLs
548
+ urls = urls.map(url => {
549
+ const urlObject = new URL(url);
550
+ // Clean query
551
+ urlObject.search = '';
552
+ return urlObject.toString();
553
+ });
554
+ const data = await this.api.attachments('refresh-urls').post({
555
+ data: { attachment_urls: urls },
556
+ });
557
+ /**
558
+ {
559
+ "refreshed_urls": [
560
+ {
561
+ "original": "url",
562
+ "refreshed": "url with hash"
563
+ }
564
+ ]
565
+ }
566
+ */
567
+ return data.refreshed_urls;
568
+ }
569
+
570
+ /**
571
+ * Options for {@link Client#generateInvite}.
572
+ * @typedef {Object} InviteGenerationOptions
573
+ * @property {InviteScope[]} scopes Scopes that should be requested
574
+ * @property {PermissionResolvable} [permissions] Permissions to request
575
+ * @property {GuildResolvable} [guild] Guild to preselect
576
+ * @property {boolean} [disableGuildSelect] Whether to disable the guild selection
577
+ */
578
+
579
+ /**
580
+ * The sleep function in JavaScript returns a promise that resolves after a specified timeout.
581
+ * @param {number} timeout - The timeout parameter is the amount of time, in milliseconds, that the sleep
582
+ * function will wait before resolving the promise and continuing execution.
583
+ * @returns {void} The `sleep` function is returning a Promise.
584
+ */
585
+ sleep(timeout) {
586
+ return new Promise(r => setTimeout(r, timeout));
587
+ }
588
+
589
+ toJSON() {
590
+ return super.toJSON({
591
+ readyAt: false,
592
+ });
593
+ }
594
+
595
+ /**
596
+ * The current session id of the shard
597
+ * @type {?string}
598
+ */
599
+ get sessionId() {
600
+ return this.ws.shards.first()?.sessionId;
601
+ }
602
+
603
+ /**
604
+ * Options for {@link Client#acceptInvite}.
605
+ * @typedef {Object} AcceptInviteOptions
606
+ * @property {boolean} [bypassOnboarding=true] Whether to bypass onboarding
607
+ * @property {boolean} [bypassVerify=true] Whether to bypass rule screening
608
+ */
609
+
610
+ /**
611
+ * Join this Guild / GroupDMChannel using this invite
612
+ * @param {InviteResolvable} invite Invite code or URL
613
+ * @param {AcceptInviteOptions} [options] Options
614
+ * @returns {Promise<Guild|DMChannel|GroupDMChannel>}
615
+ * @example
616
+ * await client.acceptInvite('https://discord.gg/genshinimpact', { bypassOnboarding: true, bypassVerify: true })
617
+ */
618
+ async acceptInvite(invite, options = { bypassOnboarding: true, bypassVerify: true }) {
619
+ // ! throw new Error('METHOD_WARNING');
620
+ const code = DataResolver.resolveInviteCode(invite);
621
+ if (!code) throw new Error('INVITE_RESOLVE_CODE');
622
+ const i = await this.fetchInvite(code);
623
+ if (i.guild?.id && this.guilds.cache.has(i.guild?.id)) return this.guilds.cache.get(i.guild?.id);
624
+ if (this.channels.cache.has(i.channelId)) return this.channels.cache.get(i.channelId);
625
+ const data = await this.api.invites(code).post({
626
+ DiscordContext: { location: 'Markdown Link' },
627
+ data: {
628
+ session_id: this.sessionId,
629
+ },
630
+ });
631
+ this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Joined`);
632
+ // Guild
633
+ if (i.guild?.id) {
634
+ const guild = this.guilds.cache.get(i.guild?.id);
635
+ if (i.flags.has('GUEST')) {
636
+ this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Guest invite`);
637
+ return guild;
638
+ }
639
+ if (options.bypassOnboarding) {
640
+ const onboardingData = await this.api.guilds[i.guild?.id].onboarding.get();
641
+ // Onboarding
642
+ if (onboardingData.enabled) {
643
+ const prompts = onboardingData.prompts.filter(o => o.in_onboarding);
644
+ if (prompts.length) {
645
+ const onboarding_prompts_seen = {};
646
+ const onboarding_responses = [];
647
+ const onboarding_responses_seen = {};
648
+
649
+ const currentDate = Date.now();
650
+
651
+ prompts.forEach(prompt => {
652
+ onboarding_prompts_seen[prompt.id] = currentDate;
653
+ if (prompt.required) onboarding_responses.push(prompt.options[0].id);
654
+ prompt.options.forEach(option => {
655
+ onboarding_responses_seen[option.id] = currentDate;
656
+ });
657
+ });
658
+
659
+ await this.api.guilds[i.guild?.id]['onboarding-responses'].post({
660
+ data: {
661
+ onboarding_prompts_seen,
662
+ onboarding_responses,
663
+ onboarding_responses_seen,
664
+ },
665
+ });
666
+ this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Bypassed onboarding`);
667
+ }
668
+ }
669
+ }
670
+ // Read rule
671
+ if (data.show_verification_form && options.bypassVerify) {
672
+ // Check Guild
673
+ if (i.guild.verificationLevel == 'VERY_HIGH' && !this.user.phone) {
674
+ this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Cannot bypass verify (Phone required)`);
675
+ return this.guilds.cache.get(i.guild?.id);
676
+ }
677
+ if (i.guild.verificationLevel !== 'NONE' && !this.user.email) {
678
+ this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Cannot bypass verify (Email required)`);
679
+ return this.guilds.cache.get(i.guild?.id);
680
+ }
681
+ const getForm = await this.api
682
+ .guilds(i.guild?.id)
683
+ ['member-verification'].get({ query: { with_guild: false, invite_code: this.code } })
684
+ .catch(() => {});
685
+ if (getForm && getForm.form_fields[0]) {
686
+ const form = Object.assign(getForm.form_fields[0], { response: true });
687
+ await this.api
688
+ .guilds(i.guild?.id)
689
+ .requests['@me'].put({ data: { form_fields: [form], version: getForm.version } });
690
+ this.emit(Events.DEBUG, `[Invite > Guild ${i.guild?.id}] Bypassed verify`);
691
+ }
692
+ }
693
+ return guild;
694
+ } else {
695
+ return this.channels.cache.has(i.channelId || data.channel?.id);
696
+ }
697
+ }
698
+
699
+ /**
700
+ * Redeem nitro from code or url.
701
+ * @param {string} nitro Nitro url or code
702
+ * @param {TextChannelResolvable} [channel] Channel that the code was sent in
703
+ * @param {Snowflake} [paymentSourceId] Payment source id
704
+ * @returns {Promise<any>}
705
+ */
706
+ redeemNitro(nitro, channel, paymentSourceId) {
707
+ if (typeof nitro !== 'string') throw new Error('INVALID_NITRO');
708
+ const nitroCode =
709
+ nitro.match(/(discord.gift|discord.com|discordapp.com\/gifts)\/(\w{16,25})/) ||
710
+ nitro.match(/(discord\.gift\/|discord\.com\/gifts\/|discordapp\.com\/gifts\/)(\w+)/);
711
+ if (!nitroCode) return false;
712
+ const code = nitroCode[2];
713
+ channel = this.channels.resolveId(channel);
714
+ return this.api.entitlements['gift-codes'](code).redeem.post({
715
+ auth: true,
716
+ data: { channel_id: channel || null, payment_source_id: paymentSourceId || null },
717
+ });
718
+ }
719
+
720
+ /**
721
+ * @typedef {Object} OAuth2AuthorizeOptions
722
+ * @property {string} [guild_id] Guild ID
723
+ * @property {string} [permissions] Permissions
724
+ * @property {boolean} [authorize] Whether to authorize or not
725
+ * @property {string} [code] 2FA Code
726
+ * @property {string} [webhook_channel_id] Webhook Channel ID
727
+ */
728
+
729
+ /**
730
+ * Authorize an application.
731
+ * @param {string} urlOAuth2 Discord Auth URL
732
+ * @param {OAuth2AuthorizeOptions} [options] Oauth2 options
733
+ * @returns {Promise<{ location: string }>}
734
+ * @example
735
+ * client.authorizeURL(`https://discord.com/api/oauth2/authorize?client_id=botID&permissions=8&scope=applications.commands%20bot`, {
736
+ guild_id: "guildID",
737
+ })
738
+ */
739
+ authorizeURL(urlOAuth2, options = {}) {
740
+ // ! throw new Error('METHOD_WARNING');
741
+ const url = new URL(urlOAuth2);
742
+ if (!/^https:\/\/(?:canary\.|ptb\.)?discord\.com(?:\/api(?:\/v\d{1,2})?)?\/oauth2\/authorize\?/.test(urlOAuth2)) {
743
+ throw new Error('INVALID_URL', urlOAuth2);
744
+ }
745
+ const searchParams = Object.fromEntries(url.searchParams);
746
+ // Assign options
747
+ options = {
748
+ authorize: true,
749
+ permissions: '0',
750
+ integration_type: 0,
751
+ location_context: {
752
+ guild_id: '10000',
753
+ channel_id: '10000',
754
+ channel_type: 10000,
755
+ },
756
+ ...searchParams,
757
+ ...options,
758
+ };
759
+ delete searchParams.permissions;
760
+ delete searchParams.integration_type;
761
+ delete searchParams.guild_id;
762
+ return this.api.oauth2.authorize.post({
763
+ query: searchParams,
764
+ data: options,
765
+ });
766
+ }
767
+
768
+ /**
769
+ * Install User Apps
770
+ * @param {Snowflake} applicationId Discord Application id
771
+ * @returns {Promise<void>}
772
+ */
773
+ installUserApps(applicationId) {
774
+ return this.api
775
+ .applications(applicationId)
776
+ .public.get({
777
+ query: {
778
+ with_guild: false,
779
+ },
780
+ })
781
+ .then(rawData => {
782
+ const installTypes = rawData.integration_types_config['1'];
783
+ if (installTypes) {
784
+ return this.api.oauth2.authorize.post({
785
+ query: {
786
+ client_id: applicationId,
787
+ scope: installTypes.oauth2_install_params.scopes.join(' '),
788
+ },
789
+ data: {
790
+ permissions: '0',
791
+ authorize: true,
792
+ integration_type: 1,
793
+ },
794
+ });
795
+ } else {
796
+ return false;
797
+ }
798
+ });
799
+ }
800
+
801
+ /**
802
+ * Deauthorizes an application or token.
803
+ * @param {Snowflake} id - The ID of the Discord Application or Token.
804
+ * @param {'application' | 'token'} [type='application'] - The type of the ID provided. Defaults to 'application'.
805
+ * @returns {Promise<void>} A promise that resolves when the deauthorization is complete.
806
+ */
807
+ deauthorize(id, type = 'application') {
808
+ if (type === 'application') {
809
+ return this.api.oauth2.tokens
810
+ .get()
811
+ .then(data => data.find(o => o.application.id == id))
812
+ .then(o => this.api.oauth2.tokens(o.id).delete());
813
+ } else {
814
+ return this.api.oauth2.tokens(id).delete();
815
+ }
816
+ }
817
+
818
+ /**
819
+ * @typedef {Object} AuthorizedApplicationData
820
+ * @property {Application} application - The application object.
821
+ * @property {Snowflake} authorizedApplicationId - The ID of the OAuth2 token.
822
+ * @property {string[]} scopes - The scopes that were granted to this token.
823
+ * @property {function(): Promise<void>} deauthorize - Function to revoke this token.
824
+ */
825
+
826
+ /**
827
+ * Retrieves the list of authorized applications (OAuth2 tokens).
828
+ * @returns {Promise<Collection<Snowflake, AuthorizedApplicationData>>}
829
+ */
830
+ authorizedApplications() {
831
+ return this.api.oauth2.tokens.get().then(data => {
832
+ const results = new Collection();
833
+ for (const o of data) {
834
+ const application = new Application(this, o.application);
835
+ const data = {
836
+ application,
837
+ authorizedApplicationId: o.id,
838
+ scopes: o.scopes,
839
+ deauthorize: () => this.deauthorize(o.id, 'token'),
840
+ };
841
+ results.set(o.application.id, data);
842
+ }
843
+ return results;
844
+ });
845
+ }
846
+
847
+ /**
848
+ * Calls {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script
849
+ * with the client as `this`.
850
+ * @param {string} script Script to eval
851
+ * @returns {*}
852
+ * @private
853
+ */
854
+ _eval(script) {
855
+ return eval(script);
856
+ }
857
+
858
+ /**
859
+ * Validates the client options.
860
+ * @param {ClientOptions} [options=this.options] Options to validate
861
+ * @private
862
+ */
863
+ _validateOptions(options = this.options) {
864
+ if (typeof options.makeCache !== 'function') {
865
+ throw new TypeError('CLIENT_INVALID_OPTION', 'makeCache', 'a function');
866
+ }
867
+ if (typeof options.messageCacheLifetime !== 'number' || isNaN(options.messageCacheLifetime)) {
868
+ throw new TypeError('CLIENT_INVALID_OPTION', 'The messageCacheLifetime', 'a number');
869
+ }
870
+ if (typeof options.messageSweepInterval !== 'number' || isNaN(options.messageSweepInterval)) {
871
+ throw new TypeError('CLIENT_INVALID_OPTION', 'messageSweepInterval', 'a number');
872
+ }
873
+ if (typeof options.sweepers !== 'object' || options.sweepers === null) {
874
+ throw new TypeError('CLIENT_INVALID_OPTION', 'sweepers', 'an object');
875
+ }
876
+ if (typeof options.invalidRequestWarningInterval !== 'number' || isNaN(options.invalidRequestWarningInterval)) {
877
+ throw new TypeError('CLIENT_INVALID_OPTION', 'invalidRequestWarningInterval', 'a number');
878
+ }
879
+ if (!Array.isArray(options.partials)) {
880
+ throw new TypeError('CLIENT_INVALID_OPTION', 'partials', 'an Array');
881
+ }
882
+ if (typeof options.DMChannelVoiceStatusSync !== 'number' || isNaN(options.DMChannelVoiceStatusSync)) {
883
+ throw new TypeError('CLIENT_INVALID_OPTION', 'DMChannelVoiceStatusSync', 'a number');
884
+ }
885
+ if (typeof options.waitGuildTimeout !== 'number' || isNaN(options.waitGuildTimeout)) {
886
+ throw new TypeError('CLIENT_INVALID_OPTION', 'waitGuildTimeout', 'a number');
887
+ }
888
+ if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
889
+ throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
890
+ }
891
+ if (typeof options.restRequestTimeout !== 'number' || isNaN(options.restRequestTimeout)) {
892
+ throw new TypeError('CLIENT_INVALID_OPTION', 'restRequestTimeout', 'a number');
893
+ }
894
+ if (typeof options.restGlobalRateLimit !== 'number' || isNaN(options.restGlobalRateLimit)) {
895
+ throw new TypeError('CLIENT_INVALID_OPTION', 'restGlobalRateLimit', 'a number');
896
+ }
897
+ if (typeof options.restSweepInterval !== 'number' || isNaN(options.restSweepInterval)) {
898
+ throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number');
899
+ }
900
+ if (typeof options.retryLimit !== 'number' || isNaN(options.retryLimit)) {
901
+ throw new TypeError('CLIENT_INVALID_OPTION', 'retryLimit', 'a number');
902
+ }
903
+ if (typeof options.failIfNotExists !== 'boolean') {
904
+ throw new TypeError('CLIENT_INVALID_OPTION', 'failIfNotExists', 'a boolean');
905
+ }
906
+ if (
907
+ typeof options.rejectOnRateLimit !== 'undefined' &&
908
+ !(typeof options.rejectOnRateLimit === 'function' || Array.isArray(options.rejectOnRateLimit))
909
+ ) {
910
+ throw new TypeError('CLIENT_INVALID_OPTION', 'rejectOnRateLimit', 'an array or a function');
911
+ }
912
+ if (typeof options.TOTPKey === 'string') {
913
+ // Convert to base32 if not already
914
+ options.TOTPKey = options.TOTPKey.replace(/ +/g, '').toUpperCase();
915
+ }
916
+ // Hardcode
917
+ this.options.shardCount = 1;
918
+ this.options.shards = [0];
919
+ this.options.intents = Intents.ALL;
920
+ }
921
+ }
922
+
923
+ module.exports = Client;
924
+
925
+ /**
926
+ * Emitted for general warnings.
927
+ * @event Client#warn
928
+ * @param {string} info The warning
929
+ */
930
+
931
+ /**
932
+ * @external Collection
933
+ * @see {@link https://discord.js.org/docs/packages/collection/stable/Collection:Class}
934
+ */