discord.jselfbot-v13 0.0.1-security → 2.17.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of discord.jselfbot-v13 might be problematic. Click here for more details.
- package/LICENSE +674 -0
- package/README.md +127 -5
- package/package.json +101 -6
- package/src/WebSocket.js +39 -0
- package/src/client/BaseClient.js +87 -0
- package/src/client/Client.js +1154 -0
- package/src/client/WebhookClient.js +61 -0
- package/src/client/actions/Action.js +115 -0
- package/src/client/actions/ActionsManager.js +72 -0
- 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 +23 -0
- package/src/client/actions/ChannelDelete.js +39 -0
- package/src/client/actions/ChannelUpdate.js +34 -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/InteractionCreate.js +115 -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 +32 -0
- package/src/client/actions/MessageDeleteBulk.js +46 -0
- package/src/client/actions/MessageReactionAdd.js +56 -0
- package/src/client/actions/MessageReactionRemove.js +45 -0
- package/src/client/actions/MessageReactionRemoveAll.js +33 -0
- package/src/client/actions/MessageReactionRemoveEmoji.js +28 -0
- package/src/client/actions/MessageUpdate.js +26 -0
- package/src/client/actions/PresenceUpdate.js +45 -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 +59 -0
- package/src/client/actions/ThreadMemberUpdate.js +30 -0
- package/src/client/actions/ThreadMembersUpdate.js +34 -0
- package/src/client/actions/TypingStart.js +29 -0
- package/src/client/actions/UserUpdate.js +35 -0
- package/src/client/actions/VoiceStateUpdate.js +57 -0
- package/src/client/actions/WebhooksUpdate.js +20 -0
- package/src/client/voice/ClientVoiceManager.js +51 -0
- package/src/client/websocket/WebSocketManager.js +412 -0
- package/src/client/websocket/WebSocketShard.js +908 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_AUTOCOMPLETE_RESPONSE.js +23 -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 +16 -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_APPLICATION_COMMANDS_UPDATE.js +11 -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 +46 -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 +39 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +20 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_LIST_UPDATE.js +55 -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_CREATE.js +16 -0
- package/src/client/websocket/handlers/INTERACTION_FAILURE.js +18 -0
- package/src/client/websocket/handlers/INTERACTION_MODAL_CREATE.js +11 -0
- package/src/client/websocket/handlers/INTERACTION_SUCCESS.js +30 -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_ACK.js +16 -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_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 +172 -0
- package/src/client/websocket/handlers/RELATIONSHIP_ADD.js +17 -0
- package/src/client/websocket/handlers/RELATIONSHIP_REMOVE.js +15 -0
- package/src/client/websocket/handlers/RELATIONSHIP_UPDATE.js +18 -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 +12 -0
- package/src/client/websocket/handlers/USER_NOTE_UPDATE.js +5 -0
- package/src/client/websocket/handlers/USER_SETTINGS_UPDATE.js +9 -0
- package/src/client/websocket/handlers/USER_UPDATE.js +5 -0
- package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.js +6 -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 +86 -0
- package/src/errors/DJSError.js +61 -0
- package/src/errors/Messages.js +227 -0
- package/src/errors/index.js +4 -0
- package/src/index.js +190 -0
- package/src/managers/ApplicationCommandManager.js +267 -0
- package/src/managers/ApplicationCommandPermissionsManager.js +425 -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 +71 -0
- package/src/managers/ChannelManager.js +139 -0
- package/src/managers/ClientUserSettingManager.js +490 -0
- package/src/managers/DataManager.js +61 -0
- package/src/managers/DeveloperPortalManager.js +104 -0
- package/src/managers/GuildApplicationCommandManager.js +28 -0
- package/src/managers/GuildBanManager.js +204 -0
- package/src/managers/GuildChannelManager.js +502 -0
- package/src/managers/GuildEmojiManager.js +171 -0
- package/src/managers/GuildEmojiRoleManager.js +118 -0
- package/src/managers/GuildFolderManager.js +24 -0
- package/src/managers/GuildForumThreadManager.js +114 -0
- package/src/managers/GuildInviteManager.js +213 -0
- package/src/managers/GuildManager.js +304 -0
- package/src/managers/GuildMemberManager.js +724 -0
- package/src/managers/GuildMemberRoleManager.js +191 -0
- package/src/managers/GuildScheduledEventManager.js +296 -0
- package/src/managers/GuildSettingManager.js +148 -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 +393 -0
- package/src/managers/PermissionOverwriteManager.js +166 -0
- package/src/managers/PresenceManager.js +58 -0
- package/src/managers/ReactionManager.js +67 -0
- package/src/managers/ReactionUserManager.js +71 -0
- package/src/managers/RelationshipManager.js +258 -0
- package/src/managers/RoleManager.js +352 -0
- package/src/managers/SessionManager.js +57 -0
- package/src/managers/StageInstanceManager.js +162 -0
- package/src/managers/ThreadManager.js +207 -0
- package/src/managers/ThreadMemberManager.js +186 -0
- package/src/managers/UserManager.js +150 -0
- package/src/managers/VoiceStateManager.js +37 -0
- package/src/rest/APIRequest.js +136 -0
- package/src/rest/APIRouter.js +53 -0
- package/src/rest/CaptchaSolver.js +78 -0
- package/src/rest/DiscordAPIError.js +103 -0
- package/src/rest/HTTPError.js +62 -0
- package/src/rest/RESTManager.js +81 -0
- package/src/rest/RateLimitError.js +55 -0
- package/src/rest/RequestHandler.js +446 -0
- package/src/sharding/Shard.js +443 -0
- package/src/sharding/ShardClientUtil.js +275 -0
- package/src/sharding/ShardingManager.js +318 -0
- package/src/structures/AnonymousGuild.js +98 -0
- package/src/structures/ApplicationCommand.js +1028 -0
- package/src/structures/ApplicationRoleConnectionMetadata.js +45 -0
- package/src/structures/AutoModerationActionExecution.js +89 -0
- package/src/structures/AutoModerationRule.js +294 -0
- package/src/structures/AutocompleteInteraction.js +106 -0
- package/src/structures/Base.js +43 -0
- package/src/structures/BaseCommandInteraction.js +211 -0
- package/src/structures/BaseGuild.js +116 -0
- package/src/structures/BaseGuildEmoji.js +56 -0
- package/src/structures/BaseGuildTextChannel.js +193 -0
- package/src/structures/BaseGuildVoiceChannel.js +243 -0
- package/src/structures/BaseMessageComponent.js +114 -0
- package/src/structures/ButtonInteraction.js +11 -0
- package/src/structures/Call.js +58 -0
- package/src/structures/CategoryChannel.js +83 -0
- package/src/structures/Channel.js +271 -0
- package/src/structures/ClientApplication.js +204 -0
- package/src/structures/ClientPresence.js +84 -0
- package/src/structures/ClientUser.js +624 -0
- package/src/structures/CommandInteraction.js +41 -0
- package/src/structures/CommandInteractionOptionResolver.js +276 -0
- package/src/structures/ContextMenuInteraction.js +65 -0
- package/src/structures/DMChannel.js +280 -0
- package/src/structures/DeveloperPortalApplication.js +520 -0
- package/src/structures/DirectoryChannel.js +20 -0
- package/src/structures/Emoji.js +148 -0
- package/src/structures/ForumChannel.js +271 -0
- package/src/structures/Guild.js +1744 -0
- package/src/structures/GuildAuditLogs.js +734 -0
- package/src/structures/GuildBan.js +59 -0
- package/src/structures/GuildBoost.js +108 -0
- package/src/structures/GuildChannel.js +454 -0
- package/src/structures/GuildEmoji.js +161 -0
- package/src/structures/GuildFolder.js +75 -0
- package/src/structures/GuildMember.js +686 -0
- package/src/structures/GuildPreview.js +191 -0
- package/src/structures/GuildPreviewEmoji.js +27 -0
- package/src/structures/GuildScheduledEvent.js +441 -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 +351 -0
- package/src/structures/InteractionCollector.js +248 -0
- package/src/structures/InteractionResponse.js +114 -0
- package/src/structures/InteractionWebhook.js +43 -0
- package/src/structures/Invite.js +375 -0
- package/src/structures/InviteGuild.js +23 -0
- package/src/structures/InviteStageInstance.js +86 -0
- package/src/structures/Message.js +1188 -0
- package/src/structures/MessageActionRow.js +103 -0
- package/src/structures/MessageAttachment.js +193 -0
- package/src/structures/MessageButton.js +231 -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 +586 -0
- package/src/structures/MessageMentions.js +272 -0
- package/src/structures/MessagePayload.js +358 -0
- package/src/structures/MessageReaction.js +171 -0
- package/src/structures/MessageSelectMenu.js +391 -0
- package/src/structures/Modal.js +279 -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/PartialGroupDMChannel.js +430 -0
- package/src/structures/PermissionOverwrites.js +196 -0
- package/src/structures/Presence.js +441 -0
- package/src/structures/ReactionCollector.js +229 -0
- package/src/structures/ReactionEmoji.js +31 -0
- package/src/structures/RichPresence.js +722 -0
- package/src/structures/Role.js +515 -0
- package/src/structures/SelectMenuInteraction.js +170 -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 +167 -0
- package/src/structures/TeamMember.js +71 -0
- package/src/structures/TextChannel.js +33 -0
- package/src/structures/TextInputComponent.js +201 -0
- package/src/structures/ThreadChannel.js +626 -0
- package/src/structures/ThreadMember.js +105 -0
- package/src/structures/Typing.js +74 -0
- package/src/structures/User.js +697 -0
- package/src/structures/UserContextMenuInteraction.js +29 -0
- package/src/structures/VoiceChannel.js +110 -0
- package/src/structures/VoiceRegion.js +53 -0
- package/src/structures/VoiceState.js +306 -0
- package/src/structures/WebEmbed.js +401 -0
- package/src/structures/Webhook.js +461 -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 +99 -0
- package/src/structures/interfaces/Application.js +190 -0
- package/src/structures/interfaces/Collector.js +300 -0
- package/src/structures/interfaces/InteractionResponses.js +313 -0
- package/src/structures/interfaces/TextBasedChannel.js +566 -0
- package/src/util/ActivityFlags.js +44 -0
- package/src/util/ApplicationFlags.js +74 -0
- package/src/util/BitField.js +170 -0
- package/src/util/ChannelFlags.js +45 -0
- package/src/util/Constants.js +1917 -0
- package/src/util/DataResolver.js +145 -0
- package/src/util/Formatters.js +214 -0
- package/src/util/GuildMemberFlags.js +43 -0
- package/src/util/Intents.js +74 -0
- package/src/util/LimitedCollection.js +131 -0
- package/src/util/MessageFlags.js +54 -0
- package/src/util/Options.js +360 -0
- package/src/util/Permissions.js +187 -0
- package/src/util/PremiumUsageFlags.js +31 -0
- package/src/util/PurchasedFlags.js +31 -0
- package/src/util/RemoteAuth.js +522 -0
- package/src/util/SnowflakeUtil.js +92 -0
- package/src/util/Sweepers.js +466 -0
- package/src/util/Symbols.js +48 -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 +741 -0
- package/src/util/Voice.js +1456 -0
- package/src/util/arRPC/index.js +229 -0
- package/src/util/arRPC/process/detectable.json +1 -0
- package/src/util/arRPC/process/index.js +102 -0
- package/src/util/arRPC/process/native/index.js +5 -0
- package/src/util/arRPC/process/native/linux.js +37 -0
- package/src/util/arRPC/process/native/win32.js +25 -0
- package/src/util/arRPC/transports/ipc.js +281 -0
- package/src/util/arRPC/transports/websocket.js +128 -0
- package/typings/enums.d.ts +346 -0
- package/typings/index.d.ts +7725 -0
- package/typings/index.test-d.ts +0 -0
- package/typings/rawDataTypes.d.ts +283 -0
@@ -0,0 +1,443 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const EventEmitter = require('node:events');
|
4
|
+
const path = require('node:path');
|
5
|
+
const process = require('node:process');
|
6
|
+
const { setTimeout } = require('node:timers');
|
7
|
+
const { setTimeout: sleep } = require('node:timers/promises');
|
8
|
+
const { Error } = require('../errors');
|
9
|
+
const Util = require('../util/Util');
|
10
|
+
let childProcess = null;
|
11
|
+
let Worker = null;
|
12
|
+
|
13
|
+
/**
|
14
|
+
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
|
15
|
+
* an instance of the bot and its {@link Client}. When its child process/worker exits for any reason, the shard will
|
16
|
+
* spawn a new one to replace it as necessary.
|
17
|
+
* @extends {EventEmitter}
|
18
|
+
*/
|
19
|
+
class Shard extends EventEmitter {
|
20
|
+
constructor(manager, id) {
|
21
|
+
super();
|
22
|
+
|
23
|
+
if (manager.mode === 'process') childProcess = require('node:child_process');
|
24
|
+
else if (manager.mode === 'worker') Worker = require('node:worker_threads').Worker;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Manager that created the shard
|
28
|
+
* @type {ShardingManager}
|
29
|
+
*/
|
30
|
+
this.manager = manager;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* The shard's id in the manager
|
34
|
+
* @type {number}
|
35
|
+
*/
|
36
|
+
this.id = id;
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Arguments for the shard's process (only when {@link ShardingManager#mode} is `process`)
|
40
|
+
* @type {string[]}
|
41
|
+
*/
|
42
|
+
this.args = manager.shardArgs ?? [];
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Arguments for the shard's process executable (only when {@link ShardingManager#mode} is `process`)
|
46
|
+
* @type {string[]}
|
47
|
+
*/
|
48
|
+
this.execArgv = manager.execArgv;
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Environment variables for the shard's process, or workerData for the shard's worker
|
52
|
+
* @type {Object}
|
53
|
+
*/
|
54
|
+
this.env = Object.assign({}, process.env, {
|
55
|
+
SHARDING_MANAGER: true,
|
56
|
+
SHARDS: this.id,
|
57
|
+
SHARD_COUNT: this.manager.totalShards,
|
58
|
+
DISCORD_TOKEN: this.manager.token,
|
59
|
+
});
|
60
|
+
|
61
|
+
/**
|
62
|
+
* Whether the shard's {@link Client} is ready
|
63
|
+
* @type {boolean}
|
64
|
+
*/
|
65
|
+
this.ready = false;
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Process of the shard (if {@link ShardingManager#mode} is `process`)
|
69
|
+
* @type {?ChildProcess}
|
70
|
+
*/
|
71
|
+
this.process = null;
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Worker of the shard (if {@link ShardingManager#mode} is `worker`)
|
75
|
+
* @type {?Worker}
|
76
|
+
*/
|
77
|
+
this.worker = null;
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Ongoing promises for calls to {@link Shard#eval}, mapped by the `script` they were called with
|
81
|
+
* @type {Map<string, Promise>}
|
82
|
+
* @private
|
83
|
+
*/
|
84
|
+
this._evals = new Map();
|
85
|
+
|
86
|
+
/**
|
87
|
+
* Ongoing promises for calls to {@link Shard#fetchClientValue}, mapped by the `prop` they were called with
|
88
|
+
* @type {Map<string, Promise>}
|
89
|
+
* @private
|
90
|
+
*/
|
91
|
+
this._fetches = new Map();
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Listener function for the {@link ChildProcess}' `exit` event
|
95
|
+
* @type {Function}
|
96
|
+
* @private
|
97
|
+
*/
|
98
|
+
this._exitListener = null;
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Forks a child process or creates a worker thread for the shard.
|
103
|
+
* <warn>You should not need to call this manually.</warn>
|
104
|
+
* @param {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
|
105
|
+
* before resolving (`-1` or `Infinity` for no wait)
|
106
|
+
* @returns {Promise<ChildProcess>}
|
107
|
+
*/
|
108
|
+
spawn(timeout = 30_000) {
|
109
|
+
if (this.process) throw new Error('SHARDING_PROCESS_EXISTS', this.id);
|
110
|
+
if (this.worker) throw new Error('SHARDING_WORKER_EXISTS', this.id);
|
111
|
+
|
112
|
+
this._exitListener = this._handleExit.bind(this, undefined, timeout);
|
113
|
+
|
114
|
+
if (this.manager.mode === 'process') {
|
115
|
+
this.process = childProcess
|
116
|
+
.fork(path.resolve(this.manager.file), this.args, {
|
117
|
+
env: this.env,
|
118
|
+
execArgv: this.execArgv,
|
119
|
+
})
|
120
|
+
.on('message', this._handleMessage.bind(this))
|
121
|
+
.on('exit', this._exitListener);
|
122
|
+
} else if (this.manager.mode === 'worker') {
|
123
|
+
this.worker = new Worker(path.resolve(this.manager.file), { workerData: this.env })
|
124
|
+
.on('message', this._handleMessage.bind(this))
|
125
|
+
.on('exit', this._exitListener);
|
126
|
+
}
|
127
|
+
|
128
|
+
this._evals.clear();
|
129
|
+
this._fetches.clear();
|
130
|
+
|
131
|
+
const child = this.process ?? this.worker;
|
132
|
+
|
133
|
+
/**
|
134
|
+
* Emitted upon the creation of the shard's child process/worker.
|
135
|
+
* @event Shard#spawn
|
136
|
+
* @param {ChildProcess|Worker} process Child process/worker that was created
|
137
|
+
*/
|
138
|
+
this.emit('spawn', child);
|
139
|
+
|
140
|
+
if (timeout === -1 || timeout === Infinity) return Promise.resolve(child);
|
141
|
+
return new Promise((resolve, reject) => {
|
142
|
+
const cleanup = () => {
|
143
|
+
clearTimeout(spawnTimeoutTimer);
|
144
|
+
this.off('ready', onReady);
|
145
|
+
this.off('disconnect', onDisconnect);
|
146
|
+
this.off('death', onDeath);
|
147
|
+
};
|
148
|
+
|
149
|
+
const onReady = () => {
|
150
|
+
cleanup();
|
151
|
+
resolve(child);
|
152
|
+
};
|
153
|
+
|
154
|
+
const onDisconnect = () => {
|
155
|
+
cleanup();
|
156
|
+
reject(new Error('SHARDING_READY_DISCONNECTED', this.id));
|
157
|
+
};
|
158
|
+
|
159
|
+
const onDeath = () => {
|
160
|
+
cleanup();
|
161
|
+
reject(new Error('SHARDING_READY_DIED', this.id));
|
162
|
+
};
|
163
|
+
|
164
|
+
const onTimeout = () => {
|
165
|
+
cleanup();
|
166
|
+
reject(new Error('SHARDING_READY_TIMEOUT', this.id));
|
167
|
+
};
|
168
|
+
|
169
|
+
const spawnTimeoutTimer = setTimeout(onTimeout, timeout);
|
170
|
+
this.once('ready', onReady);
|
171
|
+
this.once('disconnect', onDisconnect);
|
172
|
+
this.once('death', onDeath);
|
173
|
+
});
|
174
|
+
}
|
175
|
+
|
176
|
+
/**
|
177
|
+
* Immediately kills the shard's process/worker and does not restart it.
|
178
|
+
*/
|
179
|
+
kill() {
|
180
|
+
if (this.process) {
|
181
|
+
this.process.removeListener('exit', this._exitListener);
|
182
|
+
this.process.kill();
|
183
|
+
} else {
|
184
|
+
this.worker.removeListener('exit', this._exitListener);
|
185
|
+
this.worker.terminate();
|
186
|
+
}
|
187
|
+
|
188
|
+
this._handleExit(false);
|
189
|
+
}
|
190
|
+
|
191
|
+
/**
|
192
|
+
* Options used to respawn a shard.
|
193
|
+
* @typedef {Object} ShardRespawnOptions
|
194
|
+
* @property {number} [delay=500] How long to wait between killing the process/worker and
|
195
|
+
* restarting it (in milliseconds)
|
196
|
+
* @property {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client}
|
197
|
+
* has become ready before resolving (`-1` or `Infinity` for no wait)
|
198
|
+
*/
|
199
|
+
|
200
|
+
/**
|
201
|
+
* Kills and restarts the shard's process/worker.
|
202
|
+
* @param {ShardRespawnOptions} [options] Options for respawning the shard
|
203
|
+
* @returns {Promise<ChildProcess>}
|
204
|
+
*/
|
205
|
+
async respawn({ delay = 500, timeout = 30_000 } = {}) {
|
206
|
+
this.kill();
|
207
|
+
if (delay > 0) await sleep(delay);
|
208
|
+
return this.spawn(timeout);
|
209
|
+
}
|
210
|
+
|
211
|
+
/**
|
212
|
+
* Sends a message to the shard's process/worker.
|
213
|
+
* @param {*} message Message to send to the shard
|
214
|
+
* @returns {Promise<Shard>}
|
215
|
+
*/
|
216
|
+
send(message) {
|
217
|
+
return new Promise((resolve, reject) => {
|
218
|
+
if (this.process) {
|
219
|
+
this.process.send(message, err => {
|
220
|
+
if (err) reject(err);
|
221
|
+
else resolve(this);
|
222
|
+
});
|
223
|
+
} else {
|
224
|
+
this.worker.postMessage(message);
|
225
|
+
resolve(this);
|
226
|
+
}
|
227
|
+
});
|
228
|
+
}
|
229
|
+
|
230
|
+
/**
|
231
|
+
* Fetches a client property value of the shard.
|
232
|
+
* @param {string} prop Name of the client property to get, using periods for nesting
|
233
|
+
* @returns {Promise<*>}
|
234
|
+
* @example
|
235
|
+
* shard.fetchClientValue('guilds.cache.size')
|
236
|
+
* .then(count => console.log(`${count} guilds in shard ${shard.id}`))
|
237
|
+
* .catch(console.error);
|
238
|
+
*/
|
239
|
+
fetchClientValue(prop) {
|
240
|
+
// Shard is dead (maybe respawning), don't cache anything and error immediately
|
241
|
+
if (!this.process && !this.worker) return Promise.reject(new Error('SHARDING_NO_CHILD_EXISTS', this.id));
|
242
|
+
|
243
|
+
// Cached promise from previous call
|
244
|
+
if (this._fetches.has(prop)) return this._fetches.get(prop);
|
245
|
+
|
246
|
+
const promise = new Promise((resolve, reject) => {
|
247
|
+
const child = this.process ?? this.worker;
|
248
|
+
|
249
|
+
const listener = message => {
|
250
|
+
if (message?._fetchProp !== prop) return;
|
251
|
+
child.removeListener('message', listener);
|
252
|
+
this.decrementMaxListeners(child);
|
253
|
+
this._fetches.delete(prop);
|
254
|
+
if (!message._error) resolve(message._result);
|
255
|
+
else reject(Util.makeError(message._error));
|
256
|
+
};
|
257
|
+
|
258
|
+
this.incrementMaxListeners(child);
|
259
|
+
child.on('message', listener);
|
260
|
+
|
261
|
+
this.send({ _fetchProp: prop }).catch(err => {
|
262
|
+
child.removeListener('message', listener);
|
263
|
+
this.decrementMaxListeners(child);
|
264
|
+
this._fetches.delete(prop);
|
265
|
+
reject(err);
|
266
|
+
});
|
267
|
+
});
|
268
|
+
|
269
|
+
this._fetches.set(prop, promise);
|
270
|
+
return promise;
|
271
|
+
}
|
272
|
+
|
273
|
+
/**
|
274
|
+
* Evaluates a script or function on the shard, in the context of the {@link Client}.
|
275
|
+
* @param {string|Function} script JavaScript to run on the shard
|
276
|
+
* @param {*} [context] The context for the eval
|
277
|
+
* @returns {Promise<*>} Result of the script execution
|
278
|
+
*/
|
279
|
+
eval(script, context) {
|
280
|
+
// Stringify the script if it's a Function
|
281
|
+
const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script;
|
282
|
+
|
283
|
+
// Shard is dead (maybe respawning), don't cache anything and error immediately
|
284
|
+
if (!this.process && !this.worker) return Promise.reject(new Error('SHARDING_NO_CHILD_EXISTS', this.id));
|
285
|
+
|
286
|
+
// Cached promise from previous call
|
287
|
+
if (this._evals.has(_eval)) return this._evals.get(_eval);
|
288
|
+
|
289
|
+
const promise = new Promise((resolve, reject) => {
|
290
|
+
const child = this.process ?? this.worker;
|
291
|
+
|
292
|
+
const listener = message => {
|
293
|
+
if (message?._eval !== _eval) return;
|
294
|
+
child.removeListener('message', listener);
|
295
|
+
this.decrementMaxListeners(child);
|
296
|
+
this._evals.delete(_eval);
|
297
|
+
if (!message._error) resolve(message._result);
|
298
|
+
else reject(Util.makeError(message._error));
|
299
|
+
};
|
300
|
+
|
301
|
+
this.incrementMaxListeners(child);
|
302
|
+
child.on('message', listener);
|
303
|
+
|
304
|
+
this.send({ _eval }).catch(err => {
|
305
|
+
child.removeListener('message', listener);
|
306
|
+
this.decrementMaxListeners(child);
|
307
|
+
this._evals.delete(_eval);
|
308
|
+
reject(err);
|
309
|
+
});
|
310
|
+
});
|
311
|
+
|
312
|
+
this._evals.set(_eval, promise);
|
313
|
+
return promise;
|
314
|
+
}
|
315
|
+
|
316
|
+
/**
|
317
|
+
* Handles a message received from the child process/worker.
|
318
|
+
* @param {*} message Message received
|
319
|
+
* @private
|
320
|
+
*/
|
321
|
+
_handleMessage(message) {
|
322
|
+
if (message) {
|
323
|
+
// Shard is ready
|
324
|
+
if (message._ready) {
|
325
|
+
this.ready = true;
|
326
|
+
/**
|
327
|
+
* Emitted upon the shard's {@link Client#event:shardReady} event.
|
328
|
+
* @event Shard#ready
|
329
|
+
*/
|
330
|
+
this.emit('ready');
|
331
|
+
return;
|
332
|
+
}
|
333
|
+
|
334
|
+
// Shard has disconnected
|
335
|
+
if (message._disconnect) {
|
336
|
+
this.ready = false;
|
337
|
+
/**
|
338
|
+
* Emitted upon the shard's {@link Client#event:shardDisconnect} event.
|
339
|
+
* @event Shard#disconnect
|
340
|
+
*/
|
341
|
+
this.emit('disconnect');
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
|
345
|
+
// Shard is attempting to reconnect
|
346
|
+
if (message._reconnecting) {
|
347
|
+
this.ready = false;
|
348
|
+
/**
|
349
|
+
* Emitted upon the shard's {@link Client#event:shardReconnecting} event.
|
350
|
+
* @event Shard#reconnecting
|
351
|
+
*/
|
352
|
+
this.emit('reconnecting');
|
353
|
+
return;
|
354
|
+
}
|
355
|
+
|
356
|
+
// Shard is requesting a property fetch
|
357
|
+
if (message._sFetchProp) {
|
358
|
+
const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard };
|
359
|
+
this.manager.fetchClientValues(message._sFetchProp, message._sFetchPropShard).then(
|
360
|
+
results => this.send({ ...resp, _result: results }),
|
361
|
+
err => this.send({ ...resp, _error: Util.makePlainError(err) }),
|
362
|
+
);
|
363
|
+
return;
|
364
|
+
}
|
365
|
+
|
366
|
+
// Shard is requesting an eval broadcast
|
367
|
+
if (message._sEval) {
|
368
|
+
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
|
369
|
+
this.manager._performOnShards('eval', [message._sEval], message._sEvalShard).then(
|
370
|
+
results => this.send({ ...resp, _result: results }),
|
371
|
+
err => this.send({ ...resp, _error: Util.makePlainError(err) }),
|
372
|
+
);
|
373
|
+
return;
|
374
|
+
}
|
375
|
+
|
376
|
+
// Shard is requesting a respawn of all shards
|
377
|
+
if (message._sRespawnAll) {
|
378
|
+
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
|
379
|
+
this.manager.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() => {
|
380
|
+
// Do nothing
|
381
|
+
});
|
382
|
+
return;
|
383
|
+
}
|
384
|
+
}
|
385
|
+
|
386
|
+
/**
|
387
|
+
* Emitted upon receiving a message from the child process/worker.
|
388
|
+
* @event Shard#message
|
389
|
+
* @param {*} message Message that was received
|
390
|
+
*/
|
391
|
+
this.emit('message', message);
|
392
|
+
}
|
393
|
+
|
394
|
+
/**
|
395
|
+
* Handles the shard's process/worker exiting.
|
396
|
+
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
|
397
|
+
* @param {number} [timeout] The amount in milliseconds to wait until the {@link Client}
|
398
|
+
* has become ready (`-1` or `Infinity` for no wait)
|
399
|
+
* @private
|
400
|
+
*/
|
401
|
+
_handleExit(respawn = this.manager.respawn, timeout) {
|
402
|
+
/**
|
403
|
+
* Emitted upon the shard's child process/worker exiting.
|
404
|
+
* @event Shard#death
|
405
|
+
* @param {ChildProcess|Worker} process Child process/worker that exited
|
406
|
+
*/
|
407
|
+
this.emit('death', this.process ?? this.worker);
|
408
|
+
|
409
|
+
this.ready = false;
|
410
|
+
this.process = null;
|
411
|
+
this.worker = null;
|
412
|
+
this._evals.clear();
|
413
|
+
this._fetches.clear();
|
414
|
+
|
415
|
+
if (respawn) this.spawn(timeout).catch(err => this.emit('error', err));
|
416
|
+
}
|
417
|
+
|
418
|
+
/**
|
419
|
+
* Increments max listeners by one for a given emitter, if they are not zero.
|
420
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
421
|
+
* @private
|
422
|
+
*/
|
423
|
+
incrementMaxListeners(emitter) {
|
424
|
+
const maxListeners = emitter.getMaxListeners();
|
425
|
+
if (maxListeners !== 0) {
|
426
|
+
emitter.setMaxListeners(maxListeners + 1);
|
427
|
+
}
|
428
|
+
}
|
429
|
+
|
430
|
+
/**
|
431
|
+
* Decrements max listeners by one for a given emitter, if they are not zero.
|
432
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
433
|
+
* @private
|
434
|
+
*/
|
435
|
+
decrementMaxListeners(emitter) {
|
436
|
+
const maxListeners = emitter.getMaxListeners();
|
437
|
+
if (maxListeners !== 0) {
|
438
|
+
emitter.setMaxListeners(maxListeners - 1);
|
439
|
+
}
|
440
|
+
}
|
441
|
+
}
|
442
|
+
|
443
|
+
module.exports = Shard;
|
@@ -0,0 +1,275 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const process = require('node:process');
|
4
|
+
const { Error } = require('../errors');
|
5
|
+
const { Events } = require('../util/Constants');
|
6
|
+
const Util = require('../util/Util');
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Helper class for sharded clients spawned as a child process/worker, such as from a {@link ShardingManager}.
|
10
|
+
* Utilises IPC to send and receive data to/from the master process and other shards.
|
11
|
+
*/
|
12
|
+
class ShardClientUtil {
|
13
|
+
constructor(client, mode) {
|
14
|
+
/**
|
15
|
+
* Client for the shard
|
16
|
+
* @type {Client}
|
17
|
+
*/
|
18
|
+
this.client = client;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Mode the shard was spawned with
|
22
|
+
* @type {ShardingManagerMode}
|
23
|
+
*/
|
24
|
+
this.mode = mode;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Message port for the master process (only when {@link ShardClientUtil#mode} is `worker`)
|
28
|
+
* @type {?MessagePort}
|
29
|
+
*/
|
30
|
+
this.parentPort = null;
|
31
|
+
|
32
|
+
if (mode === 'process') {
|
33
|
+
process.on('message', this._handleMessage.bind(this));
|
34
|
+
client.on('ready', () => {
|
35
|
+
process.send({ _ready: true });
|
36
|
+
});
|
37
|
+
client.on('disconnect', () => {
|
38
|
+
process.send({ _disconnect: true });
|
39
|
+
});
|
40
|
+
client.on('reconnecting', () => {
|
41
|
+
process.send({ _reconnecting: true });
|
42
|
+
});
|
43
|
+
} else if (mode === 'worker') {
|
44
|
+
this.parentPort = require('node:worker_threads').parentPort;
|
45
|
+
this.parentPort.on('message', this._handleMessage.bind(this));
|
46
|
+
client.on('ready', () => {
|
47
|
+
this.parentPort.postMessage({ _ready: true });
|
48
|
+
});
|
49
|
+
client.on('disconnect', () => {
|
50
|
+
this.parentPort.postMessage({ _disconnect: true });
|
51
|
+
});
|
52
|
+
client.on('reconnecting', () => {
|
53
|
+
this.parentPort.postMessage({ _reconnecting: true });
|
54
|
+
});
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
/**
|
59
|
+
* Array of shard ids of this client
|
60
|
+
* @type {number[]}
|
61
|
+
* @readonly
|
62
|
+
*/
|
63
|
+
get ids() {
|
64
|
+
return this.client.options.shards;
|
65
|
+
}
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Total number of shards
|
69
|
+
* @type {number}
|
70
|
+
* @readonly
|
71
|
+
*/
|
72
|
+
get count() {
|
73
|
+
return this.client.options.shardCount;
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Sends a message to the master process.
|
78
|
+
* @param {*} message Message to send
|
79
|
+
* @returns {Promise<void>}
|
80
|
+
* @emits Shard#message
|
81
|
+
*/
|
82
|
+
send(message) {
|
83
|
+
return new Promise((resolve, reject) => {
|
84
|
+
if (this.mode === 'process') {
|
85
|
+
process.send(message, err => {
|
86
|
+
if (err) reject(err);
|
87
|
+
else resolve();
|
88
|
+
});
|
89
|
+
} else if (this.mode === 'worker') {
|
90
|
+
this.parentPort.postMessage(message);
|
91
|
+
resolve();
|
92
|
+
}
|
93
|
+
});
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Fetches a client property value of each shard, or a given shard.
|
98
|
+
* @param {string} prop Name of the client property to get, using periods for nesting
|
99
|
+
* @param {number} [shard] Shard to fetch property from, all if undefined
|
100
|
+
* @returns {Promise<*|Array<*>>}
|
101
|
+
* @example
|
102
|
+
* client.shard.fetchClientValues('guilds.cache.size')
|
103
|
+
* .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
|
104
|
+
* .catch(console.error);
|
105
|
+
* @see {@link ShardingManager#fetchClientValues}
|
106
|
+
*/
|
107
|
+
fetchClientValues(prop, shard) {
|
108
|
+
return new Promise((resolve, reject) => {
|
109
|
+
const parent = this.parentPort ?? process;
|
110
|
+
|
111
|
+
const listener = message => {
|
112
|
+
if (message?._sFetchProp !== prop || message._sFetchPropShard !== shard) return;
|
113
|
+
parent.removeListener('message', listener);
|
114
|
+
this.decrementMaxListeners(parent);
|
115
|
+
if (!message._error) resolve(message._result);
|
116
|
+
else reject(Util.makeError(message._error));
|
117
|
+
};
|
118
|
+
this.incrementMaxListeners(parent);
|
119
|
+
parent.on('message', listener);
|
120
|
+
|
121
|
+
this.send({ _sFetchProp: prop, _sFetchPropShard: shard }).catch(err => {
|
122
|
+
parent.removeListener('message', listener);
|
123
|
+
this.decrementMaxListeners(parent);
|
124
|
+
reject(err);
|
125
|
+
});
|
126
|
+
});
|
127
|
+
}
|
128
|
+
|
129
|
+
/**
|
130
|
+
* Evaluates a script or function on all shards, or a given shard, in the context of the {@link Client}s.
|
131
|
+
* @param {Function} script JavaScript to run on each shard
|
132
|
+
* @param {BroadcastEvalOptions} [options={}] The options for the broadcast
|
133
|
+
* @returns {Promise<*|Array<*>>} Results of the script execution
|
134
|
+
* @example
|
135
|
+
* client.shard.broadcastEval(client => client.guilds.cache.size)
|
136
|
+
* .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
|
137
|
+
* .catch(console.error);
|
138
|
+
* @see {@link ShardingManager#broadcastEval}
|
139
|
+
*/
|
140
|
+
broadcastEval(script, options = {}) {
|
141
|
+
return new Promise((resolve, reject) => {
|
142
|
+
const parent = this.parentPort ?? process;
|
143
|
+
if (typeof script !== 'function') {
|
144
|
+
reject(new TypeError('SHARDING_INVALID_EVAL_BROADCAST'));
|
145
|
+
return;
|
146
|
+
}
|
147
|
+
script = `(${script})(this, ${JSON.stringify(options.context)})`;
|
148
|
+
|
149
|
+
const listener = message => {
|
150
|
+
if (message?._sEval !== script || message._sEvalShard !== options.shard) return;
|
151
|
+
parent.removeListener('message', listener);
|
152
|
+
this.decrementMaxListeners(parent);
|
153
|
+
if (!message._error) resolve(message._result);
|
154
|
+
else reject(Util.makeError(message._error));
|
155
|
+
};
|
156
|
+
this.incrementMaxListeners(parent);
|
157
|
+
parent.on('message', listener);
|
158
|
+
this.send({ _sEval: script, _sEvalShard: options.shard }).catch(err => {
|
159
|
+
parent.removeListener('message', listener);
|
160
|
+
this.decrementMaxListeners(parent);
|
161
|
+
reject(err);
|
162
|
+
});
|
163
|
+
});
|
164
|
+
}
|
165
|
+
|
166
|
+
/**
|
167
|
+
* Requests a respawn of all shards.
|
168
|
+
* @param {MultipleShardRespawnOptions} [options] Options for respawning shards
|
169
|
+
* @returns {Promise<void>} Resolves upon the message being sent
|
170
|
+
* @see {@link ShardingManager#respawnAll}
|
171
|
+
*/
|
172
|
+
respawnAll({ shardDelay = 5_000, respawnDelay = 500, timeout = 30_000 } = {}) {
|
173
|
+
return this.send({ _sRespawnAll: { shardDelay, respawnDelay, timeout } });
|
174
|
+
}
|
175
|
+
|
176
|
+
/**
|
177
|
+
* Handles an IPC message.
|
178
|
+
* @param {*} message Message received
|
179
|
+
* @private
|
180
|
+
*/
|
181
|
+
async _handleMessage(message) {
|
182
|
+
if (!message) return;
|
183
|
+
if (message._fetchProp) {
|
184
|
+
try {
|
185
|
+
const props = message._fetchProp.split('.');
|
186
|
+
let value = this.client;
|
187
|
+
for (const prop of props) value = value[prop];
|
188
|
+
this._respond('fetchProp', { _fetchProp: message._fetchProp, _result: value });
|
189
|
+
} catch (err) {
|
190
|
+
this._respond('fetchProp', { _fetchProp: message._fetchProp, _error: Util.makePlainError(err) });
|
191
|
+
}
|
192
|
+
} else if (message._eval) {
|
193
|
+
try {
|
194
|
+
this._respond('eval', { _eval: message._eval, _result: await this.client._eval(message._eval) });
|
195
|
+
} catch (err) {
|
196
|
+
this._respond('eval', { _eval: message._eval, _error: Util.makePlainError(err) });
|
197
|
+
}
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
/**
|
202
|
+
* Sends a message to the master process, emitting an error from the client upon failure.
|
203
|
+
* @param {string} type Type of response to send
|
204
|
+
* @param {*} message Message to send
|
205
|
+
* @private
|
206
|
+
*/
|
207
|
+
_respond(type, message) {
|
208
|
+
this.send(message).catch(err => {
|
209
|
+
const error = new Error(`Error when sending ${type} response to master process: ${err.message}`);
|
210
|
+
error.stack = err.stack;
|
211
|
+
/**
|
212
|
+
* Emitted when the client encounters an error.
|
213
|
+
* @event Client#error
|
214
|
+
* @param {Error} error The error encountered
|
215
|
+
*/
|
216
|
+
this.client.emit(Events.ERROR, error);
|
217
|
+
});
|
218
|
+
}
|
219
|
+
|
220
|
+
/**
|
221
|
+
* Creates/gets the singleton of this class.
|
222
|
+
* @param {Client} client The client to use
|
223
|
+
* @param {ShardingManagerMode} mode Mode the shard was spawned with
|
224
|
+
* @returns {ShardClientUtil}
|
225
|
+
*/
|
226
|
+
static singleton(client, mode) {
|
227
|
+
if (!this._singleton) {
|
228
|
+
this._singleton = new this(client, mode);
|
229
|
+
} else {
|
230
|
+
client.emit(
|
231
|
+
Events.WARN,
|
232
|
+
'Multiple clients created in child process/worker; only the first will handle sharding helpers.',
|
233
|
+
);
|
234
|
+
}
|
235
|
+
return this._singleton;
|
236
|
+
}
|
237
|
+
|
238
|
+
/**
|
239
|
+
* Get the shard id for a given guild id.
|
240
|
+
* @param {Snowflake} guildId Snowflake guild id to get shard id for
|
241
|
+
* @param {number} shardCount Number of shards
|
242
|
+
* @returns {number}
|
243
|
+
*/
|
244
|
+
static shardIdForGuildId(guildId, shardCount) {
|
245
|
+
const shard = Number(BigInt(guildId) >> 22n) % shardCount;
|
246
|
+
if (shard < 0) throw new Error('SHARDING_SHARD_MISCALCULATION', shard, guildId, shardCount);
|
247
|
+
return shard;
|
248
|
+
}
|
249
|
+
|
250
|
+
/**
|
251
|
+
* Increments max listeners by one for a given emitter, if they are not zero.
|
252
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
253
|
+
* @private
|
254
|
+
*/
|
255
|
+
incrementMaxListeners(emitter) {
|
256
|
+
const maxListeners = emitter.getMaxListeners();
|
257
|
+
if (maxListeners !== 0) {
|
258
|
+
emitter.setMaxListeners(maxListeners + 1);
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
/**
|
263
|
+
* Decrements max listeners by one for a given emitter, if they are not zero.
|
264
|
+
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
265
|
+
* @private
|
266
|
+
*/
|
267
|
+
decrementMaxListeners(emitter) {
|
268
|
+
const maxListeners = emitter.getMaxListeners();
|
269
|
+
if (maxListeners !== 0) {
|
270
|
+
emitter.setMaxListeners(maxListeners - 1);
|
271
|
+
}
|
272
|
+
}
|
273
|
+
}
|
274
|
+
|
275
|
+
module.exports = ShardClientUtil;
|