@velliajs/discord 1.0.3 → 1.0.4
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/package.json +1 -1
- package/src/client/BaseClient.js +7 -28
- package/src/client/Client.js +116 -283
- package/src/client/WebhookClient.js +17 -59
- package/src/client/actions/Action.js +0 -10
- package/src/client/actions/ActionsManager.js +3 -3
- package/src/client/actions/ApplicationCommandPermissionsUpdate.js +2 -15
- package/src/client/actions/AutoModerationActionExecution.js +1 -6
- package/src/client/actions/AutoModerationRuleCreate.js +1 -6
- package/src/client/actions/AutoModerationRuleDelete.js +1 -6
- package/src/client/actions/AutoModerationRuleUpdate.js +1 -7
- package/src/client/actions/ChannelCreate.js +1 -5
- package/src/client/actions/ChannelDelete.js +1 -5
- package/src/client/actions/GuildAuditLogEntryCreate.js +1 -6
- package/src/client/actions/GuildBanAdd.js +1 -5
- package/src/client/actions/GuildBanRemove.js +1 -5
- package/src/client/actions/GuildDelete.js +6 -14
- package/src/client/actions/GuildEmojiCreate.js +1 -5
- package/src/client/actions/GuildEmojiDelete.js +1 -5
- package/src/client/actions/GuildEmojiUpdate.js +1 -6
- package/src/client/actions/GuildEmojisUpdate.js +4 -4
- package/src/client/actions/GuildIntegrationsUpdate.js +1 -5
- package/src/client/actions/GuildMemberRemove.js +1 -5
- package/src/client/actions/GuildMemberUpdate.js +2 -11
- package/src/client/actions/GuildRoleCreate.js +1 -5
- package/src/client/actions/GuildRoleDelete.js +1 -5
- package/src/client/actions/GuildRoleUpdate.js +1 -6
- package/src/client/actions/GuildScheduledEventCreate.js +1 -5
- package/src/client/actions/GuildScheduledEventDelete.js +1 -5
- package/src/client/actions/GuildScheduledEventUpdate.js +1 -6
- package/src/client/actions/GuildScheduledEventUserAdd.js +1 -6
- package/src/client/actions/GuildScheduledEventUserRemove.js +1 -6
- package/src/client/actions/GuildStickerCreate.js +1 -5
- package/src/client/actions/GuildStickerDelete.js +1 -5
- package/src/client/actions/GuildStickerUpdate.js +1 -6
- package/src/client/actions/GuildStickersUpdate.js +4 -4
- package/src/client/actions/GuildUpdate.js +1 -6
- package/src/client/actions/InteractionCreate.js +3 -7
- package/src/client/actions/InviteCreate.js +1 -7
- package/src/client/actions/InviteDelete.js +1 -7
- package/src/client/actions/MessageCreate.js +1 -5
- package/src/client/actions/MessageDelete.js +1 -5
- package/src/client/actions/MessageDeleteBulk.js +1 -6
- package/src/client/actions/MessageReactionAdd.js +5 -18
- package/src/client/actions/MessageReactionRemove.js +5 -16
- package/src/client/actions/MessageReactionRemoveAll.js +4 -9
- package/src/client/actions/MessageReactionRemoveEmoji.js +1 -5
- package/src/client/actions/PresenceUpdate.js +1 -6
- package/src/client/actions/StageInstanceCreate.js +1 -5
- package/src/client/actions/StageInstanceDelete.js +1 -5
- package/src/client/actions/StageInstanceUpdate.js +1 -6
- package/src/client/actions/ThreadCreate.js +1 -6
- package/src/client/actions/ThreadDelete.js +1 -5
- package/src/client/actions/ThreadListSync.js +2 -7
- package/src/client/actions/ThreadMemberUpdate.js +2 -7
- package/src/client/actions/ThreadMembersUpdate.js +2 -9
- package/src/client/actions/TypingStart.js +1 -5
- package/src/client/actions/UserUpdate.js +1 -8
- package/src/client/actions/VoiceStateUpdate.js +4 -9
- package/src/client/actions/WebhooksUpdate.js +1 -6
- package/src/client/voice/ClientVoiceManager.js +3 -13
- package/src/client/websocket/WebSocketManager.js +25 -117
- package/src/client/websocket/WebSocketShard.js +29 -116
- package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +2 -8
- package/src/client/websocket/handlers/CHANNEL_UPDATE.js +1 -6
- package/src/client/websocket/handlers/GUILD_CREATE.js +3 -7
- package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +2 -16
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +1 -5
- package/src/client/websocket/handlers/MESSAGE_UPDATE.js +1 -6
- package/src/client/websocket/handlers/RESUMED.js +1 -6
- package/src/client/websocket/handlers/THREAD_UPDATE.js +1 -6
- package/src/errors/DJSError.js +3 -14
- package/src/errors/ErrorCodes.js +2 -164
- package/src/index.js +6 -6
- package/src/managers/ApplicationCommandManager.js +14 -130
- package/src/managers/ApplicationCommandPermissionsManager.js +31 -202
- package/src/managers/AutoModerationRuleManager.js +17 -145
- package/src/managers/BaseGuildEmojiManager.js +7 -38
- package/src/managers/BaseManager.js +2 -10
- package/src/managers/CachedManager.js +3 -17
- package/src/managers/CategoryChannelChildManager.js +6 -48
- package/src/managers/ChannelManager.js +11 -52
- package/src/managers/DataManager.js +5 -27
- package/src/managers/GuildApplicationCommandManager.js +3 -12
- package/src/managers/GuildBanManager.js +16 -105
- package/src/managers/GuildChannelManager.js +31 -226
- package/src/managers/GuildEmojiManager.js +9 -65
- package/src/managers/GuildEmojiRoleManager.js +8 -47
- package/src/managers/GuildForumThreadManager.js +5 -39
- package/src/managers/GuildInviteManager.js +18 -117
- package/src/managers/GuildManager.js +14 -115
- package/src/managers/GuildMemberManager.js +38 -256
- package/src/managers/GuildMemberRoleManager.js +13 -76
- package/src/managers/GuildScheduledEventManager.js +25 -128
- package/src/managers/GuildStickerManager.js +16 -96
- package/src/managers/GuildTextThreadManager.js +4 -47
- package/src/managers/MessageManager.js +23 -137
- package/src/managers/PermissionOverwriteManager.js +10 -78
- package/src/managers/PresenceManager.js +6 -27
- package/src/managers/ReactionManager.js +7 -40
- package/src/managers/ReactionUserManager.js +6 -29
- package/src/managers/RoleManager.js +29 -177
- package/src/managers/StageInstanceManager.js +14 -77
- package/src/managers/ThreadManager.js +23 -113
- package/src/managers/ThreadMemberManager.js +20 -89
- package/src/managers/UserManager.js +13 -65
- package/src/managers/VoiceStateManager.js +3 -13
- package/src/sharding/Shard.js +42 -158
- package/src/sharding/ShardClientUtil.js +17 -101
- package/src/sharding/ShardingManager.js +34 -151
- package/src/structures/ActionRow.js +4 -21
- package/src/structures/ActionRowBuilder.js +3 -13
- package/src/structures/AnonymousGuild.js +10 -43
- package/src/structures/ApplicationCommand.js +54 -308
- package/src/structures/ApplicationRoleConnectionMetadata.js +7 -27
- package/src/structures/Attachment.js +16 -65
- package/src/structures/AttachmentBuilder.js +13 -58
- package/src/structures/AutoModerationActionExecution.js +16 -69
- package/src/structures/AutoModerationRule.js +32 -164
- package/src/structures/AutocompleteInteraction.js +10 -52
- package/src/structures/Base.js +2 -10
- package/src/structures/BaseChannel.js +16 -80
- package/src/structures/BaseGuild.js +13 -59
- package/src/structures/BaseGuildEmoji.js +5 -21
- package/src/structures/BaseGuildTextChannel.js +19 -100
- package/src/structures/BaseGuildVoiceChannel.js +21 -110
- package/src/structures/BaseInteraction.js +40 -196
- package/src/structures/BaseSelectMenuComponent.js +6 -29
- package/src/structures/ButtonBuilder.js +4 -18
- package/src/structures/ButtonComponent.js +7 -34
- package/src/structures/ButtonInteraction.js +1 -4
- package/src/structures/CategoryChannel.js +5 -30
- package/src/structures/ChannelSelectMenuBuilder.js +3 -13
- package/src/structures/ChannelSelectMenuComponent.js +2 -9
- package/src/structures/ChannelSelectMenuInteraction.js +3 -12
- package/src/structures/ChatInputCommandInteraction.js +3 -13
- package/src/structures/ClientApplication.js +20 -83
- package/src/structures/ClientPresence.js +5 -20
- package/src/structures/ClientUser.js +21 -118
- package/src/structures/CommandInteraction.js +21 -96
- package/src/structures/CommandInteractionOptionResolver.js +26 -147
- package/src/structures/Component.js +5 -21
- package/src/structures/ContextMenuCommandInteraction.js +4 -18
- package/src/structures/DMChannel.js +17 -52
- package/src/structures/DirectoryChannel.js +4 -16
- package/src/structures/Embed.js +21 -107
- package/src/structures/EmbedBuilder.js +4 -18
- package/src/structures/Emoji.js +12 -60
- package/src/structures/ForumChannel.js +30 -140
- package/src/structures/Guild.js +117 -697
- package/src/structures/GuildAuditLogs.js +7 -32
- package/src/structures/GuildAuditLogsEntry.js +27 -135
- package/src/structures/GuildBan.js +6 -26
- package/src/structures/GuildChannel.js +36 -188
- package/src/structures/GuildEmoji.js +13 -67
- package/src/structures/GuildMember.js +47 -270
- package/src/structures/GuildPreview.js +19 -84
- package/src/structures/GuildPreviewEmoji.js +3 -13
- package/src/structures/GuildScheduledEvent.js +46 -250
- package/src/structures/GuildTemplate.js +24 -108
- package/src/structures/Integration.js +25 -103
- package/src/structures/IntegrationApplication.js +8 -32
- package/src/structures/InteractionCollector.js +22 -113
- package/src/structures/InteractionResponse.js +11 -46
- package/src/structures/InteractionWebhook.js +8 -33
- package/src/structures/Invite.js +27 -131
- package/src/structures/InviteGuild.js +2 -8
- package/src/structures/InviteStageInstance.js +9 -39
- package/src/structures/MentionableSelectMenuBuilder.js +3 -14
- package/src/structures/MentionableSelectMenuComponent.js +1 -4
- package/src/structures/MentionableSelectMenuInteraction.js +5 -20
- package/src/structures/Message.js +85 -460
- package/src/structures/MessageCollector.js +14 -70
- package/src/structures/MessageComponentInteraction.js +14 -57
- package/src/structures/MessageContextMenuCommandInteraction.js +2 -9
- package/src/structures/MessageMentions.js +25 -136
- package/src/structures/MessagePayload.js +24 -94
- package/src/structures/MessageReaction.js +13 -51
- package/src/structures/ModalBuilder.js +3 -13
- package/src/structures/ModalSubmitFields.js +5 -22
- package/src/structures/ModalSubmitInteraction.js +17 -61
- package/src/structures/NewsChannel.js +2 -16
- package/src/structures/OAuth2Guild.js +3 -12
- package/src/structures/PartialGroupDMChannel.js +7 -27
- package/src/structures/PermissionOverwrites.js +20 -99
- package/src/structures/Presence.js +45 -190
- package/src/structures/ReactionCollector.js +20 -108
- package/src/structures/ReactionEmoji.js +2 -10
- package/src/structures/Role.js +39 -261
- package/src/structures/RoleSelectMenuBuilder.js +3 -13
- package/src/structures/RoleSelectMenuComponent.js +1 -4
- package/src/structures/RoleSelectMenuInteraction.js +3 -12
- package/src/structures/SelectMenuBuilder.js +1 -4
- package/src/structures/SelectMenuComponent.js +1 -4
- package/src/structures/SelectMenuInteraction.js +1 -4
- package/src/structures/SelectMenuOptionBuilder.js +1 -4
- package/src/structures/StageChannel.js +12 -84
- package/src/structures/StageInstance.js +16 -87
- package/src/structures/Sticker.js +25 -124
- package/src/structures/StickerPack.js +12 -52
- package/src/structures/StringSelectMenuBuilder.js +6 -29
- package/src/structures/StringSelectMenuComponent.js +2 -9
- package/src/structures/StringSelectMenuInteraction.js +2 -8
- package/src/structures/StringSelectMenuOptionBuilder.js +4 -18
- package/src/structures/Team.js +11 -52
- package/src/structures/TeamMember.js +7 -33
- package/src/structures/TextChannel.js +3 -14
- package/src/structures/TextInputBuilder.js +3 -13
- package/src/structures/TextInputComponent.js +3 -14
- package/src/structures/ThreadChannel.js +59 -305
- package/src/structures/ThreadMember.js +12 -55
- package/src/structures/Typing.js +8 -35
- package/src/structures/User.js +29 -151
- package/src/structures/UserContextMenuCommandInteraction.js +3 -14
- package/src/structures/UserSelectMenuBuilder.js +3 -13
- package/src/structures/UserSelectMenuComponent.js +1 -4
- package/src/structures/UserSelectMenuInteraction.js +4 -16
- package/src/structures/VoiceChannel.js +8 -67
- package/src/structures/VoiceRegion.js +6 -23
- package/src/structures/VoiceState.js +28 -140
- package/src/structures/Webhook.js +37 -227
- package/src/structures/WelcomeChannel.js +7 -29
- package/src/structures/WelcomeScreen.js +5 -20
- package/src/structures/Widget.js +9 -40
- package/src/structures/WidgetMember.js +15 -61
- package/src/structures/interfaces/Application.js +10 -49
- package/src/structures/interfaces/Collector.js +34 -155
- package/src/structures/interfaces/InteractionResponses.js +25 -164
- package/src/structures/interfaces/TextBasedChannel.js +37 -232
- package/src/util/APITypes.js +183 -456
- package/src/util/ActivityFlagsBitField.js +3 -15
- package/src/util/ApplicationFlagsBitField.js +3 -15
- package/src/util/BitField.js +17 -82
- package/src/util/ChannelFlagsBitField.js +7 -30
- package/src/util/Channels.js +5 -35
- package/src/util/Colors.js +3 -38
- package/src/util/Components.js +21 -75
- package/src/util/Constants.js +17 -144
- package/src/util/DataResolver.js +20 -77
- package/src/util/Events.js +3 -81
- package/src/util/Formatters.js +58 -305
- package/src/util/GuildMemberFlagsBitField.js +7 -30
- package/src/util/IntentsBitField.js +4 -23
- package/src/util/LimitedCollection.js +4 -21
- package/src/util/MessageFlagsBitField.js +5 -21
- package/src/util/Options.js +24 -137
- package/src/util/Partials.js +3 -30
- package/src/util/PermissionsBitField.js +12 -67
- package/src/util/ShardEvents.js +3 -15
- package/src/util/Status.js +3 -17
- package/src/util/Sweepers.js +35 -184
- package/src/util/SystemChannelFlagsBitField.js +7 -32
- package/src/util/ThreadMemberFlagsBitField.js +5 -21
- package/src/util/Transformers.js +2 -11
- package/src/util/UserFlagsBitField.js +5 -21
- package/src/util/Util.js +30 -169
- package/src/util/WebSocketShardEvents.js +3 -14
package/src/sharding/Shard.js
CHANGED
|
@@ -11,12 +11,7 @@ const { makeError, makePlainError } = require('../util/Util');
|
|
|
11
11
|
let childProcess = null;
|
|
12
12
|
let Worker = null;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
|
|
16
|
-
* an instance of the bot and its {@link Client}. When its child process/worker exits for any reason, the shard will
|
|
17
|
-
* spawn a new one to replace it as necessary.
|
|
18
|
-
* @extends {EventEmitter}
|
|
19
|
-
*/
|
|
14
|
+
|
|
20
15
|
class Shard extends EventEmitter {
|
|
21
16
|
constructor(manager, id) {
|
|
22
17
|
super();
|
|
@@ -30,34 +25,19 @@ class Shard extends EventEmitter {
|
|
|
30
25
|
break;
|
|
31
26
|
}
|
|
32
27
|
|
|
33
|
-
|
|
34
|
-
* Manager that created the shard
|
|
35
|
-
* @type {ShardingManager}
|
|
36
|
-
*/
|
|
28
|
+
|
|
37
29
|
this.manager = manager;
|
|
38
30
|
|
|
39
|
-
|
|
40
|
-
* The shard's id in the manager
|
|
41
|
-
* @type {number}
|
|
42
|
-
*/
|
|
31
|
+
|
|
43
32
|
this.id = id;
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
* Arguments for the shard's process (only when {@link ShardingManager#mode} is `process`)
|
|
47
|
-
* @type {string[]}
|
|
48
|
-
*/
|
|
34
|
+
|
|
49
35
|
this.args = manager.shardArgs ?? [];
|
|
50
36
|
|
|
51
|
-
|
|
52
|
-
* Arguments for the shard's process executable (only when {@link ShardingManager#mode} is `process`)
|
|
53
|
-
* @type {string[]}
|
|
54
|
-
*/
|
|
37
|
+
|
|
55
38
|
this.execArgv = manager.execArgv;
|
|
56
39
|
|
|
57
|
-
|
|
58
|
-
* Environment variables for the shard's process, or workerData for the shard's worker
|
|
59
|
-
* @type {Object}
|
|
60
|
-
*/
|
|
40
|
+
|
|
61
41
|
this.env = Object.assign({}, process.env, {
|
|
62
42
|
SHARDING_MANAGER: true,
|
|
63
43
|
SHARDS: this.id,
|
|
@@ -65,53 +45,26 @@ class Shard extends EventEmitter {
|
|
|
65
45
|
DISCORD_TOKEN: this.manager.token,
|
|
66
46
|
});
|
|
67
47
|
|
|
68
|
-
|
|
69
|
-
* Whether the shard's {@link Client} is ready
|
|
70
|
-
* @type {boolean}
|
|
71
|
-
*/
|
|
48
|
+
|
|
72
49
|
this.ready = false;
|
|
73
50
|
|
|
74
|
-
|
|
75
|
-
* Process of the shard (if {@link ShardingManager#mode} is `process`)
|
|
76
|
-
* @type {?ChildProcess}
|
|
77
|
-
*/
|
|
51
|
+
|
|
78
52
|
this.process = null;
|
|
79
53
|
|
|
80
|
-
|
|
81
|
-
* Worker of the shard (if {@link ShardingManager#mode} is `worker`)
|
|
82
|
-
* @type {?Worker}
|
|
83
|
-
*/
|
|
54
|
+
|
|
84
55
|
this.worker = null;
|
|
85
56
|
|
|
86
|
-
|
|
87
|
-
* Ongoing promises for calls to {@link Shard#eval}, mapped by the `script` they were called with
|
|
88
|
-
* @type {Map<string, Promise>}
|
|
89
|
-
* @private
|
|
90
|
-
*/
|
|
57
|
+
|
|
91
58
|
this._evals = new Map();
|
|
92
59
|
|
|
93
|
-
|
|
94
|
-
* Ongoing promises for calls to {@link Shard#fetchClientValue}, mapped by the `prop` they were called with
|
|
95
|
-
* @type {Map<string, Promise>}
|
|
96
|
-
* @private
|
|
97
|
-
*/
|
|
60
|
+
|
|
98
61
|
this._fetches = new Map();
|
|
99
62
|
|
|
100
|
-
|
|
101
|
-
* Listener function for the {@link ChildProcess}' `exit` event
|
|
102
|
-
* @type {Function}
|
|
103
|
-
* @private
|
|
104
|
-
*/
|
|
63
|
+
|
|
105
64
|
this._exitListener = null;
|
|
106
65
|
}
|
|
107
66
|
|
|
108
|
-
|
|
109
|
-
* Forks a child process or creates a worker thread for the shard.
|
|
110
|
-
* <warn>You should not need to call this manually.</warn>
|
|
111
|
-
* @param {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
|
|
112
|
-
* before resolving (`-1` or `Infinity` for no wait)
|
|
113
|
-
* @returns {Promise<ChildProcess>}
|
|
114
|
-
*/
|
|
67
|
+
|
|
115
68
|
spawn(timeout = 30_000) {
|
|
116
69
|
if (this.process) throw new DiscordjsError(ErrorCodes.ShardingProcessExists, this.id);
|
|
117
70
|
if (this.worker) throw new DiscordjsError(ErrorCodes.ShardingWorkerExists, this.id);
|
|
@@ -140,11 +93,7 @@ class Shard extends EventEmitter {
|
|
|
140
93
|
|
|
141
94
|
const child = this.process ?? this.worker;
|
|
142
95
|
|
|
143
|
-
|
|
144
|
-
* Emitted upon the creation of the shard's child process/worker.
|
|
145
|
-
* @event Shard#spawn
|
|
146
|
-
* @param {ChildProcess|Worker} process Child process/worker that was created
|
|
147
|
-
*/
|
|
96
|
+
|
|
148
97
|
this.emit(ShardEvents.Spawn, child);
|
|
149
98
|
|
|
150
99
|
if (timeout === -1 || timeout === Infinity) return Promise.resolve(child);
|
|
@@ -183,9 +132,7 @@ class Shard extends EventEmitter {
|
|
|
183
132
|
});
|
|
184
133
|
}
|
|
185
134
|
|
|
186
|
-
|
|
187
|
-
* Immediately kills the shard's process/worker and does not restart it.
|
|
188
|
-
*/
|
|
135
|
+
|
|
189
136
|
kill() {
|
|
190
137
|
if (this.process) {
|
|
191
138
|
this.process.removeListener('exit', this._exitListener);
|
|
@@ -198,31 +145,16 @@ class Shard extends EventEmitter {
|
|
|
198
145
|
this._handleExit(false);
|
|
199
146
|
}
|
|
200
147
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
* @property {number} [delay=500] How long to wait between killing the process/worker and
|
|
205
|
-
* restarting it (in milliseconds)
|
|
206
|
-
* @property {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client}
|
|
207
|
-
* has become ready before resolving (`-1` or `Infinity` for no wait)
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Kills and restarts the shard's process/worker.
|
|
212
|
-
* @param {ShardRespawnOptions} [options] Options for respawning the shard
|
|
213
|
-
* @returns {Promise<ChildProcess>}
|
|
214
|
-
*/
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
215
151
|
async respawn({ delay = 500, timeout = 30_000 } = {}) {
|
|
216
152
|
this.kill();
|
|
217
153
|
if (delay > 0) await sleep(delay);
|
|
218
154
|
return this.spawn(timeout);
|
|
219
155
|
}
|
|
220
156
|
|
|
221
|
-
|
|
222
|
-
* Sends a message to the shard's process/worker.
|
|
223
|
-
* @param {*} message Message to send to the shard
|
|
224
|
-
* @returns {Promise<Shard>}
|
|
225
|
-
*/
|
|
157
|
+
|
|
226
158
|
send(message) {
|
|
227
159
|
return new Promise((resolve, reject) => {
|
|
228
160
|
if (this.process) {
|
|
@@ -237,22 +169,14 @@ class Shard extends EventEmitter {
|
|
|
237
169
|
});
|
|
238
170
|
}
|
|
239
171
|
|
|
240
|
-
|
|
241
|
-
* Fetches a client property value of the shard.
|
|
242
|
-
* @param {string} prop Name of the client property to get, using periods for nesting
|
|
243
|
-
* @returns {Promise<*>}
|
|
244
|
-
* @example
|
|
245
|
-
* shard.fetchClientValue('guilds.cache.size')
|
|
246
|
-
* .then(count => console.log(`${count} guilds in shard ${shard.id}`))
|
|
247
|
-
* .catch(console.error);
|
|
248
|
-
*/
|
|
172
|
+
|
|
249
173
|
fetchClientValue(prop) {
|
|
250
|
-
|
|
174
|
+
|
|
251
175
|
if (!this.process && !this.worker) {
|
|
252
176
|
return Promise.reject(new DiscordjsError(ErrorCodes.ShardingNoChildExists, this.id));
|
|
253
177
|
}
|
|
254
178
|
|
|
255
|
-
|
|
179
|
+
|
|
256
180
|
if (this._fetches.has(prop)) return this._fetches.get(prop);
|
|
257
181
|
|
|
258
182
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -282,22 +206,17 @@ class Shard extends EventEmitter {
|
|
|
282
206
|
return promise;
|
|
283
207
|
}
|
|
284
208
|
|
|
285
|
-
|
|
286
|
-
* Evaluates a script or function on the shard, in the context of the {@link Client}.
|
|
287
|
-
* @param {string|Function} script JavaScript to run on the shard
|
|
288
|
-
* @param {*} [context] The context for the eval
|
|
289
|
-
* @returns {Promise<*>} Result of the script execution
|
|
290
|
-
*/
|
|
209
|
+
|
|
291
210
|
eval(script, context) {
|
|
292
|
-
|
|
211
|
+
|
|
293
212
|
const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script;
|
|
294
213
|
|
|
295
|
-
|
|
214
|
+
|
|
296
215
|
if (!this.process && !this.worker) {
|
|
297
216
|
return Promise.reject(new DiscordjsError(ErrorCodes.ShardingNoChildExists, this.id));
|
|
298
217
|
}
|
|
299
218
|
|
|
300
|
-
|
|
219
|
+
|
|
301
220
|
if (this._evals.has(_eval)) return this._evals.get(_eval);
|
|
302
221
|
|
|
303
222
|
const promise = new Promise((resolve, reject) => {
|
|
@@ -327,47 +246,34 @@ class Shard extends EventEmitter {
|
|
|
327
246
|
return promise;
|
|
328
247
|
}
|
|
329
248
|
|
|
330
|
-
|
|
331
|
-
* Handles a message received from the child process/worker.
|
|
332
|
-
* @param {*} message Message received
|
|
333
|
-
* @private
|
|
334
|
-
*/
|
|
249
|
+
|
|
335
250
|
_handleMessage(message) {
|
|
336
251
|
if (message) {
|
|
337
|
-
|
|
252
|
+
|
|
338
253
|
if (message._ready) {
|
|
339
254
|
this.ready = true;
|
|
340
|
-
|
|
341
|
-
* Emitted upon the shard's {@link Client#event:shardReady} event.
|
|
342
|
-
* @event Shard#ready
|
|
343
|
-
*/
|
|
255
|
+
|
|
344
256
|
this.emit(ShardEvents.Ready);
|
|
345
257
|
return;
|
|
346
258
|
}
|
|
347
259
|
|
|
348
|
-
|
|
260
|
+
|
|
349
261
|
if (message._disconnect) {
|
|
350
262
|
this.ready = false;
|
|
351
|
-
|
|
352
|
-
* Emitted upon the shard's {@link Client#event:shardDisconnect} event.
|
|
353
|
-
* @event Shard#disconnect
|
|
354
|
-
*/
|
|
263
|
+
|
|
355
264
|
this.emit(ShardEvents.Disconnect);
|
|
356
265
|
return;
|
|
357
266
|
}
|
|
358
267
|
|
|
359
|
-
|
|
268
|
+
|
|
360
269
|
if (message._reconnecting) {
|
|
361
270
|
this.ready = false;
|
|
362
|
-
|
|
363
|
-
* Emitted upon the shard's {@link Client#event:shardReconnecting} event.
|
|
364
|
-
* @event Shard#reconnecting
|
|
365
|
-
*/
|
|
271
|
+
|
|
366
272
|
this.emit(ShardEvents.Reconnecting);
|
|
367
273
|
return;
|
|
368
274
|
}
|
|
369
275
|
|
|
370
|
-
|
|
276
|
+
|
|
371
277
|
if (message._sFetchProp) {
|
|
372
278
|
const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard };
|
|
373
279
|
this.manager.fetchClientValues(message._sFetchProp, message._sFetchPropShard).then(
|
|
@@ -377,7 +283,7 @@ class Shard extends EventEmitter {
|
|
|
377
283
|
return;
|
|
378
284
|
}
|
|
379
285
|
|
|
380
|
-
|
|
286
|
+
|
|
381
287
|
if (message._sEval) {
|
|
382
288
|
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
|
|
383
289
|
this.manager._performOnShards('eval', [message._sEval], message._sEvalShard).then(
|
|
@@ -387,37 +293,23 @@ class Shard extends EventEmitter {
|
|
|
387
293
|
return;
|
|
388
294
|
}
|
|
389
295
|
|
|
390
|
-
|
|
296
|
+
|
|
391
297
|
if (message._sRespawnAll) {
|
|
392
298
|
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
|
|
393
299
|
this.manager.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() => {
|
|
394
|
-
|
|
300
|
+
|
|
395
301
|
});
|
|
396
302
|
return;
|
|
397
303
|
}
|
|
398
304
|
}
|
|
399
305
|
|
|
400
|
-
|
|
401
|
-
* Emitted upon receiving a message from the child process/worker.
|
|
402
|
-
* @event Shard#message
|
|
403
|
-
* @param {*} message Message that was received
|
|
404
|
-
*/
|
|
306
|
+
|
|
405
307
|
this.emit(ShardEvents.Message, message);
|
|
406
308
|
}
|
|
407
309
|
|
|
408
|
-
|
|
409
|
-
* Handles the shard's process/worker exiting.
|
|
410
|
-
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
|
|
411
|
-
* @param {number} [timeout] The amount in milliseconds to wait until the {@link Client}
|
|
412
|
-
* has become ready (`-1` or `Infinity` for no wait)
|
|
413
|
-
* @private
|
|
414
|
-
*/
|
|
310
|
+
|
|
415
311
|
_handleExit(respawn = this.manager.respawn, timeout) {
|
|
416
|
-
|
|
417
|
-
* Emitted upon the shard's child process/worker exiting.
|
|
418
|
-
* @event Shard#death
|
|
419
|
-
* @param {ChildProcess|Worker} process Child process/worker that exited
|
|
420
|
-
*/
|
|
312
|
+
|
|
421
313
|
this.emit(ShardEvents.Death, this.process ?? this.worker);
|
|
422
314
|
|
|
423
315
|
this.ready = false;
|
|
@@ -429,11 +321,7 @@ class Shard extends EventEmitter {
|
|
|
429
321
|
if (respawn) this.spawn(timeout).catch(err => this.emit(ShardEvents.Error, err));
|
|
430
322
|
}
|
|
431
323
|
|
|
432
|
-
|
|
433
|
-
* Increments max listeners by one for a given emitter, if they are not zero.
|
|
434
|
-
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
435
|
-
* @private
|
|
436
|
-
*/
|
|
324
|
+
|
|
437
325
|
incrementMaxListeners(emitter) {
|
|
438
326
|
const maxListeners = emitter.getMaxListeners();
|
|
439
327
|
if (maxListeners !== 0) {
|
|
@@ -441,11 +329,7 @@ class Shard extends EventEmitter {
|
|
|
441
329
|
}
|
|
442
330
|
}
|
|
443
331
|
|
|
444
|
-
|
|
445
|
-
* Decrements max listeners by one for a given emitter, if they are not zero.
|
|
446
|
-
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
447
|
-
* @private
|
|
448
|
-
*/
|
|
332
|
+
|
|
449
333
|
decrementMaxListeners(emitter) {
|
|
450
334
|
const maxListeners = emitter.getMaxListeners();
|
|
451
335
|
if (maxListeners !== 0) {
|
|
@@ -6,28 +6,16 @@ const { DiscordjsError, DiscordjsTypeError, ErrorCodes } = require('../errors');
|
|
|
6
6
|
const Events = require('../util/Events');
|
|
7
7
|
const { makeError, makePlainError } = require('../util/Util');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
* Helper class for sharded clients spawned as a child process/worker, such as from a {@link ShardingManager}.
|
|
11
|
-
* Utilises IPC to send and receive data to/from the master process and other shards.
|
|
12
|
-
*/
|
|
9
|
+
|
|
13
10
|
class ShardClientUtil {
|
|
14
11
|
constructor(client, mode) {
|
|
15
|
-
|
|
16
|
-
* Client for the shard
|
|
17
|
-
* @type {Client}
|
|
18
|
-
*/
|
|
12
|
+
|
|
19
13
|
this.client = client;
|
|
20
14
|
|
|
21
|
-
|
|
22
|
-
* Mode the shard was spawned with
|
|
23
|
-
* @type {ShardingManagerMode}
|
|
24
|
-
*/
|
|
15
|
+
|
|
25
16
|
this.mode = mode;
|
|
26
17
|
|
|
27
|
-
|
|
28
|
-
* Message port for the master process (only when {@link ShardClientUtil#mode} is `worker`)
|
|
29
|
-
* @type {?MessagePort}
|
|
30
|
-
*/
|
|
18
|
+
|
|
31
19
|
this.parentPort = null;
|
|
32
20
|
|
|
33
21
|
switch (mode) {
|
|
@@ -59,30 +47,17 @@ class ShardClientUtil {
|
|
|
59
47
|
}
|
|
60
48
|
}
|
|
61
49
|
|
|
62
|
-
|
|
63
|
-
* Array of shard ids of this client
|
|
64
|
-
* @type {number[]}
|
|
65
|
-
* @readonly
|
|
66
|
-
*/
|
|
50
|
+
|
|
67
51
|
get ids() {
|
|
68
52
|
return this.client.options.shards;
|
|
69
53
|
}
|
|
70
54
|
|
|
71
|
-
|
|
72
|
-
* Total number of shards
|
|
73
|
-
* @type {number}
|
|
74
|
-
* @readonly
|
|
75
|
-
*/
|
|
55
|
+
|
|
76
56
|
get count() {
|
|
77
57
|
return this.client.options.shardCount;
|
|
78
58
|
}
|
|
79
59
|
|
|
80
|
-
|
|
81
|
-
* Sends a message to the master process.
|
|
82
|
-
* @param {*} message Message to send
|
|
83
|
-
* @returns {Promise<void>}
|
|
84
|
-
* @emits Shard#message
|
|
85
|
-
*/
|
|
60
|
+
|
|
86
61
|
send(message) {
|
|
87
62
|
return new Promise((resolve, reject) => {
|
|
88
63
|
switch (this.mode) {
|
|
@@ -100,17 +75,7 @@ class ShardClientUtil {
|
|
|
100
75
|
});
|
|
101
76
|
}
|
|
102
77
|
|
|
103
|
-
|
|
104
|
-
* Fetches a client property value of each shard, or a given shard.
|
|
105
|
-
* @param {string} prop Name of the client property to get, using periods for nesting
|
|
106
|
-
* @param {number} [shard] Shard to fetch property from, all if undefined
|
|
107
|
-
* @returns {Promise<*|Array<*>>}
|
|
108
|
-
* @example
|
|
109
|
-
* client.shard.fetchClientValues('guilds.cache.size')
|
|
110
|
-
* .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
|
|
111
|
-
* .catch(console.error);
|
|
112
|
-
* @see {@link ShardingManager#fetchClientValues}
|
|
113
|
-
*/
|
|
78
|
+
|
|
114
79
|
fetchClientValues(prop, shard) {
|
|
115
80
|
return new Promise((resolve, reject) => {
|
|
116
81
|
const parent = this.parentPort ?? process;
|
|
@@ -133,17 +98,7 @@ class ShardClientUtil {
|
|
|
133
98
|
});
|
|
134
99
|
}
|
|
135
100
|
|
|
136
|
-
|
|
137
|
-
* Evaluates a script or function on all shards, or a given shard, in the context of the {@link Client}s.
|
|
138
|
-
* @param {Function} script JavaScript to run on each shard
|
|
139
|
-
* @param {BroadcastEvalOptions} [options={}] The options for the broadcast
|
|
140
|
-
* @returns {Promise<*|Array<*>>} Results of the script execution
|
|
141
|
-
* @example
|
|
142
|
-
* client.shard.broadcastEval(client => client.guilds.cache.size)
|
|
143
|
-
* .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
|
|
144
|
-
* .catch(console.error);
|
|
145
|
-
* @see {@link ShardingManager#broadcastEval}
|
|
146
|
-
*/
|
|
101
|
+
|
|
147
102
|
broadcastEval(script, options = {}) {
|
|
148
103
|
return new Promise((resolve, reject) => {
|
|
149
104
|
const parent = this.parentPort ?? process;
|
|
@@ -170,21 +125,12 @@ class ShardClientUtil {
|
|
|
170
125
|
});
|
|
171
126
|
}
|
|
172
127
|
|
|
173
|
-
|
|
174
|
-
* Requests a respawn of all shards.
|
|
175
|
-
* @param {MultipleShardRespawnOptions} [options] Options for respawning shards
|
|
176
|
-
* @returns {Promise<void>} Resolves upon the message being sent
|
|
177
|
-
* @see {@link ShardingManager#respawnAll}
|
|
178
|
-
*/
|
|
128
|
+
|
|
179
129
|
respawnAll({ shardDelay = 5_000, respawnDelay = 500, timeout = 30_000 } = {}) {
|
|
180
130
|
return this.send({ _sRespawnAll: { shardDelay, respawnDelay, timeout } });
|
|
181
131
|
}
|
|
182
132
|
|
|
183
|
-
|
|
184
|
-
* Handles an IPC message.
|
|
185
|
-
* @param {*} message Message received
|
|
186
|
-
* @private
|
|
187
|
-
*/
|
|
133
|
+
|
|
188
134
|
async _handleMessage(message) {
|
|
189
135
|
if (!message) return;
|
|
190
136
|
if (message._fetchProp) {
|
|
@@ -205,34 +151,17 @@ class ShardClientUtil {
|
|
|
205
151
|
}
|
|
206
152
|
}
|
|
207
153
|
|
|
208
|
-
|
|
209
|
-
* Sends a message to the master process, emitting an error from the client upon failure.
|
|
210
|
-
* @param {string} type Type of response to send
|
|
211
|
-
* @param {*} message Message to send
|
|
212
|
-
* @private
|
|
213
|
-
*/
|
|
154
|
+
|
|
214
155
|
_respond(type, message) {
|
|
215
156
|
this.send(message).catch(err => {
|
|
216
157
|
const error = new Error(`Error when sending ${type} response to master process: ${err.message}`);
|
|
217
158
|
error.stack = err.stack;
|
|
218
|
-
|
|
219
|
-
* Emitted when the client encounters an error.
|
|
220
|
-
* <warn>Errors thrown within this event do not have a catch handler, it is
|
|
221
|
-
* recommended to not use async functions as `error` event handlers. See the
|
|
222
|
-
* [Node.js docs](https://nodejs.org/api/events.html#capture-rejections-of-promises) for details.</warn>
|
|
223
|
-
* @event Client#error
|
|
224
|
-
* @param {Error} error The error encountered
|
|
225
|
-
*/
|
|
159
|
+
|
|
226
160
|
this.client.emit(Events.Error, error);
|
|
227
161
|
});
|
|
228
162
|
}
|
|
229
163
|
|
|
230
|
-
|
|
231
|
-
* Creates/gets the singleton of this class.
|
|
232
|
-
* @param {Client} client The client to use
|
|
233
|
-
* @param {ShardingManagerMode} mode Mode the shard was spawned with
|
|
234
|
-
* @returns {ShardClientUtil}
|
|
235
|
-
*/
|
|
164
|
+
|
|
236
165
|
static singleton(client, mode) {
|
|
237
166
|
if (!this._singleton) {
|
|
238
167
|
this._singleton = new this(client, mode);
|
|
@@ -245,23 +174,14 @@ class ShardClientUtil {
|
|
|
245
174
|
return this._singleton;
|
|
246
175
|
}
|
|
247
176
|
|
|
248
|
-
|
|
249
|
-
* Get the shard id for a given guild id.
|
|
250
|
-
* @param {Snowflake} guildId Snowflake guild id to get shard id for
|
|
251
|
-
* @param {number} shardCount Number of shards
|
|
252
|
-
* @returns {number}
|
|
253
|
-
*/
|
|
177
|
+
|
|
254
178
|
static shardIdForGuildId(guildId, shardCount) {
|
|
255
179
|
const shard = calculateShardId(guildId, shardCount);
|
|
256
180
|
if (shard < 0) throw new DiscordjsError(ErrorCodes.ShardingShardMiscalculation, shard, guildId, shardCount);
|
|
257
181
|
return shard;
|
|
258
182
|
}
|
|
259
183
|
|
|
260
|
-
|
|
261
|
-
* Increments max listeners by one for a given emitter, if they are not zero.
|
|
262
|
-
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
263
|
-
* @private
|
|
264
|
-
*/
|
|
184
|
+
|
|
265
185
|
incrementMaxListeners(emitter) {
|
|
266
186
|
const maxListeners = emitter.getMaxListeners();
|
|
267
187
|
if (maxListeners !== 0) {
|
|
@@ -269,11 +189,7 @@ class ShardClientUtil {
|
|
|
269
189
|
}
|
|
270
190
|
}
|
|
271
191
|
|
|
272
|
-
|
|
273
|
-
* Decrements max listeners by one for a given emitter, if they are not zero.
|
|
274
|
-
* @param {EventEmitter|process} emitter The emitter that emits the events.
|
|
275
|
-
* @private
|
|
276
|
-
*/
|
|
192
|
+
|
|
277
193
|
decrementMaxListeners(emitter) {
|
|
278
194
|
const maxListeners = emitter.getMaxListeners();
|
|
279
195
|
if (maxListeners !== 0) {
|