@biggy1337/discord-sbjs 1.3.11
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.
- package/LICENSE +674 -0
- package/README.md +147 -0
- package/package.json +80 -0
- package/src/WebSocket.js +39 -0
- package/src/client/BaseClient.js +110 -0
- package/src/client/Client.js +1076 -0
- package/src/client/WebhookClient.js +61 -0
- package/src/client/actions/Action.js +133 -0
- package/src/client/actions/ActionsManager.js +80 -0
- package/src/client/actions/ApplicationCommandPermissionsUpdate.js +34 -0
- package/src/client/actions/AutoModerationActionExecution.js +27 -0
- package/src/client/actions/AutoModerationRuleCreate.js +28 -0
- package/src/client/actions/AutoModerationRuleDelete.js +32 -0
- package/src/client/actions/AutoModerationRuleUpdate.js +30 -0
- package/src/client/actions/ChannelCreate.js +23 -0
- package/src/client/actions/ChannelDelete.js +39 -0
- package/src/client/actions/ChannelUpdate.js +43 -0
- package/src/client/actions/GuildAuditLogEntryCreate.js +29 -0
- package/src/client/actions/GuildBanAdd.js +20 -0
- package/src/client/actions/GuildBanRemove.js +25 -0
- package/src/client/actions/GuildChannelsPositionUpdate.js +21 -0
- package/src/client/actions/GuildDelete.js +65 -0
- package/src/client/actions/GuildEmojiCreate.js +20 -0
- package/src/client/actions/GuildEmojiDelete.js +21 -0
- package/src/client/actions/GuildEmojiUpdate.js +20 -0
- package/src/client/actions/GuildEmojisUpdate.js +34 -0
- package/src/client/actions/GuildIntegrationsUpdate.js +19 -0
- package/src/client/actions/GuildMemberRemove.js +33 -0
- package/src/client/actions/GuildMemberUpdate.js +44 -0
- package/src/client/actions/GuildRoleCreate.js +25 -0
- package/src/client/actions/GuildRoleDelete.js +31 -0
- package/src/client/actions/GuildRoleUpdate.js +39 -0
- package/src/client/actions/GuildRolesPositionUpdate.js +21 -0
- package/src/client/actions/GuildScheduledEventCreate.js +27 -0
- package/src/client/actions/GuildScheduledEventDelete.js +31 -0
- package/src/client/actions/GuildScheduledEventUpdate.js +30 -0
- package/src/client/actions/GuildScheduledEventUserAdd.js +32 -0
- package/src/client/actions/GuildScheduledEventUserRemove.js +32 -0
- package/src/client/actions/GuildStickerCreate.js +20 -0
- package/src/client/actions/GuildStickerDelete.js +21 -0
- package/src/client/actions/GuildStickerUpdate.js +20 -0
- package/src/client/actions/GuildStickersUpdate.js +34 -0
- package/src/client/actions/GuildUpdate.js +33 -0
- package/src/client/actions/InviteCreate.js +28 -0
- package/src/client/actions/InviteDelete.js +30 -0
- package/src/client/actions/MessageCreate.js +50 -0
- package/src/client/actions/MessageDelete.js +34 -0
- package/src/client/actions/MessageDeleteBulk.js +43 -0
- package/src/client/actions/MessagePollVoteAdd.js +33 -0
- package/src/client/actions/MessagePollVoteRemove.js +33 -0
- package/src/client/actions/MessageReactionAdd.js +71 -0
- package/src/client/actions/MessageReactionRemove.js +39 -0
- package/src/client/actions/MessageReactionRemoveAll.js +36 -0
- package/src/client/actions/MessageReactionRemoveEmoji.js +30 -0
- package/src/client/actions/MessageUpdate.js +28 -0
- package/src/client/actions/PresenceUpdate.js +55 -0
- package/src/client/actions/StageInstanceCreate.js +28 -0
- package/src/client/actions/StageInstanceDelete.js +33 -0
- package/src/client/actions/StageInstanceUpdate.js +30 -0
- package/src/client/actions/ThreadCreate.js +24 -0
- package/src/client/actions/ThreadDelete.js +32 -0
- package/src/client/actions/ThreadListSync.js +60 -0
- package/src/client/actions/ThreadMemberUpdate.js +30 -0
- package/src/client/actions/ThreadMembersUpdate.js +38 -0
- package/src/client/actions/TypingStart.js +31 -0
- package/src/client/actions/UserUpdate.js +35 -0
- package/src/client/actions/VoiceStateUpdate.js +63 -0
- package/src/client/actions/WebhooksUpdate.js +20 -0
- package/src/client/voice/ClientVoiceManager.js +162 -0
- package/src/client/voice/StreamEventRouter.js +60 -0
- package/src/client/voice/VoiceConnection.js +1173 -0
- package/src/client/voice/dispatcher/AnnexBDispatcher.js +120 -0
- package/src/client/voice/dispatcher/AudioDispatcher.js +145 -0
- package/src/client/voice/dispatcher/BaseDispatcher.js +459 -0
- package/src/client/voice/dispatcher/VPxDispatcher.js +54 -0
- package/src/client/voice/dispatcher/VideoDispatcher.js +68 -0
- package/src/client/voice/networking/VoiceUDPClient.js +187 -0
- package/src/client/voice/networking/VoiceWebSocket.js +347 -0
- package/src/client/voice/player/MediaPlayer.js +321 -0
- package/src/client/voice/player/processing/AnnexBNalSplitter.js +244 -0
- package/src/client/voice/player/processing/IvfSplitter.js +106 -0
- package/src/client/voice/player/processing/PCMInsertSilence.js +37 -0
- package/src/client/voice/receiver/PacketHandler.js +260 -0
- package/src/client/voice/receiver/Receiver.js +96 -0
- package/src/client/voice/receiver/Recorder.js +191 -0
- package/src/client/voice/util/Function.js +116 -0
- package/src/client/voice/util/PlayInterface.js +122 -0
- package/src/client/voice/util/Secretbox.js +64 -0
- package/src/client/voice/util/Silence.js +16 -0
- package/src/client/voice/util/Socket.js +62 -0
- package/src/client/voice/util/VolumeInterface.js +104 -0
- package/src/client/websocket/DispatchTable.js +7 -0
- package/src/client/websocket/GatewaySendScheduler.js +122 -0
- package/src/client/websocket/WebSocketManager.js +439 -0
- package/src/client/websocket/WebSocketShard.js +1085 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_CREATE.js +18 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_DELETE.js +20 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_UPDATE.js +20 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js +5 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js +5 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js +5 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/CALL_CREATE.js +14 -0
- package/src/client/websocket/handlers/CALL_DELETE.js +11 -0
- package/src/client/websocket/handlers/CALL_UPDATE.js +11 -0
- package/src/client/websocket/handlers/CHANNEL_CREATE.js +5 -0
- package/src/client/websocket/handlers/CHANNEL_DELETE.js +5 -0
- package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +22 -0
- package/src/client/websocket/handlers/CHANNEL_RECIPIENT_ADD.js +19 -0
- package/src/client/websocket/handlers/CHANNEL_RECIPIENT_REMOVE.js +16 -0
- package/src/client/websocket/handlers/CHANNEL_UPDATE.js +16 -0
- package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_BAN_ADD.js +5 -0
- package/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_CREATE.js +51 -0
- package/src/client/websocket/handlers/GUILD_DELETE.js +5 -0
- package/src/client/websocket/handlers/GUILD_EMOJIS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +45 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +20 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_ROLE_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_ROLE_DELETE.js +5 -0
- package/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_DELETE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_ADD.js +5 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_STICKERS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_UPDATE.js +5 -0
- package/src/client/websocket/handlers/INTERACTION_MODAL_CREATE.js +12 -0
- package/src/client/websocket/handlers/INVITE_CREATE.js +5 -0
- package/src/client/websocket/handlers/INVITE_DELETE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_CREATE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE_BULK.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_ADD.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_REMOVE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_ADD.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_ALL.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_EMOJI.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_UPDATE.js +16 -0
- package/src/client/websocket/handlers/PRESENCE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/READY.js +156 -0
- package/src/client/websocket/handlers/RELATIONSHIP_ADD.js +19 -0
- package/src/client/websocket/handlers/RELATIONSHIP_REMOVE.js +17 -0
- package/src/client/websocket/handlers/RELATIONSHIP_UPDATE.js +41 -0
- package/src/client/websocket/handlers/RESUMED.js +14 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_CREATE.js +5 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_DELETE.js +5 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_CREATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_DELETE.js +5 -0
- package/src/client/websocket/handlers/THREAD_LIST_SYNC.js +5 -0
- package/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/THREAD_UPDATE.js +16 -0
- package/src/client/websocket/handlers/TYPING_START.js +5 -0
- package/src/client/websocket/handlers/USER_GUILD_SETTINGS_UPDATE.js +6 -0
- package/src/client/websocket/handlers/USER_NOTE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/USER_REQUIRED_ACTION_UPDATE.js +82 -0
- package/src/client/websocket/handlers/USER_SETTINGS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/USER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/VOICE_CHANNEL_EFFECT_SEND.js +16 -0
- package/src/client/websocket/handlers/VOICE_CHANNEL_STATUS_UPDATE.js +12 -0
- package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js +12 -0
- package/src/client/websocket/handlers/VOICE_STATE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/WEBHOOKS_UPDATE.js +5 -0
- package/src/client/websocket/handlers/index.js +84 -0
- package/src/errors/DJSError.js +61 -0
- package/src/errors/Messages.js +218 -0
- package/src/errors/index.js +4 -0
- package/src/index.js +172 -0
- package/src/managers/ApplicationCommandManager.js +264 -0
- package/src/managers/ApplicationCommandPermissionsManager.js +417 -0
- package/src/managers/AutoModerationRuleManager.js +296 -0
- package/src/managers/BaseGuildEmojiManager.js +80 -0
- package/src/managers/BaseManager.js +19 -0
- package/src/managers/BillingManager.js +66 -0
- package/src/managers/CachedManager.js +72 -0
- package/src/managers/ChannelManager.js +148 -0
- package/src/managers/ClientUserSettingManager.js +372 -0
- package/src/managers/DataManager.js +61 -0
- package/src/managers/DeveloperManager.js +265 -0
- package/src/managers/GuildBanManager.js +252 -0
- package/src/managers/GuildChannelManager.js +488 -0
- package/src/managers/GuildEmojiManager.js +171 -0
- package/src/managers/GuildEmojiRoleManager.js +118 -0
- package/src/managers/GuildForumThreadManager.js +108 -0
- package/src/managers/GuildInviteManager.js +213 -0
- package/src/managers/GuildManager.js +401 -0
- package/src/managers/GuildMemberManager.js +608 -0
- package/src/managers/GuildMemberRoleManager.js +195 -0
- package/src/managers/GuildScheduledEventManager.js +314 -0
- package/src/managers/GuildSettingManager.js +155 -0
- package/src/managers/GuildStickerManager.js +179 -0
- package/src/managers/GuildTextThreadManager.js +98 -0
- package/src/managers/InteractionManager.js +39 -0
- package/src/managers/MessageManager.js +457 -0
- package/src/managers/PermissionOverwriteManager.js +169 -0
- package/src/managers/PresenceManager.js +71 -0
- package/src/managers/QuestManager.js +391 -0
- package/src/managers/ReactionManager.js +67 -0
- package/src/managers/ReactionUserManager.js +73 -0
- package/src/managers/RelationshipManager.js +341 -0
- package/src/managers/RoleManager.js +448 -0
- package/src/managers/SessionManager.js +66 -0
- package/src/managers/StageInstanceManager.js +162 -0
- package/src/managers/ThreadManager.js +175 -0
- package/src/managers/ThreadMemberManager.js +186 -0
- package/src/managers/UserManager.js +274 -0
- package/src/managers/UserNoteManager.js +53 -0
- package/src/managers/VoiceStateManager.js +59 -0
- package/src/rest/APIRequest.js +198 -0
- package/src/rest/APIRouter.js +94 -0
- package/src/rest/DiscordAPIError.js +120 -0
- package/src/rest/HTTPError.js +62 -0
- package/src/rest/RESTManager.js +203 -0
- package/src/rest/RateLimitCoordinator.js +156 -0
- package/src/rest/RateLimitError.js +55 -0
- package/src/rest/RequestHandler.js +552 -0
- package/src/sharding/Shard.js +444 -0
- package/src/sharding/ShardClientUtil.js +279 -0
- package/src/sharding/ShardingManager.js +319 -0
- package/src/structures/AnonymousGuild.js +98 -0
- package/src/structures/ApplicationCommand.js +593 -0
- package/src/structures/ApplicationRoleConnectionMetadata.js +48 -0
- package/src/structures/AutoModerationActionExecution.js +89 -0
- package/src/structures/AutoModerationRule.js +294 -0
- package/src/structures/AutocompleteInteraction.js +107 -0
- package/src/structures/Base.js +43 -0
- package/src/structures/BaseCommandInteraction.js +211 -0
- package/src/structures/BaseGuild.js +124 -0
- package/src/structures/BaseGuildEmoji.js +56 -0
- package/src/structures/BaseGuildTextChannel.js +191 -0
- package/src/structures/BaseGuildVoiceChannel.js +241 -0
- package/src/structures/BaseMessageComponent.js +181 -0
- package/src/structures/ButtonInteraction.js +11 -0
- package/src/structures/CallState.js +63 -0
- package/src/structures/CategoryChannel.js +85 -0
- package/src/structures/Channel.js +284 -0
- package/src/structures/ClientPresence.js +77 -0
- package/src/structures/ClientUser.js +706 -0
- package/src/structures/CommandInteraction.js +41 -0
- package/src/structures/CommandInteractionOptionResolver.js +276 -0
- package/src/structures/ContainerComponent.js +68 -0
- package/src/structures/ContextMenuInteraction.js +65 -0
- package/src/structures/DMChannel.js +222 -0
- package/src/structures/DirectoryChannel.js +20 -0
- package/src/structures/Emoji.js +148 -0
- package/src/structures/FileComponent.js +49 -0
- package/src/structures/ForumChannel.js +31 -0
- package/src/structures/GroupDMChannel.js +415 -0
- package/src/structures/Guild.js +1804 -0
- package/src/structures/GuildAuditLogs.js +746 -0
- package/src/structures/GuildBan.js +59 -0
- package/src/structures/GuildBoost.js +108 -0
- package/src/structures/GuildChannel.js +470 -0
- package/src/structures/GuildEmoji.js +161 -0
- package/src/structures/GuildMember.js +636 -0
- package/src/structures/GuildPreview.js +191 -0
- package/src/structures/GuildPreviewEmoji.js +27 -0
- package/src/structures/GuildScheduledEvent.js +536 -0
- package/src/structures/GuildTemplate.js +236 -0
- package/src/structures/Integration.js +188 -0
- package/src/structures/IntegrationApplication.js +96 -0
- package/src/structures/Interaction.js +290 -0
- package/src/structures/InteractionCollector.js +248 -0
- package/src/structures/InteractionWebhook.js +43 -0
- package/src/structures/Invite.js +358 -0
- package/src/structures/InviteGuild.js +23 -0
- package/src/structures/InviteStageInstance.js +86 -0
- package/src/structures/MediaChannel.js +11 -0
- package/src/structures/MediaGalleryComponent.js +41 -0
- package/src/structures/MediaGalleryItem.js +47 -0
- package/src/structures/Message.js +1272 -0
- package/src/structures/MessageActionRow.js +105 -0
- package/src/structures/MessageAttachment.js +216 -0
- package/src/structures/MessageButton.js +166 -0
- package/src/structures/MessageCollector.js +146 -0
- package/src/structures/MessageComponentInteraction.js +120 -0
- package/src/structures/MessageContextMenuInteraction.js +20 -0
- package/src/structures/MessageEmbed.js +596 -0
- package/src/structures/MessageMentions.js +273 -0
- package/src/structures/MessagePayload.js +354 -0
- package/src/structures/MessageReaction.js +181 -0
- package/src/structures/MessageSelectMenu.js +141 -0
- package/src/structures/Modal.js +161 -0
- package/src/structures/ModalSubmitFieldsResolver.js +53 -0
- package/src/structures/ModalSubmitInteraction.js +119 -0
- package/src/structures/NewsChannel.js +32 -0
- package/src/structures/OAuth2Guild.js +28 -0
- package/src/structures/PermissionOverwrites.js +198 -0
- package/src/structures/Poll.js +108 -0
- package/src/structures/PollAnswer.js +88 -0
- package/src/structures/Presence.js +1165 -0
- package/src/structures/ReactionCollector.js +229 -0
- package/src/structures/ReactionEmoji.js +31 -0
- package/src/structures/Role.js +590 -0
- package/src/structures/SectionComponent.js +48 -0
- package/src/structures/SelectMenuInteraction.js +21 -0
- package/src/structures/SeparatorComponent.js +48 -0
- package/src/structures/Session.js +81 -0
- package/src/structures/StageChannel.js +104 -0
- package/src/structures/StageInstance.js +208 -0
- package/src/structures/Sticker.js +310 -0
- package/src/structures/StickerPack.js +95 -0
- package/src/structures/StoreChannel.js +56 -0
- package/src/structures/Team.js +118 -0
- package/src/structures/TeamMember.js +80 -0
- package/src/structures/TextChannel.js +33 -0
- package/src/structures/TextDisplayComponent.js +40 -0
- package/src/structures/TextInputComponent.js +132 -0
- package/src/structures/ThreadChannel.js +605 -0
- package/src/structures/ThreadMember.js +105 -0
- package/src/structures/ThreadOnlyChannel.js +249 -0
- package/src/structures/ThumbnailComponent.js +57 -0
- package/src/structures/Typing.js +74 -0
- package/src/structures/UnfurledMediaItem.js +29 -0
- package/src/structures/User.js +992 -0
- package/src/structures/UserContextMenuInteraction.js +29 -0
- package/src/structures/VoiceChannel.js +110 -0
- package/src/structures/VoiceChannelEffect.js +69 -0
- package/src/structures/VoiceRegion.js +53 -0
- package/src/structures/VoiceState.js +354 -0
- package/src/structures/WebEmbed.js +373 -0
- package/src/structures/Webhook.js +478 -0
- package/src/structures/WelcomeChannel.js +60 -0
- package/src/structures/WelcomeScreen.js +48 -0
- package/src/structures/Widget.js +87 -0
- package/src/structures/WidgetMember.js +100 -0
- package/src/structures/interfaces/Application.js +953 -0
- package/src/structures/interfaces/Collector.js +301 -0
- package/src/structures/interfaces/InteractionResponses.js +313 -0
- package/src/structures/interfaces/TextBasedChannel.js +821 -0
- package/src/util/APITypes.js +59 -0
- package/src/util/ActivityFlags.js +44 -0
- package/src/util/ApplicationFlags.js +76 -0
- package/src/util/AttachmentFlags.js +38 -0
- package/src/util/BitField.js +170 -0
- package/src/util/ChannelFlags.js +45 -0
- package/src/util/Constants.js +1953 -0
- package/src/util/DataResolver.js +162 -0
- package/src/util/FastQueue.js +93 -0
- package/src/util/FetchUtil.js +77 -0
- package/src/util/Formatters.js +228 -0
- package/src/util/GuildMemberFlags.js +43 -0
- package/src/util/Intents.js +74 -0
- package/src/util/InviteFlags.js +34 -0
- package/src/util/LimitedCollection.js +131 -0
- package/src/util/ListenerUtil.js +12 -0
- package/src/util/MessageFlags.js +63 -0
- package/src/util/Options.js +431 -0
- package/src/util/Permissions.js +202 -0
- package/src/util/PremiumUsageFlags.js +31 -0
- package/src/util/PurchasedFlags.js +33 -0
- package/src/util/RemoteAuth.js +425 -0
- package/src/util/RoleFlags.js +37 -0
- package/src/util/SnowflakeUtil.js +92 -0
- package/src/util/Speaking.js +33 -0
- package/src/util/Sweepers.js +493 -0
- package/src/util/SystemChannelFlags.js +55 -0
- package/src/util/ThreadMemberFlags.js +30 -0
- package/src/util/UserFlags.js +104 -0
- package/src/util/Util.js +1144 -0
- package/typings/enums.d.ts +437 -0
- package/typings/index.d.ts +8857 -0
- package/typings/index.test-d.ts +0 -0
- package/typings/rawDataTypes.d.ts +403 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { setTimeout } = require('node:timers');
|
|
4
|
+
const { setTimeout: sleep } = require('node:timers/promises');
|
|
5
|
+
const { AsyncQueue } = require('@sapphire/async-queue');
|
|
6
|
+
const DiscordAPIError = require('./DiscordAPIError');
|
|
7
|
+
const HTTPError = require('./HTTPError');
|
|
8
|
+
const RateLimitError = require('./RateLimitError');
|
|
9
|
+
const {
|
|
10
|
+
Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST },
|
|
11
|
+
} = require('../util/Constants');
|
|
12
|
+
const { hasListener } = require('../util/ListenerUtil');
|
|
13
|
+
|
|
14
|
+
function parseResponse(res) {
|
|
15
|
+
if (res.headers.get('content-type')?.startsWith('application/json')) return res.json();
|
|
16
|
+
return res.arrayBuffer();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function getAPIOffset(serverDate) {
|
|
20
|
+
if (!serverDate) return 0;
|
|
21
|
+
const parsed = new Date(serverDate).getTime();
|
|
22
|
+
if (!Number.isFinite(parsed)) return 0;
|
|
23
|
+
return parsed - Date.now();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function calculateReset(reset, resetAfter, serverDate) {
|
|
27
|
+
if (resetAfter) return Date.now() + Number(resetAfter) * 1_000;
|
|
28
|
+
const parsedReset = new Date(Number(reset) * 1_000).getTime();
|
|
29
|
+
if (!Number.isFinite(parsedReset)) return Date.now();
|
|
30
|
+
return parsedReset - getAPIOffset(serverDate);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* Invalid request limiting is done on a per-IP basis, not a per-token basis.
|
|
34
|
+
* The best we can do is track invalid counts process-wide (on the theory that
|
|
35
|
+
* users could have multiple bots run from one process) rather than per-bot.
|
|
36
|
+
* Therefore, store these at file scope here rather than in the client's
|
|
37
|
+
* RESTManager object.
|
|
38
|
+
*/
|
|
39
|
+
let invalidCount = 0;
|
|
40
|
+
let invalidCountResetTime = null;
|
|
41
|
+
|
|
42
|
+
class RequestHandler {
|
|
43
|
+
constructor(manager) {
|
|
44
|
+
this.manager = manager;
|
|
45
|
+
this.queue = new AsyncQueue();
|
|
46
|
+
this.reset = -1;
|
|
47
|
+
this.remaining = -1;
|
|
48
|
+
this.limit = -1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async push(request) {
|
|
52
|
+
await this.queue.wait();
|
|
53
|
+
try {
|
|
54
|
+
return await this.execute(request);
|
|
55
|
+
} finally {
|
|
56
|
+
this.queue.shift();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get globalLimited() {
|
|
61
|
+
return this.manager.globalRemaining <= 0 && Date.now() < this.manager.globalReset;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get localLimited() {
|
|
65
|
+
return this.remaining <= 0 && Date.now() < this.reset;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get limited() {
|
|
69
|
+
return this._getActiveRateLimit() !== null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get _inactive() {
|
|
73
|
+
return this.queue.remaining === 0 && !this.limited;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_getActiveRateLimit(now = Date.now(), request) {
|
|
77
|
+
if (this.manager.coordinator?.getActiveRateLimit) {
|
|
78
|
+
return this.manager.coordinator.getActiveRateLimit(this, now, request);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!request?.options?.webhook && this.manager.globalRemaining <= 0 && now < this.manager.globalReset) {
|
|
82
|
+
const timeout = this.manager.globalReset + this.manager.client.options.restTimeOffset - now;
|
|
83
|
+
return { isGlobal: true, limit: this.manager.globalLimit, timeout };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (this.remaining <= 0 && now < this.reset) {
|
|
87
|
+
const timeout = this.reset + this.manager.client.options.restTimeOffset - now;
|
|
88
|
+
return { isGlobal: false, limit: this.limit, timeout };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_getDelayPromise(isGlobal, timeout) {
|
|
95
|
+
const delay = Math.max(timeout, 0);
|
|
96
|
+
if (isGlobal) {
|
|
97
|
+
if (!this.manager.globalDelay) {
|
|
98
|
+
this.manager.globalDelay = this.globalDelayFor(delay);
|
|
99
|
+
}
|
|
100
|
+
return this.manager.globalDelay;
|
|
101
|
+
}
|
|
102
|
+
return sleep(delay);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
globalDelayFor(ms) {
|
|
106
|
+
return new Promise(resolve => {
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
this.manager.globalDelay = null;
|
|
109
|
+
resolve();
|
|
110
|
+
}, ms).unref();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* Determines whether the request should be queued or whether a RateLimitError should be thrown
|
|
116
|
+
*/
|
|
117
|
+
async onRateLimit(request, limit, timeout, isGlobal) {
|
|
118
|
+
const { options } = this.manager.client;
|
|
119
|
+
if (!options.rejectOnRateLimit) return;
|
|
120
|
+
|
|
121
|
+
const rateLimitData = {
|
|
122
|
+
timeout,
|
|
123
|
+
limit,
|
|
124
|
+
method: request.method,
|
|
125
|
+
path: request.path,
|
|
126
|
+
route: request.route,
|
|
127
|
+
global: isGlobal,
|
|
128
|
+
};
|
|
129
|
+
const shouldThrow =
|
|
130
|
+
typeof options.rejectOnRateLimit === 'function'
|
|
131
|
+
? await options.rejectOnRateLimit(rateLimitData)
|
|
132
|
+
: options.rejectOnRateLimit.some(route => rateLimitData.route.startsWith(route.toLowerCase()));
|
|
133
|
+
if (shouldThrow) {
|
|
134
|
+
throw new RateLimitError(rateLimitData);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async execute(request, captchaKey, captchaRqToken, captchaSessionId) {
|
|
139
|
+
if (request.options.delay) {
|
|
140
|
+
const initialDelay = Math.floor(Math.random() * request.options.delay) + 50;
|
|
141
|
+
await sleep(initialDelay);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const run = async (activeCaptchaKey, activeCaptchaRqToken, activeCaptchaSessionId) => {
|
|
145
|
+
const hasDebugListener = hasListener(this.manager.client, DEBUG);
|
|
146
|
+
const hasRateLimitListener = hasListener(this.manager.client, RATE_LIMIT);
|
|
147
|
+
const hasApiRequestListener = hasListener(this.manager.client, API_REQUEST);
|
|
148
|
+
const hasApiResponseListener = hasListener(this.manager.client, API_RESPONSE);
|
|
149
|
+
const invalidRequestInterval = this.manager.client.options.invalidRequestWarningInterval;
|
|
150
|
+
const hasInvalidRequestListener =
|
|
151
|
+
hasListener(this.manager.client, INVALID_REQUEST_WARNING) && invalidRequestInterval > 0;
|
|
152
|
+
|
|
153
|
+
/*
|
|
154
|
+
* After calculations have been done, pre-emptively stop further requests
|
|
155
|
+
* Potentially loop until this task can run if e.g. the global rate limit is hit twice
|
|
156
|
+
*/
|
|
157
|
+
for (
|
|
158
|
+
let rateLimitState = this._getActiveRateLimit(Date.now(), request);
|
|
159
|
+
rateLimitState;
|
|
160
|
+
rateLimitState = this._getActiveRateLimit(Date.now(), request)
|
|
161
|
+
) {
|
|
162
|
+
const { isGlobal, limit, timeout } = rateLimitState;
|
|
163
|
+
const safeTimeout = Math.max(timeout, 0);
|
|
164
|
+
|
|
165
|
+
if (hasRateLimitListener) {
|
|
166
|
+
/**
|
|
167
|
+
* Emitted when the client hits a rate limit while making a request
|
|
168
|
+
* @event BaseClient#rateLimit
|
|
169
|
+
* @param {RateLimitData} rateLimitData Object containing the rate limit info
|
|
170
|
+
*/
|
|
171
|
+
this.manager.client.emit(RATE_LIMIT, {
|
|
172
|
+
timeout: safeTimeout,
|
|
173
|
+
limit,
|
|
174
|
+
method: request.method,
|
|
175
|
+
path: request.path,
|
|
176
|
+
route: request.route,
|
|
177
|
+
global: isGlobal,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const delayPromise = this._getDelayPromise(isGlobal, safeTimeout);
|
|
182
|
+
|
|
183
|
+
// Determine whether a RateLimitError should be thrown
|
|
184
|
+
await this.onRateLimit(request, limit, safeTimeout, isGlobal); // eslint-disable-line no-await-in-loop
|
|
185
|
+
|
|
186
|
+
// Wait for the timeout to expire in order to avoid an actual 429
|
|
187
|
+
await delayPromise; // eslint-disable-line no-await-in-loop
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// As the request goes out, update the global usage information
|
|
191
|
+
if (this.manager.coordinator?.markRequestStart) {
|
|
192
|
+
this.manager.coordinator.markRequestStart(request, Date.now());
|
|
193
|
+
} else {
|
|
194
|
+
const now = Date.now();
|
|
195
|
+
if (!request.options?.webhook && (!this.manager.globalReset || this.manager.globalReset < now)) {
|
|
196
|
+
this.manager.globalReset = now + 1_000;
|
|
197
|
+
this.manager.globalRemaining = this.manager.globalLimit;
|
|
198
|
+
}
|
|
199
|
+
if (!request.options?.webhook) this.manager.globalRemaining--;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Represents a request that will or has been made to the Discord API
|
|
204
|
+
* @typedef {Object} APIRequest
|
|
205
|
+
* @property {HTTPMethod} method The HTTP method used in this request
|
|
206
|
+
* @property {string} path The full path used to make the request
|
|
207
|
+
* @property {string} route The API route identifying the rate limit for this request
|
|
208
|
+
* @property {Object} options Additional options for this request
|
|
209
|
+
* @property {number} retries The number of times this request has been attempted
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
if (hasApiRequestListener) {
|
|
213
|
+
/**
|
|
214
|
+
* Emitted before every API request.
|
|
215
|
+
* This event can emit several times for the same request, e.g. when hitting a rate limit.
|
|
216
|
+
* <info>This is an informational event that is emitted quite frequently,
|
|
217
|
+
* it is highly recommended to check `request.path` to filter the data.</info>
|
|
218
|
+
* @event BaseClient#apiRequest
|
|
219
|
+
* @param {APIRequest} request The request that is about to be sent
|
|
220
|
+
*/
|
|
221
|
+
this.manager.client.emit(API_REQUEST, {
|
|
222
|
+
method: request.method,
|
|
223
|
+
path: request.path,
|
|
224
|
+
route: request.route,
|
|
225
|
+
options: request.options,
|
|
226
|
+
retries: request.retries,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Perform the request
|
|
231
|
+
let res;
|
|
232
|
+
try {
|
|
233
|
+
res = await request.make(activeCaptchaKey, activeCaptchaRqToken, activeCaptchaSessionId);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
// Retry the specified number of times for request abortions
|
|
236
|
+
if (request.retries === this.manager.client.options.retryLimit) {
|
|
237
|
+
throw new HTTPError(error.message, error.constructor.name, error.status, request);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
request.retries++;
|
|
241
|
+
return run();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (hasApiResponseListener) {
|
|
245
|
+
/**
|
|
246
|
+
* Emitted after every API request has received a response.
|
|
247
|
+
* This event does not necessarily correlate to completion of the request, e.g. when hitting a rate limit.
|
|
248
|
+
* <info>This is an informational event that is emitted quite frequently,
|
|
249
|
+
* it is highly recommended to check `request.path` to filter the data.</info>
|
|
250
|
+
* @event BaseClient#apiResponse
|
|
251
|
+
* @param {APIRequest} request The request that triggered this response
|
|
252
|
+
* @param {Response} response The response received from the Discord API
|
|
253
|
+
*/
|
|
254
|
+
this.manager.client.emit(
|
|
255
|
+
API_RESPONSE,
|
|
256
|
+
{
|
|
257
|
+
method: request.method,
|
|
258
|
+
path: request.path,
|
|
259
|
+
route: request.route,
|
|
260
|
+
options: request.options,
|
|
261
|
+
retries: request.retries,
|
|
262
|
+
},
|
|
263
|
+
res.clone(),
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
let sublimitTimeout;
|
|
268
|
+
if (res.headers) {
|
|
269
|
+
const applied = this.manager.coordinator?.applyHeaders?.(this, request, res.headers);
|
|
270
|
+
if (applied) {
|
|
271
|
+
sublimitTimeout = applied.sublimitTimeout;
|
|
272
|
+
} else {
|
|
273
|
+
const serverDate = res.headers.get('date');
|
|
274
|
+
const bucketHash = res.headers.get('x-ratelimit-bucket');
|
|
275
|
+
const limit = res.headers.get('x-ratelimit-limit');
|
|
276
|
+
const remaining = res.headers.get('x-ratelimit-remaining');
|
|
277
|
+
const reset = res.headers.get('x-ratelimit-reset');
|
|
278
|
+
const resetAfter = res.headers.get('x-ratelimit-reset-after');
|
|
279
|
+
|
|
280
|
+
if (bucketHash) this.manager.bindBucket?.(request.method, request.route, bucketHash, this);
|
|
281
|
+
|
|
282
|
+
this.limit = limit ? Number(limit) : Infinity;
|
|
283
|
+
this.remaining = remaining ? Number(remaining) : 1;
|
|
284
|
+
this.reset = reset || resetAfter ? calculateReset(reset, resetAfter, serverDate) : Date.now();
|
|
285
|
+
|
|
286
|
+
if (!resetAfter && serverDate && request.route.includes('reactions')) {
|
|
287
|
+
this.reset = new Date(serverDate).getTime() - getAPIOffset(serverDate) + 250;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
let retryAfter = res.headers.get('retry-after');
|
|
291
|
+
retryAfter = retryAfter ? Number(retryAfter) * 1_000 : -1;
|
|
292
|
+
if (retryAfter > 0) {
|
|
293
|
+
const isGlobalScope = this.manager.coordinator?.isGlobalScope
|
|
294
|
+
? this.manager.coordinator.isGlobalScope(res.headers)
|
|
295
|
+
: res.headers.get('x-ratelimit-global') || res.headers.get('x-ratelimit-scope') === 'global';
|
|
296
|
+
if (isGlobalScope) {
|
|
297
|
+
this.manager.globalRemaining = 0;
|
|
298
|
+
this.manager.globalReset = Date.now() + retryAfter;
|
|
299
|
+
} else if (!this.localLimited) {
|
|
300
|
+
sublimitTimeout = retryAfter;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Count the invalid requests
|
|
307
|
+
if (res.status === 401 || res.status === 403 || res.status === 429) {
|
|
308
|
+
const isShared429 =
|
|
309
|
+
res.status === 429 &&
|
|
310
|
+
(this.manager.coordinator?.isSharedScope
|
|
311
|
+
? this.manager.coordinator.isSharedScope(res.headers)
|
|
312
|
+
: res.headers?.get('x-ratelimit-scope') === 'shared');
|
|
313
|
+
if (!isShared429) {
|
|
314
|
+
const invalidNow = Date.now();
|
|
315
|
+
if (!invalidCountResetTime || invalidCountResetTime < invalidNow) {
|
|
316
|
+
invalidCountResetTime = invalidNow + 1_000 * 60 * 10;
|
|
317
|
+
invalidCount = 0;
|
|
318
|
+
}
|
|
319
|
+
invalidCount++;
|
|
320
|
+
|
|
321
|
+
let breakerDelay = 0;
|
|
322
|
+
if (invalidCount >= 9_000) breakerDelay = 5_000;
|
|
323
|
+
else if (invalidCount >= 5_000) breakerDelay = 1_500;
|
|
324
|
+
else if (invalidCount >= 2_500) breakerDelay = 500;
|
|
325
|
+
|
|
326
|
+
if (breakerDelay > 0) {
|
|
327
|
+
await sleep(breakerDelay);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const emitInvalid = hasInvalidRequestListener && invalidCount % invalidRequestInterval === 0;
|
|
331
|
+
if (emitInvalid) {
|
|
332
|
+
/**
|
|
333
|
+
* @typedef {Object} InvalidRequestWarningData
|
|
334
|
+
* @property {number} count Number of invalid requests that have been made in the window
|
|
335
|
+
* @property {number} remainingTime Time in milliseconds remaining before the count resets
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Emitted periodically when the process sends invalid requests to let users avoid the
|
|
340
|
+
* 10k invalid requests in 10 minutes threshold that causes a ban
|
|
341
|
+
* @event BaseClient#invalidRequestWarning
|
|
342
|
+
* @param {InvalidRequestWarningData} invalidRequestWarningData Object containing the invalid request info
|
|
343
|
+
*/
|
|
344
|
+
this.manager.client.emit(INVALID_REQUEST_WARNING, {
|
|
345
|
+
count: invalidCount,
|
|
346
|
+
remainingTime: invalidCountResetTime - invalidNow,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Handle 2xx and 3xx responses
|
|
353
|
+
if (res.ok) {
|
|
354
|
+
// Nothing wrong with the request, proceed with the next one
|
|
355
|
+
return parseResponse(res);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Handle 4xx responses
|
|
359
|
+
if (res.status >= 400 && res.status < 500) {
|
|
360
|
+
// Handle ratelimited requests
|
|
361
|
+
if (res.status === 429) {
|
|
362
|
+
const rateLimitNow = Date.now();
|
|
363
|
+
const rateLimitState = this._getActiveRateLimit(rateLimitNow, request);
|
|
364
|
+
let isGlobal = rateLimitState?.isGlobal ?? (!request.options?.webhook && this.globalLimited);
|
|
365
|
+
const limit = rateLimitState?.limit ?? (isGlobal ? this.manager.globalLimit : this.limit);
|
|
366
|
+
const computedTimeout =
|
|
367
|
+
rateLimitState?.timeout ??
|
|
368
|
+
(isGlobal
|
|
369
|
+
? this.manager.globalReset + this.manager.client.options.restTimeOffset - rateLimitNow
|
|
370
|
+
: this.reset + this.manager.client.options.restTimeOffset - rateLimitNow);
|
|
371
|
+
let safeTimeout = Math.max(computedTimeout, 0);
|
|
372
|
+
let bodyRetryAfter = null;
|
|
373
|
+
const headerGlobal =
|
|
374
|
+
this.manager.coordinator?.isGlobalScope?.(res.headers) ??
|
|
375
|
+
Boolean(res.headers.get('x-ratelimit-global') || res.headers.get('x-ratelimit-scope') === 'global');
|
|
376
|
+
if (headerGlobal) isGlobal = true;
|
|
377
|
+
if (!sublimitTimeout) {
|
|
378
|
+
const resolved = this.manager.coordinator?.resolve429Timeout
|
|
379
|
+
? await this.manager.coordinator.resolve429Timeout(res, safeTimeout)
|
|
380
|
+
: { safeTimeout, bodyRetryAfter, bodyGlobal: false, scope: null };
|
|
381
|
+
safeTimeout = resolved.safeTimeout;
|
|
382
|
+
bodyRetryAfter = resolved.bodyRetryAfter;
|
|
383
|
+
if (resolved.bodyGlobal && safeTimeout > 0) {
|
|
384
|
+
isGlobal = true;
|
|
385
|
+
this.manager.globalRemaining = 0;
|
|
386
|
+
this.manager.globalReset = Date.now() + safeTimeout;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (isGlobal && safeTimeout > 0) {
|
|
391
|
+
this.manager.globalRemaining = 0;
|
|
392
|
+
this.manager.globalReset = Date.now() + safeTimeout;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (hasDebugListener) {
|
|
396
|
+
this.manager.client.emit(
|
|
397
|
+
DEBUG,
|
|
398
|
+
`[Request Handler] Hit a 429 while executing a request.
|
|
399
|
+
Global : ${isGlobal}
|
|
400
|
+
Method : ${request.method}
|
|
401
|
+
Path : ${request.path}
|
|
402
|
+
Route : ${request.route}
|
|
403
|
+
Limit : ${limit}
|
|
404
|
+
Timeout : ${safeTimeout}ms
|
|
405
|
+
Sublimit: ${sublimitTimeout ? `${sublimitTimeout}ms` : 'None'}
|
|
406
|
+
Fallback: ${bodyRetryAfter ? `${bodyRetryAfter}ms` : 'None'}`,
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
await this.onRateLimit(request, limit, safeTimeout, isGlobal);
|
|
411
|
+
|
|
412
|
+
// If caused by a sublimit, wait it out here so other requests on the route can be handled
|
|
413
|
+
if (sublimitTimeout) {
|
|
414
|
+
await sleep(sublimitTimeout);
|
|
415
|
+
} else if (bodyRetryAfter && bodyRetryAfter > 0) {
|
|
416
|
+
await sleep(bodyRetryAfter);
|
|
417
|
+
} else if (this.manager.coordinator?.sleepBackoff) {
|
|
418
|
+
await this.manager.coordinator.sleepBackoff(429, Math.max(request.retries, 1));
|
|
419
|
+
} else {
|
|
420
|
+
const fallback = Math.min(1_500, 125 * 2 ** Math.min(Math.max(request.retries, 1), 5));
|
|
421
|
+
const jitter = Math.floor(Math.random() * 201);
|
|
422
|
+
await sleep(fallback + jitter);
|
|
423
|
+
}
|
|
424
|
+
request.retries++;
|
|
425
|
+
return run();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Handle possible malformed requests
|
|
429
|
+
let data;
|
|
430
|
+
try {
|
|
431
|
+
data = await parseResponse(res);
|
|
432
|
+
// Captcha
|
|
433
|
+
if (
|
|
434
|
+
res.status === 400 &&
|
|
435
|
+
data?.captcha_key &&
|
|
436
|
+
typeof this.manager.client.options.captchaSolver === 'function' &&
|
|
437
|
+
request.retries < this.manager.client.options.captchaRetryLimit
|
|
438
|
+
) {
|
|
439
|
+
// Retry the request after a captcha is solved
|
|
440
|
+
if (hasDebugListener) {
|
|
441
|
+
this.manager.client.emit(
|
|
442
|
+
DEBUG,
|
|
443
|
+
`[Request Handler] Hit a captcha while executing a request (${data.captcha_key.join(', ')})
|
|
444
|
+
Method : ${request.method}
|
|
445
|
+
Path : ${request.path}
|
|
446
|
+
Route : ${request.route}
|
|
447
|
+
Sitekey : ${data.captcha_sitekey}
|
|
448
|
+
rqToken : ${data.captcha_rqtoken}
|
|
449
|
+
session : ${data.captcha_session_id ?? 'None'}`,
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
const captcha = await this.manager.client.options.captchaSolver(data, request.fullUserAgent);
|
|
453
|
+
if (hasDebugListener) {
|
|
454
|
+
this.manager.client.emit(
|
|
455
|
+
DEBUG,
|
|
456
|
+
`[Request Handler] Captcha details:
|
|
457
|
+
Method : ${request.method}
|
|
458
|
+
Path : ${request.path}
|
|
459
|
+
Route : ${request.route}
|
|
460
|
+
Key : ${captcha ? `${captcha.slice(0, 120)}...` : '[Captcha not solved]'}
|
|
461
|
+
rqToken : ${data.captcha_rqtoken}
|
|
462
|
+
session : ${data.captcha_session_id ?? 'None'}`,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
request.retries++;
|
|
466
|
+
return run(captcha, data.captcha_rqtoken, data.captcha_session_id);
|
|
467
|
+
}
|
|
468
|
+
// Two factor handling
|
|
469
|
+
if (data?.code && data.code == 60003 && request.options.auth !== false && request.retries < 1) {
|
|
470
|
+
// https://gist.github.com/Dziurwa14/de2498e5ee28d2089f095aa037957cbb
|
|
471
|
+
// 60003: Two factor is required for this operation
|
|
472
|
+
/**
|
|
473
|
+
* {
|
|
474
|
+
* message: "Two factor is required for this operation";
|
|
475
|
+
* code: 60003;
|
|
476
|
+
* mfa: {
|
|
477
|
+
* ticket: string;
|
|
478
|
+
* methods: {
|
|
479
|
+
* type: "password" | "totp" | "sms" | "backup" | "webauthn";
|
|
480
|
+
* backup_codes_allowed?: boolean;
|
|
481
|
+
* }[];
|
|
482
|
+
* };
|
|
483
|
+
* };
|
|
484
|
+
*/
|
|
485
|
+
if (
|
|
486
|
+
data.mfa.methods.find(o => o.type === 'totp') &&
|
|
487
|
+
typeof this.manager.client.options.TOTPKey === 'string'
|
|
488
|
+
) {
|
|
489
|
+
// Get mfa code
|
|
490
|
+
const otp = this.manager.client.authenticator.generate(this.manager.client.options.TOTPKey);
|
|
491
|
+
if (hasDebugListener) {
|
|
492
|
+
this.manager.client.emit(
|
|
493
|
+
DEBUG,
|
|
494
|
+
`[Request Handler] ${data.message}
|
|
495
|
+
Method : ${request.method}
|
|
496
|
+
Path : ${request.path}
|
|
497
|
+
Route : ${request.route}
|
|
498
|
+
mfaCode : ${otp}`,
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
// Get ticket
|
|
502
|
+
const mfaData = data.mfa;
|
|
503
|
+
const mfaPost = await this.manager.client.api.mfa.finish.post({
|
|
504
|
+
data: {
|
|
505
|
+
ticket: mfaData.ticket,
|
|
506
|
+
data: otp,
|
|
507
|
+
mfa_type: 'totp',
|
|
508
|
+
},
|
|
509
|
+
});
|
|
510
|
+
request.options.mfaToken = mfaPost.token;
|
|
511
|
+
request.retries++;
|
|
512
|
+
return run();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
} catch (err) {
|
|
516
|
+
throw new HTTPError(err.message, err.constructor.name, err.status, request);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
throw new DiscordAPIError(data, res.status, request);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// Handle 5xx responses
|
|
523
|
+
if (res.status >= 500 && res.status < 600) {
|
|
524
|
+
// Retry the specified number of times for possible serverside issues
|
|
525
|
+
if (request.retries === this.manager.client.options.retryLimit) {
|
|
526
|
+
throw new HTTPError(res.statusText, res.constructor.name, res.status, request);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
await this.manager.coordinator?.sleepBackoff?.(res.status, request.retries);
|
|
530
|
+
request.retries++;
|
|
531
|
+
return run();
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Fallback in the rare case a status code outside the range 200..=599 is returned
|
|
535
|
+
return null;
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
return run(captchaKey, captchaRqToken, captchaSessionId);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
module.exports = RequestHandler;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* @external HTTPMethod
|
|
546
|
+
* @see {@link https://developer.mozilla.org/docs/Web/HTTP/Methods}
|
|
547
|
+
*/
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* @external Response
|
|
551
|
+
* @see {@link https://developer.mozilla.org/docs/Web/API/Response}
|
|
552
|
+
*/
|