lolka.js 0.1.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/package.json +25 -52
- package/src/client/BaseClient.js +120 -0
- package/src/client/Client.js +147 -446
- package/src/client/WebhookClient.js +103 -0
- package/src/client/actions/Action.js +3 -4
- package/src/client/actions/ActionsManager.js +65 -55
- package/src/client/actions/ApplicationCommandPermissionsUpdate.js +34 -0
- package/src/client/actions/AutoModerationActionExecution.js +26 -0
- package/src/client/actions/AutoModerationRuleCreate.js +27 -0
- package/src/client/actions/AutoModerationRuleDelete.js +31 -0
- package/src/client/actions/AutoModerationRuleUpdate.js +29 -0
- package/src/client/actions/ChannelCreate.js +3 -5
- package/src/client/actions/ChannelDelete.js +3 -4
- package/src/client/actions/ChannelUpdate.js +3 -3
- package/src/client/actions/EntitlementCreate.js +23 -0
- package/src/client/actions/EntitlementDelete.js +27 -0
- package/src/client/actions/EntitlementUpdate.js +25 -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 +3 -3
- package/src/client/actions/GuildDelete.js +44 -0
- package/src/client/actions/GuildEmojiCreate.js +3 -4
- package/src/client/actions/GuildEmojiDelete.js +3 -4
- package/src/client/actions/GuildEmojiUpdate.js +3 -4
- package/src/client/actions/GuildEmojisUpdate.js +2 -2
- package/src/client/actions/GuildIntegrationsUpdate.js +19 -0
- package/src/client/actions/GuildMemberRemove.js +6 -8
- package/src/client/actions/GuildMemberUpdate.js +6 -7
- package/src/client/actions/GuildRoleCreate.js +4 -6
- package/src/client/actions/GuildRoleDelete.js +3 -4
- package/src/client/actions/GuildRoleUpdate.js +39 -0
- package/src/client/actions/GuildRolesPositionUpdate.js +3 -3
- package/src/client/actions/GuildScheduledEventCreate.js +27 -0
- package/src/client/actions/GuildScheduledEventDelete.js +3 -4
- package/src/client/actions/GuildScheduledEventUpdate.js +30 -0
- package/src/client/actions/GuildScheduledEventUserAdd.js +3 -4
- package/src/client/actions/GuildScheduledEventUserRemove.js +3 -4
- package/src/client/actions/GuildSoundboardSoundDelete.js +3 -4
- package/src/client/actions/GuildStickerCreate.js +3 -4
- package/src/client/actions/GuildStickerDelete.js +3 -4
- package/src/client/actions/GuildStickerUpdate.js +3 -4
- package/src/client/actions/GuildStickersUpdate.js +2 -2
- package/src/client/actions/GuildUpdate.js +3 -4
- package/src/client/actions/InteractionCreate.js +15 -18
- package/src/client/actions/InviteCreate.js +27 -0
- package/src/client/actions/InviteDelete.js +29 -0
- package/src/client/actions/MessageCreate.js +3 -4
- package/src/client/actions/MessageDelete.js +3 -4
- package/src/client/actions/MessageDeleteBulk.js +3 -5
- package/src/client/actions/MessagePollVoteAdd.js +3 -4
- package/src/client/actions/MessagePollVoteRemove.js +3 -4
- package/src/client/actions/MessageReactionAdd.js +5 -7
- package/src/client/actions/MessageReactionRemove.js +4 -5
- package/src/client/actions/MessageReactionRemoveAll.js +4 -5
- package/src/client/actions/MessageReactionRemoveEmoji.js +4 -5
- package/src/client/actions/MessageUpdate.js +2 -2
- package/src/client/actions/PresenceUpdate.js +45 -0
- package/src/client/actions/StageInstanceCreate.js +3 -4
- package/src/client/actions/StageInstanceDelete.js +3 -4
- package/src/client/actions/StageInstanceUpdate.js +3 -4
- package/src/client/actions/ThreadCreate.js +3 -5
- package/src/client/actions/ThreadDelete.js +26 -0
- package/src/client/actions/ThreadListSync.js +60 -0
- package/src/client/actions/ThreadMemberUpdate.js +30 -0
- package/src/client/actions/ThreadMembersUpdate.js +3 -5
- package/src/client/actions/TypingStart.js +5 -6
- package/src/client/actions/UserUpdate.js +3 -4
- package/src/client/actions/VoiceStateUpdate.js +43 -0
- package/src/client/actions/WebhooksUpdate.js +37 -0
- package/src/client/voice/ClientVoiceManager.js +6 -10
- package/src/client/voice/VoiceConnection.js +420 -0
- package/src/client/websocket/WebSocketManager.js +407 -0
- package/src/client/websocket/WebSocketShard.js +234 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +2 -27
- package/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.js +2 -15
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.js +2 -16
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.js +2 -19
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.js +2 -18
- package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +1 -2
- package/src/client/websocket/handlers/CHANNEL_UPDATE.js +1 -2
- package/src/client/websocket/handlers/ENTITLEMENT_CREATE.js +2 -12
- package/src/client/websocket/handlers/ENTITLEMENT_DELETE.js +2 -16
- package/src/client/websocket/handlers/ENTITLEMENT_UPDATE.js +2 -14
- package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +2 -17
- package/src/client/websocket/handlers/GUILD_BAN_ADD.js +2 -13
- package/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +2 -18
- package/src/client/websocket/handlers/GUILD_CREATE.js +5 -7
- package/src/client/websocket/handlers/GUILD_DELETE.js +2 -34
- package/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.js +2 -13
- package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +1 -3
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +11 -9
- package/src/client/websocket/handlers/GUILD_MEMBER_REMOVE.js +2 -2
- package/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.js +2 -2
- package/src/client/websocket/handlers/GUILD_ROLE_UPDATE.js +2 -19
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.js +2 -15
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.js +2 -17
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUNDS_UPDATE.js +1 -2
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_CREATE.js +1 -2
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_UPDATE.js +1 -2
- package/src/client/websocket/handlers/INVITE_CREATE.js +2 -18
- package/src/client/websocket/handlers/INVITE_DELETE.js +2 -21
- package/src/client/websocket/handlers/MESSAGE_UPDATE.js +1 -2
- package/src/client/websocket/handlers/PRESENCE_UPDATE.js +2 -40
- package/src/client/websocket/handlers/RATE_LIMITED.js +2 -1
- package/src/client/websocket/handlers/READY.js +5 -8
- package/src/client/websocket/handlers/RESUMED.js +14 -0
- package/src/client/websocket/handlers/SOUNDBOARD_SOUNDS.js +1 -2
- package/src/client/websocket/handlers/SUBSCRIPTION_CREATE.js +1 -2
- package/src/client/websocket/handlers/SUBSCRIPTION_DELETE.js +1 -2
- package/src/client/websocket/handlers/SUBSCRIPTION_UPDATE.js +1 -2
- package/src/client/websocket/handlers/THREAD_DELETE.js +2 -15
- package/src/client/websocket/handlers/THREAD_LIST_SYNC.js +2 -49
- package/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.js +2 -23
- package/src/client/websocket/handlers/THREAD_UPDATE.js +1 -2
- package/src/client/websocket/handlers/VOICE_CHANNEL_EFFECT_SEND.js +2 -3
- package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js +3 -30
- package/src/client/websocket/handlers/VOICE_STATE_UPDATE.js +2 -35
- package/src/client/websocket/handlers/WEBHOOKS_UPDATE.js +2 -14
- package/src/client/websocket/handlers/index.js +75 -74
- package/src/errors/DJSError.js +7 -7
- package/src/errors/ErrorCodes.js +134 -58
- package/src/errors/Messages.js +143 -115
- package/src/errors/index.js +3 -5
- package/src/index.js +230 -234
- package/src/managers/ApplicationCommandManager.js +24 -60
- package/src/managers/ApplicationCommandPermissionsManager.js +33 -57
- package/src/managers/ApplicationEmojiManager.js +8 -18
- package/src/managers/AutoModerationRuleManager.js +6 -24
- package/src/managers/BaseGuildEmojiManager.js +85 -0
- package/src/managers/BaseManager.js +1 -3
- package/src/managers/CachedManager.js +8 -12
- package/src/managers/CategoryChannelChildManager.js +4 -10
- package/src/managers/ChannelManager.js +8 -70
- package/src/managers/DMMessageManager.js +2 -4
- package/src/managers/DataManager.js +5 -10
- package/src/managers/EntitlementManager.js +8 -19
- package/src/managers/GuildApplicationCommandManager.js +3 -6
- package/src/managers/GuildBanManager.js +52 -27
- package/src/managers/GuildChannelManager.js +54 -91
- package/src/managers/GuildEmojiManager.js +18 -108
- package/src/managers/GuildEmojiRoleManager.js +14 -24
- package/src/managers/GuildForumThreadManager.js +5 -10
- package/src/managers/GuildInviteManager.js +27 -51
- package/src/managers/GuildManager.js +185 -45
- package/src/managers/GuildMemberManager.js +105 -89
- package/src/managers/GuildMemberRoleManager.js +14 -29
- package/src/managers/GuildMessageManager.js +2 -20
- package/src/managers/GuildScheduledEventManager.js +13 -31
- package/src/managers/GuildSoundboardSoundManager.js +5 -17
- package/src/managers/GuildStickerManager.js +21 -34
- package/src/managers/GuildTextThreadManager.js +5 -9
- package/src/managers/MessageManager.js +77 -64
- package/src/managers/PartialGroupDMMessageManager.js +2 -4
- package/src/managers/PermissionOverwriteManager.js +15 -26
- package/src/managers/PollAnswerVoterManager.js +2 -14
- package/src/managers/PresenceManager.js +6 -11
- package/src/managers/ReactionManager.js +6 -13
- package/src/managers/ReactionUserManager.js +4 -11
- package/src/managers/RoleManager.js +74 -76
- package/src/managers/StageInstanceManager.js +8 -18
- package/src/managers/SubscriptionManager.js +4 -9
- package/src/managers/ThreadManager.js +13 -33
- package/src/managers/ThreadMemberManager.js +34 -27
- package/src/managers/UserManager.js +34 -32
- package/src/managers/VoiceStateManager.js +4 -9
- package/src/sharding/Shard.js +40 -64
- package/src/sharding/ShardClientUtil.js +60 -60
- package/src/sharding/ShardingManager.js +30 -89
- package/src/structures/ActionRow.js +18 -6
- package/src/structures/ActionRowBuilder.js +35 -0
- package/src/structures/AnonymousGuild.js +10 -14
- package/src/structures/ApplicationCommand.js +49 -67
- package/src/structures/ApplicationEmoji.js +5 -22
- package/src/structures/ApplicationRoleConnectionMetadata.js +0 -6
- package/src/structures/Attachment.js +7 -25
- package/src/structures/AttachmentBuilder.js +111 -0
- package/src/structures/AutoModerationActionExecution.js +2 -17
- package/src/structures/AutoModerationRule.js +17 -46
- package/src/structures/AutocompleteInteraction.js +4 -14
- package/src/structures/Base.js +2 -4
- package/src/structures/BaseChannel.js +4 -21
- package/src/structures/BaseGuild.js +7 -19
- package/src/structures/BaseGuildEmoji.js +13 -13
- package/src/structures/BaseGuildTextChannel.js +16 -45
- package/src/structures/BaseGuildVoiceChannel.js +12 -43
- package/src/structures/BaseInteraction.js +25 -54
- package/src/structures/BaseSelectMenuComponent.js +2 -8
- package/src/structures/ButtonBuilder.js +44 -0
- package/src/structures/ButtonComponent.js +2 -9
- package/src/structures/ButtonInteraction.js +2 -3
- package/src/structures/CategoryChannel.js +3 -8
- package/src/structures/ChannelSelectMenuBuilder.js +31 -0
- package/src/structures/ChannelSelectMenuComponent.js +2 -4
- package/src/structures/ChannelSelectMenuInteraction.js +2 -5
- package/src/structures/ChatInputCommandInteraction.js +4 -7
- package/src/structures/ClientApplication.js +12 -60
- package/src/structures/ClientPresence.js +10 -15
- package/src/structures/ClientUser.js +19 -36
- package/src/structures/CommandInteraction.js +7 -27
- package/src/structures/CommandInteractionOptionResolver.js +8 -36
- package/src/structures/Component.js +1 -7
- package/src/structures/ContainerComponent.js +3 -9
- package/src/structures/ContextMenuCommandInteraction.js +5 -9
- package/src/structures/DMChannel.js +15 -34
- package/src/structures/DirectoryChannel.js +2 -6
- package/src/structures/Embed.js +2 -19
- package/src/structures/EmbedBuilder.js +50 -0
- package/src/structures/Emoji.js +19 -19
- package/src/structures/Entitlement.js +2 -22
- package/src/structures/FileComponent.js +3 -7
- package/src/structures/ForumChannel.js +3 -6
- package/src/structures/Guild.js +131 -187
- package/src/structures/GuildAuditLogs.js +6 -12
- package/src/structures/GuildAuditLogsEntry.js +63 -88
- package/src/structures/GuildBan.js +3 -9
- package/src/structures/GuildChannel.js +33 -68
- package/src/structures/GuildEmoji.js +7 -19
- package/src/structures/GuildMember.js +58 -107
- package/src/structures/GuildOnboarding.js +2 -9
- package/src/structures/GuildOnboardingPrompt.js +2 -12
- package/src/structures/GuildOnboardingPromptOption.js +2 -12
- package/src/structures/GuildPreview.js +21 -30
- package/src/structures/GuildPreviewEmoji.js +2 -5
- package/src/structures/GuildScheduledEvent.js +19 -59
- package/src/structures/GuildTemplate.js +45 -24
- package/src/structures/Integration.js +7 -30
- package/src/structures/IntegrationApplication.js +12 -9
- package/src/structures/InteractionCallback.js +1 -10
- package/src/structures/InteractionCallbackResource.js +2 -7
- package/src/structures/InteractionCallbackResponse.js +3 -6
- package/src/structures/InteractionCollector.js +36 -23
- package/src/structures/InteractionResponse.js +102 -0
- package/src/structures/InteractionWebhook.js +6 -14
- package/src/structures/Invite.js +341 -0
- package/src/structures/InviteGuild.js +4 -6
- package/src/structures/InviteStageInstance.js +87 -0
- package/src/structures/LabelComponent.js +3 -3
- package/src/structures/MediaChannel.js +2 -3
- package/src/structures/MediaGalleryComponent.js +3 -6
- package/src/structures/MediaGalleryItem.js +2 -7
- package/src/structures/MentionableSelectMenuBuilder.js +32 -0
- package/src/structures/MentionableSelectMenuComponent.js +2 -3
- package/src/structures/MentionableSelectMenuInteraction.js +3 -8
- package/src/structures/Message.js +100 -169
- package/src/structures/MessageCollector.js +5 -16
- package/src/structures/MessageComponentInteraction.js +14 -34
- package/src/structures/MessageContextMenuCommandInteraction.js +2 -4
- package/src/structures/MessageMentions.js +4 -30
- package/src/structures/MessagePayload.js +90 -92
- package/src/structures/MessageReaction.js +17 -31
- package/src/structures/ModalBuilder.js +36 -0
- package/src/structures/{ModalComponentResolver.js → ModalSubmitFields.js} +31 -37
- package/src/structures/ModalSubmitInteraction.js +55 -54
- package/src/structures/NewsChannel.js +32 -0
- package/src/structures/OAuth2Guild.js +3 -6
- package/src/structures/PartialGroupDMChannel.js +11 -23
- package/src/structures/PermissionOverwrites.js +25 -47
- package/src/structures/Poll.js +4 -19
- package/src/structures/PollAnswer.js +29 -13
- package/src/structures/Presence.js +13 -57
- package/src/structures/PrimaryEntryPointCommandInteraction.js +2 -3
- package/src/structures/ReactionCollector.js +4 -21
- package/src/structures/ReactionEmoji.js +3 -5
- package/src/structures/Role.js +48 -65
- package/src/structures/RoleSelectMenuBuilder.js +31 -0
- package/src/structures/RoleSelectMenuComponent.js +2 -3
- package/src/structures/RoleSelectMenuInteraction.js +2 -5
- package/src/structures/SKU.js +2 -9
- package/src/structures/SectionComponent.js +3 -7
- package/src/structures/SelectMenuBuilder.js +26 -0
- package/src/structures/SelectMenuComponent.js +26 -0
- package/src/structures/SelectMenuInteraction.js +26 -0
- package/src/structures/SelectMenuOptionBuilder.js +26 -0
- package/src/structures/SeparatorComponent.js +2 -5
- package/src/structures/SoundboardSound.js +2 -18
- package/src/structures/StageChannel.js +4 -13
- package/src/structures/StageInstance.js +16 -19
- package/src/structures/Sticker.js +4 -28
- package/src/structures/StickerPack.js +4 -16
- package/src/structures/StringSelectMenuBuilder.js +79 -0
- package/src/structures/StringSelectMenuComponent.js +2 -4
- package/src/structures/StringSelectMenuInteraction.js +2 -4
- package/src/structures/StringSelectMenuOptionBuilder.js +49 -0
- package/src/structures/Subscription.js +1 -15
- package/src/structures/Team.js +7 -16
- package/src/structures/TeamMember.js +15 -9
- package/src/structures/TextChannel.js +3 -6
- package/src/structures/TextDisplayComponent.js +2 -4
- package/src/structures/TextInputBuilder.js +31 -0
- package/src/structures/TextInputComponent.js +2 -5
- package/src/structures/ThreadChannel.js +35 -85
- package/src/structures/ThreadMember.js +12 -17
- package/src/structures/ThreadOnlyChannel.js +15 -36
- package/src/structures/ThumbnailComponent.js +3 -8
- package/src/structures/Typing.js +2 -10
- package/src/structures/UnfurledMediaItem.js +2 -5
- package/src/structures/User.js +68 -60
- package/src/structures/UserContextMenuCommandInteraction.js +2 -5
- package/src/structures/UserSelectMenuBuilder.js +31 -0
- package/src/structures/UserSelectMenuComponent.js +2 -3
- package/src/structures/UserSelectMenuInteraction.js +3 -7
- package/src/structures/VoiceChannel.js +4 -11
- package/src/structures/VoiceChannelEffect.js +2 -12
- package/src/structures/VoiceRegion.js +2 -7
- package/src/structures/VoiceState.js +10 -36
- package/src/structures/Webhook.js +37 -65
- package/src/structures/WelcomeChannel.js +5 -12
- package/src/structures/WelcomeScreen.js +3 -8
- package/src/structures/Widget.js +3 -13
- package/src/structures/WidgetMember.js +2 -17
- package/src/structures/interfaces/Application.js +10 -19
- package/src/structures/interfaces/Collector.js +12 -45
- package/src/structures/interfaces/InteractionResponses.js +152 -43
- package/src/structures/interfaces/TextBasedChannel.js +99 -83
- package/src/util/APITypes.js +16 -30
- package/src/util/ActivityFlagsBitField.js +2 -5
- package/src/util/ApplicationFlagsBitField.js +6 -11
- package/src/util/AttachmentFlagsBitField.js +2 -5
- package/src/util/BitField.js +8 -27
- package/src/util/ChannelFlagsBitField.js +6 -11
- package/src/util/Channels.js +61 -73
- package/src/util/Colors.js +43 -43
- package/src/util/Components.js +82 -61
- package/src/util/Constants.js +124 -94
- package/src/util/DataResolver.js +17 -36
- package/src/util/Enums.js +1 -2
- package/src/util/Events.js +14 -3
- package/src/util/Formatters.js +413 -0
- package/src/util/GuildMemberFlagsBitField.js +5 -9
- package/src/util/IntentsBitField.js +6 -10
- package/src/util/InviteFlagsBitField.js +1 -2
- package/src/util/LimitedCollection.js +6 -13
- package/src/util/LolkaSnowflake.js +14 -1
- package/src/util/MessageFlagsBitField.js +6 -11
- package/src/util/Options.js +57 -35
- package/src/util/Partials.js +4 -5
- package/src/util/PermissionsBitField.js +8 -21
- package/src/util/RoleFlagsBitField.js +2 -5
- package/src/util/SKUFlagsBitField.js +1 -4
- package/src/util/ShardEvents.js +3 -1
- package/src/util/Status.js +21 -5
- package/src/util/Sweepers.js +13 -59
- package/src/util/SystemChannelFlagsBitField.js +6 -11
- package/src/util/ThreadMemberFlagsBitField.js +2 -6
- package/src/util/Transformers.js +18 -17
- package/src/util/UserFlagsBitField.js +2 -6
- package/src/util/Util.js +97 -148
- package/src/util/WebSocketShardEvents.js +25 -0
- package/typings/index.d.mts +2432 -1773
- package/typings/index.d.ts +2432 -1773
- package/typings/rawDataTypes.d.ts +211 -0
- package/src/structures/ActivityInstance.js +0 -52
- package/src/structures/ActivityLocation.js +0 -65
- package/src/structures/AnnouncementChannel.js +0 -29
- package/src/structures/AuthorizingIntegrationOwners.js +0 -64
- package/src/structures/BaseInvite.js +0 -187
- package/src/structures/GroupDMInvite.js +0 -37
- package/src/structures/GuildInvite.js +0 -223
- package/src/util/Invites.js +0 -31
package/src/sharding/Shard.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
/* eslint-disable promise/prefer-await-to-callbacks, promise/prefer-await-to-then, no-use-before-define */
|
|
2
1
|
'use strict';
|
|
3
2
|
|
|
3
|
+
const EventEmitter = require('node:events');
|
|
4
4
|
const path = require('node:path');
|
|
5
5
|
const process = require('node:process');
|
|
6
6
|
const { setTimeout, clearTimeout } = require('node:timers');
|
|
7
7
|
const { setTimeout: sleep } = require('node:timers/promises');
|
|
8
8
|
const { SHARE_ENV } = require('node:worker_threads');
|
|
9
|
-
const {
|
|
10
|
-
const
|
|
11
|
-
const {
|
|
12
|
-
const { makeError, makePlainError } = require('../util/Util.js');
|
|
9
|
+
const { DiscordjsError, ErrorCodes } = require('../errors');
|
|
10
|
+
const ShardEvents = require('../util/ShardEvents');
|
|
11
|
+
const { makeError, makePlainError } = require('../util/Util');
|
|
13
12
|
|
|
14
13
|
let childProcess = null;
|
|
15
14
|
let Worker = null;
|
|
@@ -18,10 +17,9 @@ let Worker = null;
|
|
|
18
17
|
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
|
|
19
18
|
* an instance of the bot and its {@link Client}. When its child process/worker exits for any reason, the shard will
|
|
20
19
|
* spawn a new one to replace it as necessary.
|
|
21
|
-
*
|
|
22
|
-
* @extends {AsyncEventEmitter}
|
|
20
|
+
* @extends {EventEmitter}
|
|
23
21
|
*/
|
|
24
|
-
class Shard extends
|
|
22
|
+
class Shard extends EventEmitter {
|
|
25
23
|
constructor(manager, id) {
|
|
26
24
|
super();
|
|
27
25
|
|
|
@@ -32,82 +30,69 @@ class Shard extends AsyncEventEmitter {
|
|
|
32
30
|
case 'worker':
|
|
33
31
|
Worker = require('node:worker_threads').Worker;
|
|
34
32
|
break;
|
|
35
|
-
default:
|
|
36
|
-
throw new Error(`Invalid sharding mode in Shard ${id}`);
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
/**
|
|
40
36
|
* Manager that created the shard
|
|
41
|
-
*
|
|
42
37
|
* @type {ShardingManager}
|
|
43
38
|
*/
|
|
44
39
|
this.manager = manager;
|
|
45
40
|
|
|
46
41
|
/**
|
|
47
42
|
* The shard's id in the manager
|
|
48
|
-
*
|
|
49
43
|
* @type {number}
|
|
50
44
|
*/
|
|
51
45
|
this.id = id;
|
|
52
46
|
|
|
53
47
|
/**
|
|
54
48
|
* Whether to pass silent flag to the shard's process (only when {@link ShardingManager#mode} is `process`)
|
|
55
|
-
*
|
|
56
49
|
* @type {boolean}
|
|
57
50
|
*/
|
|
58
51
|
this.silent = manager.silent;
|
|
59
52
|
|
|
60
53
|
/**
|
|
61
54
|
* Arguments for the shard's process/worker
|
|
62
|
-
*
|
|
63
55
|
* @type {string[]}
|
|
64
56
|
*/
|
|
65
57
|
this.args = manager.shardArgs ?? [];
|
|
66
58
|
|
|
67
59
|
/**
|
|
68
60
|
* Arguments for the shard's process/worker executable
|
|
69
|
-
*
|
|
70
61
|
* @type {string[]}
|
|
71
62
|
*/
|
|
72
63
|
this.execArgv = manager.execArgv;
|
|
73
64
|
|
|
74
65
|
/**
|
|
75
66
|
* Environment variables for the shard's process, or workerData for the shard's worker
|
|
76
|
-
*
|
|
77
67
|
* @type {Object}
|
|
78
68
|
*/
|
|
79
|
-
this.env = {
|
|
80
|
-
...process.env,
|
|
69
|
+
this.env = Object.assign({}, process.env, {
|
|
81
70
|
SHARDING_MANAGER: true,
|
|
82
71
|
SHARDS: this.id,
|
|
83
72
|
SHARD_COUNT: this.manager.totalShards,
|
|
84
73
|
DISCORD_TOKEN: this.manager.token,
|
|
85
|
-
};
|
|
74
|
+
});
|
|
86
75
|
|
|
87
76
|
/**
|
|
88
77
|
* Whether the shard's {@link Client} is ready
|
|
89
|
-
*
|
|
90
78
|
* @type {boolean}
|
|
91
79
|
*/
|
|
92
80
|
this.ready = false;
|
|
93
81
|
|
|
94
82
|
/**
|
|
95
83
|
* Process of the shard (if {@link ShardingManager#mode} is `process`)
|
|
96
|
-
*
|
|
97
84
|
* @type {?ChildProcess}
|
|
98
85
|
*/
|
|
99
86
|
this.process = null;
|
|
100
87
|
|
|
101
88
|
/**
|
|
102
89
|
* Worker of the shard (if {@link ShardingManager#mode} is `worker`)
|
|
103
|
-
*
|
|
104
90
|
* @type {?Worker}
|
|
105
91
|
*/
|
|
106
92
|
this.worker = null;
|
|
107
93
|
|
|
108
94
|
/**
|
|
109
95
|
* Ongoing promises for calls to {@link Shard#eval}, mapped by the `script` they were called with
|
|
110
|
-
*
|
|
111
96
|
* @type {Map<string, Promise>}
|
|
112
97
|
* @private
|
|
113
98
|
*/
|
|
@@ -115,7 +100,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
115
100
|
|
|
116
101
|
/**
|
|
117
102
|
* Ongoing promises for calls to {@link Shard#fetchClientValue}, mapped by the `prop` they were called with
|
|
118
|
-
*
|
|
119
103
|
* @type {Map<string, Promise>}
|
|
120
104
|
* @private
|
|
121
105
|
*/
|
|
@@ -123,7 +107,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
123
107
|
|
|
124
108
|
/**
|
|
125
109
|
* Listener function for the {@link ChildProcess}' `exit` event
|
|
126
|
-
*
|
|
127
110
|
* @type {Function}
|
|
128
111
|
* @private
|
|
129
112
|
*/
|
|
@@ -133,7 +116,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
133
116
|
/**
|
|
134
117
|
* Forks a child process or creates a worker thread for the shard.
|
|
135
118
|
* <warn>You should not need to call this manually.</warn>
|
|
136
|
-
*
|
|
137
119
|
* @param {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
|
|
138
120
|
* before resolving (`-1` or `Infinity` for no wait)
|
|
139
121
|
* @returns {Promise<ChildProcess>}
|
|
@@ -165,8 +147,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
165
147
|
.on('message', this._handleMessage.bind(this))
|
|
166
148
|
.on('exit', this._exitListener);
|
|
167
149
|
break;
|
|
168
|
-
default:
|
|
169
|
-
break;
|
|
170
150
|
}
|
|
171
151
|
|
|
172
152
|
this._evals.clear();
|
|
@@ -176,7 +156,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
176
156
|
|
|
177
157
|
/**
|
|
178
158
|
* Emitted upon the creation of the shard's child process/worker.
|
|
179
|
-
*
|
|
180
159
|
* @event Shard#spawn
|
|
181
160
|
* @param {ChildProcess|Worker} process Child process/worker that was created
|
|
182
161
|
*/
|
|
@@ -235,7 +214,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
235
214
|
|
|
236
215
|
/**
|
|
237
216
|
* Options used to respawn a shard.
|
|
238
|
-
*
|
|
239
217
|
* @typedef {Object} ShardRespawnOptions
|
|
240
218
|
* @property {number} [delay=500] How long to wait between killing the process/worker and
|
|
241
219
|
* restarting it (in milliseconds)
|
|
@@ -245,7 +223,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
245
223
|
|
|
246
224
|
/**
|
|
247
225
|
* Kills and restarts the shard's process/worker.
|
|
248
|
-
*
|
|
249
226
|
* @param {ShardRespawnOptions} [options] Options for respawning the shard
|
|
250
227
|
* @returns {Promise<ChildProcess>}
|
|
251
228
|
*/
|
|
@@ -257,11 +234,10 @@ class Shard extends AsyncEventEmitter {
|
|
|
257
234
|
|
|
258
235
|
/**
|
|
259
236
|
* Sends a message to the shard's process/worker.
|
|
260
|
-
*
|
|
261
237
|
* @param {*} message Message to send to the shard
|
|
262
238
|
* @returns {Promise<Shard>}
|
|
263
239
|
*/
|
|
264
|
-
|
|
240
|
+
send(message) {
|
|
265
241
|
return new Promise((resolve, reject) => {
|
|
266
242
|
if (this.process) {
|
|
267
243
|
this.process.send(message, err => {
|
|
@@ -277,7 +253,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
277
253
|
|
|
278
254
|
/**
|
|
279
255
|
* Fetches a client property value of the shard.
|
|
280
|
-
*
|
|
281
256
|
* @param {string} prop Name of the client property to get, using periods for nesting
|
|
282
257
|
* @returns {Promise<*>}
|
|
283
258
|
* @example
|
|
@@ -302,18 +277,18 @@ class Shard extends AsyncEventEmitter {
|
|
|
302
277
|
child.removeListener('message', listener);
|
|
303
278
|
this.decrementMaxListeners(child);
|
|
304
279
|
this._fetches.delete(prop);
|
|
305
|
-
if (message._error)
|
|
306
|
-
else
|
|
280
|
+
if (!message._error) resolve(message._result);
|
|
281
|
+
else reject(makeError(message._error));
|
|
307
282
|
};
|
|
308
283
|
|
|
309
284
|
this.incrementMaxListeners(child);
|
|
310
285
|
child.on('message', listener);
|
|
311
286
|
|
|
312
|
-
this.send({ _fetchProp: prop }).catch(
|
|
287
|
+
this.send({ _fetchProp: prop }).catch(err => {
|
|
313
288
|
child.removeListener('message', listener);
|
|
314
289
|
this.decrementMaxListeners(child);
|
|
315
290
|
this._fetches.delete(prop);
|
|
316
|
-
reject(
|
|
291
|
+
reject(err);
|
|
317
292
|
});
|
|
318
293
|
});
|
|
319
294
|
|
|
@@ -323,7 +298,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
323
298
|
|
|
324
299
|
/**
|
|
325
300
|
* Evaluates a script or function on the shard, in the context of the {@link Client}.
|
|
326
|
-
*
|
|
327
301
|
* @param {string|Function} script JavaScript to run on the shard
|
|
328
302
|
* @param {*} [context] The context for the eval
|
|
329
303
|
* @returns {Promise<*>} Result of the script execution
|
|
@@ -348,18 +322,18 @@ class Shard extends AsyncEventEmitter {
|
|
|
348
322
|
child.removeListener('message', listener);
|
|
349
323
|
this.decrementMaxListeners(child);
|
|
350
324
|
this._evals.delete(_eval);
|
|
351
|
-
if (message._error)
|
|
352
|
-
else
|
|
325
|
+
if (!message._error) resolve(message._result);
|
|
326
|
+
else reject(makeError(message._error));
|
|
353
327
|
};
|
|
354
328
|
|
|
355
329
|
this.incrementMaxListeners(child);
|
|
356
330
|
child.on('message', listener);
|
|
357
331
|
|
|
358
|
-
this.send({ _eval }).catch(
|
|
332
|
+
this.send({ _eval }).catch(err => {
|
|
359
333
|
child.removeListener('message', listener);
|
|
360
334
|
this.decrementMaxListeners(child);
|
|
361
335
|
this._evals.delete(_eval);
|
|
362
|
-
reject(
|
|
336
|
+
reject(err);
|
|
363
337
|
});
|
|
364
338
|
});
|
|
365
339
|
|
|
@@ -369,7 +343,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
369
343
|
|
|
370
344
|
/**
|
|
371
345
|
* Handles a message received from the child process/worker.
|
|
372
|
-
*
|
|
373
346
|
* @param {*} message Message received
|
|
374
347
|
* @private
|
|
375
348
|
*/
|
|
@@ -379,8 +352,7 @@ class Shard extends AsyncEventEmitter {
|
|
|
379
352
|
if (message._ready) {
|
|
380
353
|
this.ready = true;
|
|
381
354
|
/**
|
|
382
|
-
* Emitted upon the shard's {@link Client#event:
|
|
383
|
-
*
|
|
355
|
+
* Emitted upon the shard's {@link Client#event:shardReady} event.
|
|
384
356
|
* @event Shard#ready
|
|
385
357
|
*/
|
|
386
358
|
this.emit(ShardEvents.Ready);
|
|
@@ -391,20 +363,29 @@ class Shard extends AsyncEventEmitter {
|
|
|
391
363
|
if (message._disconnect) {
|
|
392
364
|
this.ready = false;
|
|
393
365
|
/**
|
|
394
|
-
* Emitted upon the shard's {@link
|
|
395
|
-
*
|
|
366
|
+
* Emitted upon the shard's {@link Client#event:shardDisconnect} event.
|
|
396
367
|
* @event Shard#disconnect
|
|
397
368
|
*/
|
|
398
369
|
this.emit(ShardEvents.Disconnect);
|
|
399
370
|
return;
|
|
400
371
|
}
|
|
401
372
|
|
|
373
|
+
// Shard is attempting to reconnect
|
|
374
|
+
if (message._reconnecting) {
|
|
375
|
+
this.ready = false;
|
|
376
|
+
/**
|
|
377
|
+
* Emitted upon the shard's {@link Client#event:shardReconnecting} event.
|
|
378
|
+
* @event Shard#reconnecting
|
|
379
|
+
*/
|
|
380
|
+
this.emit(ShardEvents.Reconnecting);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
402
384
|
// Shard has resumed
|
|
403
385
|
if (message._resume) {
|
|
404
386
|
this.ready = true;
|
|
405
387
|
/**
|
|
406
|
-
* Emitted upon the shard's {@link
|
|
407
|
-
*
|
|
388
|
+
* Emitted upon the shard's {@link Client#event:shardResume} event.
|
|
408
389
|
* @event Shard#resume
|
|
409
390
|
*/
|
|
410
391
|
this.emit(ShardEvents.Resume);
|
|
@@ -415,8 +396,8 @@ class Shard extends AsyncEventEmitter {
|
|
|
415
396
|
if (message._sFetchProp) {
|
|
416
397
|
const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard };
|
|
417
398
|
this.manager.fetchClientValues(message._sFetchProp, message._sFetchPropShard).then(
|
|
418
|
-
|
|
419
|
-
|
|
399
|
+
results => this.send({ ...resp, _result: results }),
|
|
400
|
+
err => this.send({ ...resp, _error: makePlainError(err) }),
|
|
420
401
|
);
|
|
421
402
|
return;
|
|
422
403
|
}
|
|
@@ -425,8 +406,8 @@ class Shard extends AsyncEventEmitter {
|
|
|
425
406
|
if (message._sEval) {
|
|
426
407
|
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
|
|
427
408
|
this.manager._performOnShards('eval', [message._sEval], message._sEvalShard).then(
|
|
428
|
-
|
|
429
|
-
|
|
409
|
+
results => this.send({ ...resp, _result: results }),
|
|
410
|
+
err => this.send({ ...resp, _error: makePlainError(err) }),
|
|
430
411
|
);
|
|
431
412
|
return;
|
|
432
413
|
}
|
|
@@ -443,7 +424,6 @@ class Shard extends AsyncEventEmitter {
|
|
|
443
424
|
|
|
444
425
|
/**
|
|
445
426
|
* Emitted upon receiving a message from the child process/worker.
|
|
446
|
-
*
|
|
447
427
|
* @event Shard#message
|
|
448
428
|
* @param {*} message Message that was received
|
|
449
429
|
*/
|
|
@@ -452,16 +432,14 @@ class Shard extends AsyncEventEmitter {
|
|
|
452
432
|
|
|
453
433
|
/**
|
|
454
434
|
* Handles the shard's process/worker exiting.
|
|
455
|
-
*
|
|
456
435
|
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
|
|
457
436
|
* @param {number} [timeout] The amount in milliseconds to wait until the {@link Client}
|
|
458
437
|
* has become ready (`-1` or `Infinity` for no wait)
|
|
459
438
|
* @private
|
|
460
439
|
*/
|
|
461
|
-
_handleExit(respawn = this.manager.respawn, timeout
|
|
440
|
+
_handleExit(respawn = this.manager.respawn, timeout) {
|
|
462
441
|
/**
|
|
463
442
|
* Emitted upon the shard's child process/worker exiting.
|
|
464
|
-
*
|
|
465
443
|
* @event Shard#death
|
|
466
444
|
* @param {ChildProcess|Worker} process Child process/worker that exited
|
|
467
445
|
*/
|
|
@@ -473,13 +451,12 @@ class Shard extends AsyncEventEmitter {
|
|
|
473
451
|
this._evals.clear();
|
|
474
452
|
this._fetches.clear();
|
|
475
453
|
|
|
476
|
-
if (respawn) this.spawn(timeout).catch(
|
|
454
|
+
if (respawn) this.spawn(timeout).catch(err => this.emit(ShardEvents.Error, err));
|
|
477
455
|
}
|
|
478
456
|
|
|
479
457
|
/**
|
|
480
458
|
* Increments max listeners by one for a given emitter, if they are not zero.
|
|
481
|
-
*
|
|
482
|
-
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
|
|
459
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
483
460
|
* @private
|
|
484
461
|
*/
|
|
485
462
|
incrementMaxListeners(emitter) {
|
|
@@ -491,8 +468,7 @@ class Shard extends AsyncEventEmitter {
|
|
|
491
468
|
|
|
492
469
|
/**
|
|
493
470
|
* Decrements max listeners by one for a given emitter, if they are not zero.
|
|
494
|
-
*
|
|
495
|
-
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
|
|
471
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
496
472
|
* @private
|
|
497
473
|
*/
|
|
498
474
|
decrementMaxListeners(emitter) {
|
|
@@ -503,4 +479,4 @@ class Shard extends AsyncEventEmitter {
|
|
|
503
479
|
}
|
|
504
480
|
}
|
|
505
481
|
|
|
506
|
-
exports
|
|
482
|
+
module.exports = Shard;
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
/* eslint-disable promise/prefer-await-to-callbacks, promise/prefer-await-to-then */
|
|
2
1
|
'use strict';
|
|
3
2
|
|
|
4
3
|
const process = require('node:process');
|
|
5
4
|
const { calculateShardId } = require('@discordjs/util');
|
|
6
|
-
const {
|
|
7
|
-
const
|
|
8
|
-
const {
|
|
9
|
-
const { makeError, makePlainError } = require('../util/Util.js');
|
|
5
|
+
const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors');
|
|
6
|
+
const Events = require('../util/Events');
|
|
7
|
+
const { makeError, makePlainError } = require('../util/Util');
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
10
|
* Helper class for sharded clients spawned as a child process/worker, such as from a {@link ShardingManager}.
|
|
@@ -16,21 +14,18 @@ class ShardClientUtil {
|
|
|
16
14
|
constructor(client, mode) {
|
|
17
15
|
/**
|
|
18
16
|
* Client for the shard
|
|
19
|
-
*
|
|
20
17
|
* @type {Client}
|
|
21
18
|
*/
|
|
22
19
|
this.client = client;
|
|
23
20
|
|
|
24
21
|
/**
|
|
25
22
|
* Mode the shard was spawned with
|
|
26
|
-
*
|
|
27
23
|
* @type {ShardingManagerMode}
|
|
28
24
|
*/
|
|
29
25
|
this.mode = mode;
|
|
30
26
|
|
|
31
27
|
/**
|
|
32
28
|
* Message port for the master process (only when {@link ShardClientUtil#mode} is `worker`)
|
|
33
|
-
*
|
|
34
29
|
* @type {?MessagePort}
|
|
35
30
|
*/
|
|
36
31
|
this.parentPort = null;
|
|
@@ -38,42 +33,63 @@ class ShardClientUtil {
|
|
|
38
33
|
switch (mode) {
|
|
39
34
|
case 'process':
|
|
40
35
|
process.on('message', this._handleMessage.bind(this));
|
|
41
|
-
client.on(Events.
|
|
36
|
+
client.on(Events.ShardReady, () => {
|
|
42
37
|
process.send({ _ready: true });
|
|
43
38
|
});
|
|
44
|
-
client.
|
|
39
|
+
client.on(Events.ShardDisconnect, () => {
|
|
45
40
|
process.send({ _disconnect: true });
|
|
46
41
|
});
|
|
47
|
-
client.
|
|
42
|
+
client.on(Events.ShardReconnecting, () => {
|
|
43
|
+
process.send({ _reconnecting: true });
|
|
44
|
+
});
|
|
45
|
+
client.on(Events.ShardResume, () => {
|
|
48
46
|
process.send({ _resume: true });
|
|
49
47
|
});
|
|
50
48
|
break;
|
|
51
49
|
case 'worker':
|
|
52
50
|
this.parentPort = require('node:worker_threads').parentPort;
|
|
53
51
|
this.parentPort.on('message', this._handleMessage.bind(this));
|
|
54
|
-
client.on(Events.
|
|
52
|
+
client.on(Events.ShardReady, () => {
|
|
55
53
|
this.parentPort.postMessage({ _ready: true });
|
|
56
54
|
});
|
|
57
|
-
client.
|
|
55
|
+
client.on(Events.ShardDisconnect, () => {
|
|
58
56
|
this.parentPort.postMessage({ _disconnect: true });
|
|
59
57
|
});
|
|
60
|
-
client.
|
|
58
|
+
client.on(Events.ShardReconnecting, () => {
|
|
59
|
+
this.parentPort.postMessage({ _reconnecting: true });
|
|
60
|
+
});
|
|
61
|
+
client.on(Events.ShardResume, () => {
|
|
61
62
|
this.parentPort.postMessage({ _resume: true });
|
|
62
63
|
});
|
|
63
64
|
break;
|
|
64
|
-
default:
|
|
65
|
-
break;
|
|
66
65
|
}
|
|
67
66
|
}
|
|
68
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Array of shard ids of this client
|
|
70
|
+
* @type {number[]}
|
|
71
|
+
* @readonly
|
|
72
|
+
*/
|
|
73
|
+
get ids() {
|
|
74
|
+
return this.client.options.shards;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Total number of shards
|
|
79
|
+
* @type {number}
|
|
80
|
+
* @readonly
|
|
81
|
+
*/
|
|
82
|
+
get count() {
|
|
83
|
+
return this.client.options.shardCount;
|
|
84
|
+
}
|
|
85
|
+
|
|
69
86
|
/**
|
|
70
87
|
* Sends a message to the master process.
|
|
71
|
-
*
|
|
72
88
|
* @param {*} message Message to send
|
|
73
89
|
* @returns {Promise<void>}
|
|
74
90
|
* @emits Shard#message
|
|
75
91
|
*/
|
|
76
|
-
|
|
92
|
+
send(message) {
|
|
77
93
|
return new Promise((resolve, reject) => {
|
|
78
94
|
switch (this.mode) {
|
|
79
95
|
case 'process':
|
|
@@ -86,15 +102,12 @@ class ShardClientUtil {
|
|
|
86
102
|
this.parentPort.postMessage(message);
|
|
87
103
|
resolve();
|
|
88
104
|
break;
|
|
89
|
-
default:
|
|
90
|
-
break;
|
|
91
105
|
}
|
|
92
106
|
});
|
|
93
107
|
}
|
|
94
108
|
|
|
95
109
|
/**
|
|
96
110
|
* Fetches a client property value of each shard, or a given shard.
|
|
97
|
-
*
|
|
98
111
|
* @param {string} prop Name of the client property to get, using periods for nesting
|
|
99
112
|
* @param {number} [shard] Shard to fetch property from, all if undefined
|
|
100
113
|
* @returns {Promise<*|Array<*>>}
|
|
@@ -104,7 +117,7 @@ class ShardClientUtil {
|
|
|
104
117
|
* .catch(console.error);
|
|
105
118
|
* @see {@link ShardingManager#fetchClientValues}
|
|
106
119
|
*/
|
|
107
|
-
|
|
120
|
+
fetchClientValues(prop, shard) {
|
|
108
121
|
return new Promise((resolve, reject) => {
|
|
109
122
|
const parent = this.parentPort ?? process;
|
|
110
123
|
|
|
@@ -112,24 +125,22 @@ class ShardClientUtil {
|
|
|
112
125
|
if (message?._sFetchProp !== prop || message._sFetchPropShard !== shard) return;
|
|
113
126
|
parent.removeListener('message', listener);
|
|
114
127
|
this.decrementMaxListeners(parent);
|
|
115
|
-
if (message._error)
|
|
116
|
-
else
|
|
128
|
+
if (!message._error) resolve(message._result);
|
|
129
|
+
else reject(makeError(message._error));
|
|
117
130
|
};
|
|
118
|
-
|
|
119
131
|
this.incrementMaxListeners(parent);
|
|
120
132
|
parent.on('message', listener);
|
|
121
133
|
|
|
122
|
-
this.send({ _sFetchProp: prop, _sFetchPropShard: shard }).catch(
|
|
134
|
+
this.send({ _sFetchProp: prop, _sFetchPropShard: shard }).catch(err => {
|
|
123
135
|
parent.removeListener('message', listener);
|
|
124
136
|
this.decrementMaxListeners(parent);
|
|
125
|
-
reject(
|
|
137
|
+
reject(err);
|
|
126
138
|
});
|
|
127
139
|
});
|
|
128
140
|
}
|
|
129
141
|
|
|
130
142
|
/**
|
|
131
143
|
* Evaluates a script or function on all shards, or a given shard, in the context of the {@link Client}s.
|
|
132
|
-
*
|
|
133
144
|
* @param {Function} script JavaScript to run on each shard
|
|
134
145
|
* @param {BroadcastEvalOptions} [options={}] The options for the broadcast
|
|
135
146
|
* @returns {Promise<*|Array<*>>} Results of the script execution
|
|
@@ -139,48 +150,44 @@ class ShardClientUtil {
|
|
|
139
150
|
* .catch(console.error);
|
|
140
151
|
* @see {@link ShardingManager#broadcastEval}
|
|
141
152
|
*/
|
|
142
|
-
|
|
153
|
+
broadcastEval(script, options = {}) {
|
|
143
154
|
return new Promise((resolve, reject) => {
|
|
144
155
|
const parent = this.parentPort ?? process;
|
|
145
156
|
if (typeof script !== 'function') {
|
|
146
157
|
reject(new DiscordjsTypeError(ErrorCodes.ShardingInvalidEvalBroadcast));
|
|
147
158
|
return;
|
|
148
159
|
}
|
|
149
|
-
|
|
150
|
-
const evalScript = `(${script})(this, ${JSON.stringify(options.context)})`;
|
|
160
|
+
script = `(${script})(this, ${JSON.stringify(options.context)})`;
|
|
151
161
|
|
|
152
162
|
const listener = message => {
|
|
153
|
-
if (message?._sEval !==
|
|
163
|
+
if (message?._sEval !== script || message._sEvalShard !== options.shard) return;
|
|
154
164
|
parent.removeListener('message', listener);
|
|
155
165
|
this.decrementMaxListeners(parent);
|
|
156
|
-
if (message._error)
|
|
157
|
-
else
|
|
166
|
+
if (!message._error) resolve(message._result);
|
|
167
|
+
else reject(makeError(message._error));
|
|
158
168
|
};
|
|
159
|
-
|
|
160
169
|
this.incrementMaxListeners(parent);
|
|
161
170
|
parent.on('message', listener);
|
|
162
|
-
this.send({ _sEval:
|
|
171
|
+
this.send({ _sEval: script, _sEvalShard: options.shard }).catch(err => {
|
|
163
172
|
parent.removeListener('message', listener);
|
|
164
173
|
this.decrementMaxListeners(parent);
|
|
165
|
-
reject(
|
|
174
|
+
reject(err);
|
|
166
175
|
});
|
|
167
176
|
});
|
|
168
177
|
}
|
|
169
178
|
|
|
170
179
|
/**
|
|
171
180
|
* Requests a respawn of all shards.
|
|
172
|
-
*
|
|
173
181
|
* @param {MultipleShardRespawnOptions} [options] Options for respawning shards
|
|
174
182
|
* @returns {Promise<void>} Resolves upon the message being sent
|
|
175
183
|
* @see {@link ShardingManager#respawnAll}
|
|
176
184
|
*/
|
|
177
|
-
|
|
185
|
+
respawnAll({ shardDelay = 5_000, respawnDelay = 500, timeout = 30_000 } = {}) {
|
|
178
186
|
return this.send({ _sRespawnAll: { shardDelay, respawnDelay, timeout } });
|
|
179
187
|
}
|
|
180
188
|
|
|
181
189
|
/**
|
|
182
190
|
* Handles an IPC message.
|
|
183
|
-
*
|
|
184
191
|
* @param {*} message Message received
|
|
185
192
|
* @private
|
|
186
193
|
*/
|
|
@@ -192,36 +199,34 @@ class ShardClientUtil {
|
|
|
192
199
|
let value = this.client;
|
|
193
200
|
for (const prop of props) value = value[prop];
|
|
194
201
|
this._respond('fetchProp', { _fetchProp: message._fetchProp, _result: value });
|
|
195
|
-
} catch (
|
|
196
|
-
this._respond('fetchProp', { _fetchProp: message._fetchProp, _error: makePlainError(
|
|
202
|
+
} catch (err) {
|
|
203
|
+
this._respond('fetchProp', { _fetchProp: message._fetchProp, _error: makePlainError(err) });
|
|
197
204
|
}
|
|
198
205
|
} else if (message._eval) {
|
|
199
206
|
try {
|
|
200
207
|
this._respond('eval', { _eval: message._eval, _result: await this.client._eval(message._eval) });
|
|
201
|
-
} catch (
|
|
202
|
-
this._respond('eval', { _eval: message._eval, _error: makePlainError(
|
|
208
|
+
} catch (err) {
|
|
209
|
+
this._respond('eval', { _eval: message._eval, _error: makePlainError(err) });
|
|
203
210
|
}
|
|
204
211
|
}
|
|
205
212
|
}
|
|
206
213
|
|
|
207
214
|
/**
|
|
208
215
|
* Sends a message to the master process, emitting an error from the client upon failure.
|
|
209
|
-
*
|
|
210
216
|
* @param {string} type Type of response to send
|
|
211
217
|
* @param {*} message Message to send
|
|
212
218
|
* @private
|
|
213
219
|
*/
|
|
214
220
|
_respond(type, message) {
|
|
215
|
-
this.send(message).catch(
|
|
216
|
-
const error = new Error(`Error when sending ${type} response to master process: ${
|
|
217
|
-
error.stack =
|
|
221
|
+
this.send(message).catch(err => {
|
|
222
|
+
const error = new Error(`Error when sending ${type} response to master process: ${err.message}`);
|
|
223
|
+
error.stack = err.stack;
|
|
218
224
|
/**
|
|
219
225
|
* Emitted when the client encounters an error.
|
|
220
226
|
* <warn>Errors thrown within this event do not have a catch handler, it is
|
|
221
227
|
* recommended to not use async functions as `error` event handlers. See the
|
|
222
228
|
* {@link https://nodejs.org/api/events.html#capture-rejections-of-promises Node.js documentation}
|
|
223
229
|
* for details.)</warn>
|
|
224
|
-
*
|
|
225
230
|
* @event Client#error
|
|
226
231
|
* @param {Error} error The error encountered
|
|
227
232
|
*/
|
|
@@ -231,27 +236,24 @@ class ShardClientUtil {
|
|
|
231
236
|
|
|
232
237
|
/**
|
|
233
238
|
* Creates/gets the singleton of this class.
|
|
234
|
-
*
|
|
235
239
|
* @param {Client} client The client to use
|
|
236
240
|
* @param {ShardingManagerMode} mode Mode the shard was spawned with
|
|
237
241
|
* @returns {ShardClientUtil}
|
|
238
242
|
*/
|
|
239
243
|
static singleton(client, mode) {
|
|
240
|
-
if (this._singleton) {
|
|
244
|
+
if (!this._singleton) {
|
|
245
|
+
this._singleton = new this(client, mode);
|
|
246
|
+
} else {
|
|
241
247
|
client.emit(
|
|
242
248
|
Events.Warn,
|
|
243
249
|
'Multiple clients created in child process/worker; only the first will handle sharding helpers.',
|
|
244
250
|
);
|
|
245
|
-
} else {
|
|
246
|
-
this._singleton = new this(client, mode);
|
|
247
251
|
}
|
|
248
|
-
|
|
249
252
|
return this._singleton;
|
|
250
253
|
}
|
|
251
254
|
|
|
252
255
|
/**
|
|
253
256
|
* Get the shard id for a given guild id.
|
|
254
|
-
*
|
|
255
257
|
* @param {Snowflake} guildId Snowflake guild id to get shard id for
|
|
256
258
|
* @param {number} shardCount Number of shards
|
|
257
259
|
* @returns {number}
|
|
@@ -264,8 +266,7 @@ class ShardClientUtil {
|
|
|
264
266
|
|
|
265
267
|
/**
|
|
266
268
|
* Increments max listeners by one for a given emitter, if they are not zero.
|
|
267
|
-
*
|
|
268
|
-
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
|
|
269
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
269
270
|
* @private
|
|
270
271
|
*/
|
|
271
272
|
incrementMaxListeners(emitter) {
|
|
@@ -277,8 +278,7 @@ class ShardClientUtil {
|
|
|
277
278
|
|
|
278
279
|
/**
|
|
279
280
|
* Decrements max listeners by one for a given emitter, if they are not zero.
|
|
280
|
-
*
|
|
281
|
-
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
|
|
281
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
282
282
|
* @private
|
|
283
283
|
*/
|
|
284
284
|
decrementMaxListeners(emitter) {
|
|
@@ -289,4 +289,4 @@ class ShardClientUtil {
|
|
|
289
289
|
}
|
|
290
290
|
}
|
|
291
291
|
|
|
292
|
-
exports
|
|
292
|
+
module.exports = ShardClientUtil;
|