@ovencord/discord.js 14.16.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/README.md +148 -0
- package/package.json +73 -0
- package/src/client/Client.ts +967 -0
- package/src/client/actions/Action.ts +141 -0
- package/src/client/actions/ActionsManager.ts +138 -0
- package/src/client/actions/ChannelCreate.ts +21 -0
- package/src/client/actions/ChannelDelete.ts +20 -0
- package/src/client/actions/ChannelUpdate.ts +38 -0
- package/src/client/actions/GuildChannelsPositionUpdate.ts +17 -0
- package/src/client/actions/GuildEmojiCreate.ts +17 -0
- package/src/client/actions/GuildEmojiDelete.ts +16 -0
- package/src/client/actions/GuildEmojiUpdate.ts +17 -0
- package/src/client/actions/GuildEmojisUpdate.ts +30 -0
- package/src/client/actions/GuildMemberRemove.ts +29 -0
- package/src/client/actions/GuildMemberUpdate.ts +41 -0
- package/src/client/actions/GuildRoleCreate.ts +23 -0
- package/src/client/actions/GuildRoleDelete.ts +26 -0
- package/src/client/actions/GuildRolesPositionUpdate.ts +17 -0
- package/src/client/actions/GuildScheduledEventDelete.ts +28 -0
- package/src/client/actions/GuildScheduledEventUserAdd.ts +29 -0
- package/src/client/actions/GuildScheduledEventUserRemove.ts +29 -0
- package/src/client/actions/GuildSoundboardSoundDelete.ts +26 -0
- package/src/client/actions/GuildStickerCreate.ts +17 -0
- package/src/client/actions/GuildStickerDelete.ts +16 -0
- package/src/client/actions/GuildStickerUpdate.ts +17 -0
- package/src/client/actions/GuildStickersUpdate.ts +30 -0
- package/src/client/actions/GuildUpdate.ts +30 -0
- package/src/client/actions/InteractionCreate.ts +104 -0
- package/src/client/actions/MessageCreate.ts +38 -0
- package/src/client/actions/MessageDelete.ts +29 -0
- package/src/client/actions/MessageDeleteBulk.ts +45 -0
- package/src/client/actions/MessagePollVoteAdd.ts +37 -0
- package/src/client/actions/MessagePollVoteRemove.ts +35 -0
- package/src/client/actions/MessageReactionAdd.ts +68 -0
- package/src/client/actions/MessageReactionRemove.ts +47 -0
- package/src/client/actions/MessageReactionRemoveAll.ts +30 -0
- package/src/client/actions/MessageReactionRemoveEmoji.ts +25 -0
- package/src/client/actions/MessageUpdate.ts +22 -0
- package/src/client/actions/StageInstanceCreate.ts +25 -0
- package/src/client/actions/StageInstanceDelete.ts +28 -0
- package/src/client/actions/StageInstanceUpdate.ts +27 -0
- package/src/client/actions/ThreadCreate.ts +22 -0
- package/src/client/actions/ThreadMembersUpdate.ts +45 -0
- package/src/client/actions/TypingStart.ts +26 -0
- package/src/client/actions/UserUpdate.ts +33 -0
- package/src/client/voice/ClientVoiceManager.ts +46 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.ts +19 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_ACTION_EXECUTION.ts +17 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_CREATE.ts +18 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_DELETE.ts +21 -0
- package/src/client/websocket/handlers/AUTO_MODERATION_RULE_UPDATE.ts +20 -0
- package/src/client/websocket/handlers/CHANNEL_CREATE.ts +4 -0
- package/src/client/websocket/handlers/CHANNEL_DELETE.ts +4 -0
- package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.ts +22 -0
- package/src/client/websocket/handlers/CHANNEL_UPDATE.ts +16 -0
- package/src/client/websocket/handlers/ENTITLEMENT_CREATE.ts +14 -0
- package/src/client/websocket/handlers/ENTITLEMENT_DELETE.ts +18 -0
- package/src/client/websocket/handlers/ENTITLEMENT_UPDATE.ts +16 -0
- package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.ts +19 -0
- package/src/client/websocket/handlers/GUILD_BAN_ADD.ts +15 -0
- package/src/client/websocket/handlers/GUILD_BAN_REMOVE.ts +20 -0
- package/src/client/websocket/handlers/GUILD_CREATE.ts +34 -0
- package/src/client/websocket/handlers/GUILD_DELETE.ts +36 -0
- package/src/client/websocket/handlers/GUILD_EMOJIS_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_INTEGRATIONS_UPDATE.ts +15 -0
- package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.ts +40 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.ts +17 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_REMOVE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_MEMBER_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_ROLE_CREATE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_ROLE_DELETE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_ROLE_UPDATE.ts +21 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_CREATE.ts +17 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_DELETE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_UPDATE.ts +19 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_ADD.ts +4 -0
- package/src/client/websocket/handlers/GUILD_SCHEDULED_EVENT_USER_REMOVE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUNDS_UPDATE.ts +24 -0
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_CREATE.ts +18 -0
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_DELETE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_SOUNDBOARD_SOUND_UPDATE.ts +20 -0
- package/src/client/websocket/handlers/GUILD_STICKERS_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/GUILD_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/INTERACTION_CREATE.ts +4 -0
- package/src/client/websocket/handlers/INVITE_CREATE.ts +20 -0
- package/src/client/websocket/handlers/INVITE_DELETE.ts +23 -0
- package/src/client/websocket/handlers/MESSAGE_CREATE.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE_BULK.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_ADD.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_POLL_VOTE_REMOVE.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_ADD.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_ALL.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_REACTION_REMOVE_EMOJI.ts +4 -0
- package/src/client/websocket/handlers/MESSAGE_UPDATE.ts +16 -0
- package/src/client/websocket/handlers/PRESENCE_UPDATE.ts +42 -0
- package/src/client/websocket/handlers/RATE_LIMITED.ts +23 -0
- package/src/client/websocket/handlers/READY.ts +29 -0
- package/src/client/websocket/handlers/SOUNDBOARD_SOUNDS.ts +24 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_CREATE.ts +4 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_DELETE.ts +4 -0
- package/src/client/websocket/handlers/STAGE_INSTANCE_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/SUBSCRIPTION_CREATE.ts +14 -0
- package/src/client/websocket/handlers/SUBSCRIPTION_DELETE.ts +16 -0
- package/src/client/websocket/handlers/SUBSCRIPTION_UPDATE.ts +16 -0
- package/src/client/websocket/handlers/THREAD_CREATE.ts +4 -0
- package/src/client/websocket/handlers/THREAD_DELETE.ts +17 -0
- package/src/client/websocket/handlers/THREAD_LIST_SYNC.ts +50 -0
- package/src/client/websocket/handlers/THREAD_MEMBERS_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/THREAD_MEMBER_UPDATE.ts +25 -0
- package/src/client/websocket/handlers/THREAD_UPDATE.ts +16 -0
- package/src/client/websocket/handlers/TYPING_START.ts +4 -0
- package/src/client/websocket/handlers/USER_UPDATE.ts +4 -0
- package/src/client/websocket/handlers/VOICE_CHANNEL_EFFECT_SEND.ts +16 -0
- package/src/client/websocket/handlers/VOICE_SERVER_UPDATE.ts +5 -0
- package/src/client/websocket/handlers/VOICE_STATE_UPDATE.ts +37 -0
- package/src/client/websocket/handlers/WEBHOOKS_UPDATE.ts +16 -0
- package/src/client/websocket/handlers/index.ts +74 -0
- package/src/errors/DJSError.ts +50 -0
- package/src/errors/ErrorCodes.ts +277 -0
- package/src/errors/Messages.ts +155 -0
- package/src/errors/index.ts +6 -0
- package/src/index.ts +241 -0
- package/src/managers/ApplicationCommandManager.ts +303 -0
- package/src/managers/ApplicationCommandPermissionsManager.ts +453 -0
- package/src/managers/ApplicationEmojiManager.ts +149 -0
- package/src/managers/AutoModerationRuleManager.ts +307 -0
- package/src/managers/BaseManager.ts +13 -0
- package/src/managers/CachedManager.ts +56 -0
- package/src/managers/CategoryChannelChildManager.ts +81 -0
- package/src/managers/ChannelManager.ts +194 -0
- package/src/managers/DMMessageManager.ts +15 -0
- package/src/managers/DataManager.ts +38 -0
- package/src/managers/EntitlementManager.ts +182 -0
- package/src/managers/GuildApplicationCommandManager.ts +29 -0
- package/src/managers/GuildBanManager.ts +221 -0
- package/src/managers/GuildChannelManager.ts +552 -0
- package/src/managers/GuildEmojiManager.ts +262 -0
- package/src/managers/GuildEmojiRoleManager.ts +134 -0
- package/src/managers/GuildForumThreadManager.ts +84 -0
- package/src/managers/GuildInviteManager.ts +236 -0
- package/src/managers/GuildManager.ts +265 -0
- package/src/managers/GuildMemberManager.ts +619 -0
- package/src/managers/GuildMemberRoleManager.ts +230 -0
- package/src/managers/GuildMessageManager.ts +31 -0
- package/src/managers/GuildScheduledEventManager.ts +330 -0
- package/src/managers/GuildSoundboardSoundManager.ts +157 -0
- package/src/managers/GuildStickerManager.ts +192 -0
- package/src/managers/GuildTextThreadManager.ts +91 -0
- package/src/managers/MessageManager.ts +352 -0
- package/src/managers/PartialGroupDMMessageManager.ts +15 -0
- package/src/managers/PermissionOverwriteManager.ts +174 -0
- package/src/managers/PollAnswerVoterManager.ts +59 -0
- package/src/managers/PresenceManager.ts +60 -0
- package/src/managers/ReactionManager.ts +72 -0
- package/src/managers/ReactionUserManager.ts +82 -0
- package/src/managers/RoleManager.ts +432 -0
- package/src/managers/StageInstanceManager.ts +166 -0
- package/src/managers/SubscriptionManager.ts +83 -0
- package/src/managers/ThreadManager.ts +210 -0
- package/src/managers/ThreadMemberManager.ts +194 -0
- package/src/managers/UserManager.ts +139 -0
- package/src/managers/VoiceStateManager.ts +61 -0
- package/src/sharding/Shard.ts +513 -0
- package/src/sharding/ShardClientUtil.ts +293 -0
- package/src/sharding/ShardingManager.ts +374 -0
- package/src/structures/ActionRow.ts +31 -0
- package/src/structures/AnnouncementChannel.ts +25 -0
- package/src/structures/AnonymousGuild.ts +110 -0
- package/src/structures/ApplicationCommand.ts +675 -0
- package/src/structures/ApplicationEmoji.ts +189 -0
- package/src/structures/ApplicationRoleConnectionMetadata.ts +54 -0
- package/src/structures/Attachment.ts +189 -0
- package/src/structures/AuthorizingIntegrationOwners.ts +63 -0
- package/src/structures/AutoModerationActionExecution.ts +138 -0
- package/src/structures/AutoModerationRule.ts +325 -0
- package/src/structures/AutocompleteInteraction.ts +115 -0
- package/src/structures/Base.ts +37 -0
- package/src/structures/BaseChannel.ts +188 -0
- package/src/structures/BaseGuild.ts +134 -0
- package/src/structures/BaseGuildEmoji.ts +113 -0
- package/src/structures/BaseGuildTextChannel.ts +230 -0
- package/src/structures/BaseGuildVoiceChannel.ts +270 -0
- package/src/structures/BaseInteraction.ts +393 -0
- package/src/structures/BaseInvite.ts +192 -0
- package/src/structures/BaseSelectMenuComponent.ts +58 -0
- package/src/structures/ButtonComponent.ts +68 -0
- package/src/structures/ButtonInteraction.ts +8 -0
- package/src/structures/CategoryChannel.ts +46 -0
- package/src/structures/ChannelSelectMenuComponent.ts +18 -0
- package/src/structures/ChannelSelectMenuInteraction.ts +34 -0
- package/src/structures/ChatInputCommandInteraction.ts +43 -0
- package/src/structures/ClientApplication.ts +470 -0
- package/src/structures/ClientPresence.ts +87 -0
- package/src/structures/ClientUser.ts +219 -0
- package/src/structures/CommandInteraction.ts +193 -0
- package/src/structures/CommandInteractionOptionResolver.ts +336 -0
- package/src/structures/Component.ts +59 -0
- package/src/structures/ContainerComponent.ts +63 -0
- package/src/structures/ContextMenuCommandInteraction.ts +68 -0
- package/src/structures/DMChannel.ts +148 -0
- package/src/structures/DirectoryChannel.ts +40 -0
- package/src/structures/Embed.ts +251 -0
- package/src/structures/Emoji.ts +61 -0
- package/src/structures/Entitlement.ts +206 -0
- package/src/structures/FileComponent.ts +41 -0
- package/src/structures/ForumChannel.ts +31 -0
- package/src/structures/GroupDMInvite.ts +34 -0
- package/src/structures/Guild.ts +1605 -0
- package/src/structures/GuildAuditLogs.ts +99 -0
- package/src/structures/GuildAuditLogsEntry.ts +557 -0
- package/src/structures/GuildBan.ts +65 -0
- package/src/structures/GuildChannel.ts +515 -0
- package/src/structures/GuildEmoji.ts +159 -0
- package/src/structures/GuildInvite.ts +233 -0
- package/src/structures/GuildMember.ts +643 -0
- package/src/structures/GuildOnboarding.ts +73 -0
- package/src/structures/GuildOnboardingPrompt.ts +93 -0
- package/src/structures/GuildOnboardingPromptOption.ts +100 -0
- package/src/structures/GuildPreview.ts +218 -0
- package/src/structures/GuildPreviewEmoji.ts +28 -0
- package/src/structures/GuildScheduledEvent.ts +593 -0
- package/src/structures/GuildTemplate.ts +229 -0
- package/src/structures/Integration.ts +257 -0
- package/src/structures/IntegrationApplication.ts +84 -0
- package/src/structures/InteractionCallback.ts +86 -0
- package/src/structures/InteractionCallbackResource.ts +57 -0
- package/src/structures/InteractionCallbackResponse.ts +35 -0
- package/src/structures/InteractionCollector.ts +260 -0
- package/src/structures/InteractionWebhook.ts +65 -0
- package/src/structures/InviteGuild.ts +22 -0
- package/src/structures/LabelComponent.ts +51 -0
- package/src/structures/MediaChannel.ts +8 -0
- package/src/structures/MediaGalleryComponent.ts +31 -0
- package/src/structures/MediaGalleryItem.ts +54 -0
- package/src/structures/MentionableSelectMenuComponent.ts +8 -0
- package/src/structures/MentionableSelectMenuInteraction.ts +76 -0
- package/src/structures/Message.ts +1202 -0
- package/src/structures/MessageCollector.ts +155 -0
- package/src/structures/MessageComponentInteraction.ts +132 -0
- package/src/structures/MessageContextMenuCommandInteraction.ts +18 -0
- package/src/structures/MessageMentions.ts +327 -0
- package/src/structures/MessagePayload.ts +325 -0
- package/src/structures/MessageReaction.ts +219 -0
- package/src/structures/ModalComponentResolver.ts +237 -0
- package/src/structures/ModalSubmitInteraction.ts +260 -0
- package/src/structures/OAuth2Guild.ts +30 -0
- package/src/structures/PartialGroupDMChannel.ts +151 -0
- package/src/structures/PermissionOverwrites.ts +222 -0
- package/src/structures/Poll.ts +195 -0
- package/src/structures/PollAnswer.ts +100 -0
- package/src/structures/Presence.ts +448 -0
- package/src/structures/PrimaryEntryPointCommandInteraction.ts +8 -0
- package/src/structures/ReactionCollector.ts +245 -0
- package/src/structures/ReactionEmoji.ts +30 -0
- package/src/structures/Role.ts +543 -0
- package/src/structures/RoleSelectMenuComponent.ts +8 -0
- package/src/structures/RoleSelectMenuInteraction.ts +34 -0
- package/src/structures/SKU.ts +62 -0
- package/src/structures/SectionComponent.ts +44 -0
- package/src/structures/SeparatorComponent.ts +29 -0
- package/src/structures/SoundboardSound.ts +176 -0
- package/src/structures/StageChannel.ts +118 -0
- package/src/structures/StageInstance.ts +173 -0
- package/src/structures/Sticker.ts +300 -0
- package/src/structures/StickerPack.ts +111 -0
- package/src/structures/StringSelectMenuComponent.ts +18 -0
- package/src/structures/StringSelectMenuInteraction.ts +21 -0
- package/src/structures/Subscription.ts +138 -0
- package/src/structures/Team.ts +131 -0
- package/src/structures/TeamMember.ts +77 -0
- package/src/structures/TextChannel.ts +33 -0
- package/src/structures/TextDisplayComponent.ts +18 -0
- package/src/structures/TextInputComponent.ts +28 -0
- package/src/structures/ThreadChannel.ts +680 -0
- package/src/structures/ThreadMember.ts +125 -0
- package/src/structures/ThreadOnlyChannel.ts +276 -0
- package/src/structures/ThumbnailComponent.ts +51 -0
- package/src/structures/Typing.ts +81 -0
- package/src/structures/UnfurledMediaItem.ts +33 -0
- package/src/structures/User.ts +511 -0
- package/src/structures/UserContextMenuCommandInteraction.ts +28 -0
- package/src/structures/UserSelectMenuComponent.ts +8 -0
- package/src/structures/UserSelectMenuInteraction.ts +54 -0
- package/src/structures/VoiceChannel.ts +119 -0
- package/src/structures/VoiceChannelEffect.ts +92 -0
- package/src/structures/VoiceRegion.ts +52 -0
- package/src/structures/VoiceState.ts +346 -0
- package/src/structures/Webhook.ts +526 -0
- package/src/structures/WelcomeChannel.ts +67 -0
- package/src/structures/WelcomeScreen.ts +53 -0
- package/src/structures/Widget.ts +110 -0
- package/src/structures/WidgetMember.ts +124 -0
- package/src/structures/interfaces/Application.ts +179 -0
- package/src/structures/interfaces/Collector.ts +372 -0
- package/src/structures/interfaces/InteractionResponses.ts +384 -0
- package/src/structures/interfaces/TextBasedChannel.ts +430 -0
- package/src/util/APITypes.ts +690 -0
- package/src/util/ActivityFlagsBitField.ts +26 -0
- package/src/util/ApplicationFlagsBitField.ts +43 -0
- package/src/util/AttachmentFlagsBitField.ts +26 -0
- package/src/util/BitField.ts +178 -0
- package/src/util/ChannelFlagsBitField.ts +43 -0
- package/src/util/Channels.ts +165 -0
- package/src/util/Colors.ts +72 -0
- package/src/util/Components.ts +285 -0
- package/src/util/Constants.ts +251 -0
- package/src/util/DataResolver.ts +146 -0
- package/src/util/Enums.ts +10 -0
- package/src/util/Events.ts +178 -0
- package/src/util/GuildMemberFlagsBitField.ts +43 -0
- package/src/util/IntentsBitField.ts +35 -0
- package/src/util/InviteFlagsBitField.ts +26 -0
- package/src/util/Invites.ts +27 -0
- package/src/util/LimitedCollection.ts +73 -0
- package/src/util/MessageFlagsBitField.ts +43 -0
- package/src/util/Options.ts +215 -0
- package/src/util/Partials.ts +50 -0
- package/src/util/PermissionsBitField.ts +113 -0
- package/src/util/RoleFlagsBitField.ts +26 -0
- package/src/util/SKUFlagsBitField.ts +26 -0
- package/src/util/ShardEvents.ts +26 -0
- package/src/util/Status.ts +16 -0
- package/src/util/Sweepers.ts +532 -0
- package/src/util/Symbols.ts +1 -0
- package/src/util/SystemChannelFlagsBitField.ts +45 -0
- package/src/util/ThreadMemberFlagsBitField.ts +33 -0
- package/src/util/Transformers.ts +123 -0
- package/src/util/UserFlagsBitField.ts +33 -0
- package/src/util/Util.ts +519 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { ChannelType, Routes } from 'discord-api-types/v10';
|
|
2
|
+
import { DiscordjsError, ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import { GuildMember } from '../structures/GuildMember.js';
|
|
4
|
+
import { Message } from '../structures/Message.js';
|
|
5
|
+
import { ThreadMember } from '../structures/ThreadMember.js';
|
|
6
|
+
import { User } from '../structures/User.js';
|
|
7
|
+
import { CachedManager } from './CachedManager.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Manages API methods for users and stores their cache.
|
|
11
|
+
*
|
|
12
|
+
* @extends {CachedManager}
|
|
13
|
+
*/
|
|
14
|
+
export class UserManager extends CachedManager {
|
|
15
|
+
public client: any;
|
|
16
|
+
constructor(client, iterable) {
|
|
17
|
+
super(client, User, iterable);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The cache of this manager
|
|
22
|
+
*
|
|
23
|
+
* @type {Collection<Snowflake, User>}
|
|
24
|
+
* @name UserManager#cache
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Data that resolves to give a User object. This can be:
|
|
29
|
+
* - A User object
|
|
30
|
+
* - A Snowflake
|
|
31
|
+
* - A Message object (resolves to the message author)
|
|
32
|
+
* - A GuildMember object
|
|
33
|
+
* - A ThreadMember object
|
|
34
|
+
*
|
|
35
|
+
* @typedef {User|Snowflake|Message|GuildMember|ThreadMember} UserResolvable
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The DM between the client's user and a user
|
|
40
|
+
*
|
|
41
|
+
* @param {Snowflake} userId The user id
|
|
42
|
+
* @returns {?DMChannel}
|
|
43
|
+
* @private
|
|
44
|
+
*/
|
|
45
|
+
dmChannel(userId) {
|
|
46
|
+
return (
|
|
47
|
+
this.client.channels.cache.find(channel => channel.type === ChannelType.DM && channel.recipientId === userId) ??
|
|
48
|
+
null
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Creates a {@link DMChannel} between the client and a user.
|
|
54
|
+
*
|
|
55
|
+
* @param {UserResolvable} user The UserResolvable to identify
|
|
56
|
+
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
57
|
+
* @returns {Promise<DMChannel>}
|
|
58
|
+
*/
|
|
59
|
+
async createDM(user, { cache = true, force = false } = {}) {
|
|
60
|
+
const id = this.resolveId(user);
|
|
61
|
+
|
|
62
|
+
if (!force) {
|
|
63
|
+
const dmChannel = this.dmChannel(id);
|
|
64
|
+
if (dmChannel && !dmChannel.partial) return dmChannel;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const data = await this.client.rest.post(Routes.userChannels(), { body: { recipient_id: id } });
|
|
68
|
+
return this.client.channels._add(data, null, { cache });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Deletes a {@link DMChannel} (if one exists) between the client and a user. Resolves with the channel if successful.
|
|
73
|
+
*
|
|
74
|
+
* @param {UserResolvable} user The UserResolvable to identify
|
|
75
|
+
* @returns {Promise<DMChannel>}
|
|
76
|
+
*/
|
|
77
|
+
async deleteDM(user) {
|
|
78
|
+
const id = this.resolveId(user);
|
|
79
|
+
const dmChannel = this.dmChannel(id);
|
|
80
|
+
if (!dmChannel) throw new DiscordjsError(ErrorCodes.UserNoDMChannel);
|
|
81
|
+
await this.client.rest.delete(Routes.channel(dmChannel.id));
|
|
82
|
+
this.client.channels._remove(dmChannel.id);
|
|
83
|
+
return dmChannel;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Obtains a user from Discord, or the user cache if it's already available.
|
|
88
|
+
*
|
|
89
|
+
* @param {UserResolvable} user The user to fetch
|
|
90
|
+
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
91
|
+
* @returns {Promise<User>}
|
|
92
|
+
*/
|
|
93
|
+
async fetch(user, { cache = true, force = false } = {}) {
|
|
94
|
+
const id = this.resolveId(user);
|
|
95
|
+
if (!force) {
|
|
96
|
+
const existing = this.cache.get(id);
|
|
97
|
+
if (existing && !existing.partial) return existing;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const data = await this.client.rest.get(Routes.user(id));
|
|
101
|
+
return this._add(data, cache);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Sends a message to a user.
|
|
106
|
+
*
|
|
107
|
+
* @param {UserResolvable} user The UserResolvable to identify
|
|
108
|
+
* @param {string|MessagePayload|MessageCreateOptions} options The options to provide
|
|
109
|
+
* @returns {Promise<Message>}
|
|
110
|
+
*/
|
|
111
|
+
async send(user, options) {
|
|
112
|
+
return (await this.createDM(user)).send(options);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Resolves a {@link UserResolvable} to a {@link User} object.
|
|
117
|
+
*
|
|
118
|
+
* @param {UserResolvable} user The UserResolvable to identify
|
|
119
|
+
* @returns {?User}
|
|
120
|
+
*/
|
|
121
|
+
resolve(user) {
|
|
122
|
+
if (user instanceof GuildMember || user instanceof ThreadMember) return user.user;
|
|
123
|
+
if (user instanceof Message) return user.author;
|
|
124
|
+
return super.resolve(user);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Resolves a {@link UserResolvable} to a {@link User} id.
|
|
129
|
+
*
|
|
130
|
+
* @param {UserResolvable} user The UserResolvable to identify
|
|
131
|
+
* @returns {?Snowflake}
|
|
132
|
+
*/
|
|
133
|
+
resolveId(user) {
|
|
134
|
+
if (user instanceof ThreadMember) return user.id;
|
|
135
|
+
if (user instanceof GuildMember) return user.user.id;
|
|
136
|
+
if (user instanceof Message) return user.author.id;
|
|
137
|
+
return super.resolveId(user);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Routes } from 'discord-api-types/v10';
|
|
2
|
+
import { VoiceState } from '../structures/VoiceState.js';
|
|
3
|
+
import { CachedManager } from './CachedManager.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Manages API methods for VoiceStates and stores their cache.
|
|
7
|
+
*
|
|
8
|
+
* @extends {CachedManager}
|
|
9
|
+
*/
|
|
10
|
+
export class VoiceStateManager extends CachedManager {
|
|
11
|
+
public guild: any;
|
|
12
|
+
constructor(guild, iterable) {
|
|
13
|
+
super(guild.client, VoiceState, iterable);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The guild this manager belongs to
|
|
17
|
+
*
|
|
18
|
+
* @type {Guild}
|
|
19
|
+
*/
|
|
20
|
+
this.guild = guild;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The cache of this manager
|
|
25
|
+
*
|
|
26
|
+
* @type {Collection<Snowflake, VoiceState>}
|
|
27
|
+
* @name VoiceStateManager#cache
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
_add(data, cache = true) {
|
|
31
|
+
const existing = this.cache.get(data.user_id);
|
|
32
|
+
if (existing) return existing._patch(data);
|
|
33
|
+
|
|
34
|
+
const entry = new this.holds(this.guild, data);
|
|
35
|
+
if (cache) this.cache.set(data.user_id, entry);
|
|
36
|
+
return entry;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Obtains a user's voice state from discord or from the cache if it's already available.
|
|
41
|
+
*
|
|
42
|
+
* @param {UserResolvable|'@me'} member The member whose voice state is to be fetched
|
|
43
|
+
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
44
|
+
* @returns {Promise<VoiceState>}
|
|
45
|
+
* @example
|
|
46
|
+
* // Fetch a member's voice state
|
|
47
|
+
* guild.voiceStates.fetch("66564597481480192")
|
|
48
|
+
* .then(console.log)
|
|
49
|
+
* .catch(console.error);
|
|
50
|
+
*/
|
|
51
|
+
async fetch(member, { cache = true, force = false } = {}) {
|
|
52
|
+
const id = member === '@me' ? member : this.guild.members.resolveId(member);
|
|
53
|
+
if (!force) {
|
|
54
|
+
const existing = this.cache.get(id === '@me' ? this.client.user.id : id);
|
|
55
|
+
if (existing) return existing;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const data = await this.client.rest.get(Routes.guildVoiceState(this.guild.id, id));
|
|
59
|
+
return this._add(data, cache);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
/* eslint-disable no-use-before-define */
|
|
2
|
+
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { setTimeout, clearTimeout } from 'node:timers';
|
|
6
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
7
|
+
import { SHARE_ENV } from 'node:worker_threads';
|
|
8
|
+
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
|
9
|
+
import { DiscordjsError, ErrorCodes } from '../errors/index.js';
|
|
10
|
+
import { ShardEvents } from '../util/ShardEvents.js';
|
|
11
|
+
import { makeError, makePlainError } from '../util/Util.js';
|
|
12
|
+
|
|
13
|
+
let childProcess = null;
|
|
14
|
+
let Worker = null;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
|
|
18
|
+
* an instance of the bot and its {@link Client}. When its child process/worker exits for any reason, the shard will
|
|
19
|
+
* spawn a new one to replace it as necessary.
|
|
20
|
+
*
|
|
21
|
+
* @extends {AsyncEventEmitter}
|
|
22
|
+
*/
|
|
23
|
+
export class Shard extends AsyncEventEmitter {
|
|
24
|
+
public manager: any;
|
|
25
|
+
public id: any;
|
|
26
|
+
public silent: any;
|
|
27
|
+
public args: any;
|
|
28
|
+
public execArgv: any;
|
|
29
|
+
public env: any;
|
|
30
|
+
public worker: any;
|
|
31
|
+
public _evals: any;
|
|
32
|
+
public _fetches: any;
|
|
33
|
+
public _exitListener: any;
|
|
34
|
+
constructor(manager, id) {
|
|
35
|
+
super();
|
|
36
|
+
|
|
37
|
+
switch (manager.mode) {
|
|
38
|
+
case 'process':
|
|
39
|
+
childProcess = require('node:child_process');
|
|
40
|
+
break;
|
|
41
|
+
case 'worker':
|
|
42
|
+
Worker = require('node:worker_threads').Worker;
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
throw new Error(`Invalid sharding mode in Shard ${id}`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Manager that created the shard
|
|
50
|
+
*
|
|
51
|
+
* @type {ShardingManager}
|
|
52
|
+
*/
|
|
53
|
+
this.manager = manager;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The shard's id in the manager
|
|
57
|
+
*
|
|
58
|
+
* @type {number}
|
|
59
|
+
*/
|
|
60
|
+
this.id = id;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether to pass silent flag to the shard's process (only when {@link ShardingManager#mode} is `process`)
|
|
64
|
+
*
|
|
65
|
+
* @type {boolean}
|
|
66
|
+
*/
|
|
67
|
+
this.silent = manager.silent;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Arguments for the shard's process/worker
|
|
71
|
+
*
|
|
72
|
+
* @type {string[]}
|
|
73
|
+
*/
|
|
74
|
+
this.args = manager.shardArgs ?? [];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Arguments for the shard's process/worker executable
|
|
78
|
+
*
|
|
79
|
+
* @type {string[]}
|
|
80
|
+
*/
|
|
81
|
+
this.execArgv = manager.execArgv;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Environment variables for the shard's process, or workerData for the shard's worker
|
|
85
|
+
*
|
|
86
|
+
* @type {Object}
|
|
87
|
+
*/
|
|
88
|
+
this.env = {
|
|
89
|
+
...process.env,
|
|
90
|
+
SHARDING_MANAGER: true,
|
|
91
|
+
SHARDS: this.id,
|
|
92
|
+
SHARD_COUNT: this.manager.totalShards,
|
|
93
|
+
DISCORD_TOKEN: this.manager.token,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Whether the shard's {@link Client} is ready
|
|
98
|
+
*
|
|
99
|
+
* @type {boolean}
|
|
100
|
+
*/
|
|
101
|
+
this.ready = false;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Process of the shard (if {@link ShardingManager#mode} is `process`)
|
|
105
|
+
*
|
|
106
|
+
* @type {?ChildProcess}
|
|
107
|
+
*/
|
|
108
|
+
this.process = null;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Worker of the shard (if {@link ShardingManager#mode} is `worker`)
|
|
112
|
+
*
|
|
113
|
+
* @type {?Worker}
|
|
114
|
+
*/
|
|
115
|
+
this.worker = null;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Ongoing promises for calls to {@link Shard#eval}, mapped by the `script` they were called with
|
|
119
|
+
*
|
|
120
|
+
* @type {Map<string, Promise>}
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
this._evals = new Map();
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Ongoing promises for calls to {@link Shard#fetchClientValue}, mapped by the `prop` they were called with
|
|
127
|
+
*
|
|
128
|
+
* @type {Map<string, Promise>}
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
this._fetches = new Map();
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Listener function for the {@link ChildProcess}' `exit` event
|
|
135
|
+
*
|
|
136
|
+
* @type {Function}
|
|
137
|
+
* @private
|
|
138
|
+
*/
|
|
139
|
+
this._exitListener = null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Forks a child process or creates a worker thread for the shard.
|
|
144
|
+
* <warn>You should not need to call this manually.</warn>
|
|
145
|
+
*
|
|
146
|
+
* @param {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client} has become ready
|
|
147
|
+
* before resolving (`-1` or `Infinity` for no wait)
|
|
148
|
+
* @returns {Promise<ChildProcess>}
|
|
149
|
+
*/
|
|
150
|
+
async spawn(timeout = 30_000) {
|
|
151
|
+
if (this.process) throw new DiscordjsError(ErrorCodes.ShardingProcessExists, this.id);
|
|
152
|
+
if (this.worker) throw new DiscordjsError(ErrorCodes.ShardingWorkerExists, this.id);
|
|
153
|
+
|
|
154
|
+
this._exitListener = this._handleExit.bind(this, undefined, timeout);
|
|
155
|
+
|
|
156
|
+
switch (this.manager.mode) {
|
|
157
|
+
case 'process':
|
|
158
|
+
this.process = childProcess
|
|
159
|
+
.fork(path.resolve(this.manager.file), this.args, {
|
|
160
|
+
env: this.env,
|
|
161
|
+
execArgv: this.execArgv,
|
|
162
|
+
silent: this.silent,
|
|
163
|
+
})
|
|
164
|
+
.on('message', this._handleMessage.bind(this))
|
|
165
|
+
.on('exit', this._exitListener);
|
|
166
|
+
break;
|
|
167
|
+
case 'worker':
|
|
168
|
+
this.worker = new Worker(path.resolve(this.manager.file), {
|
|
169
|
+
workerData: this.env,
|
|
170
|
+
env: SHARE_ENV,
|
|
171
|
+
execArgv: this.execArgv,
|
|
172
|
+
argv: this.args,
|
|
173
|
+
})
|
|
174
|
+
.on('message', this._handleMessage.bind(this))
|
|
175
|
+
.on('exit', this._exitListener);
|
|
176
|
+
break;
|
|
177
|
+
default:
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
this._evals.clear();
|
|
182
|
+
this._fetches.clear();
|
|
183
|
+
|
|
184
|
+
const child = this.process ?? this.worker;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Emitted upon the creation of the shard's child process/worker.
|
|
188
|
+
*
|
|
189
|
+
* @event Shard#spawn
|
|
190
|
+
* @param {ChildProcess|Worker} process Child process/worker that was created
|
|
191
|
+
*/
|
|
192
|
+
this.emit(ShardEvents.Spawn, child);
|
|
193
|
+
|
|
194
|
+
if (timeout === -1 || timeout === Infinity) return child;
|
|
195
|
+
return new Promise((resolve, reject) => {
|
|
196
|
+
const cleanup = () => {
|
|
197
|
+
clearTimeout(spawnTimeoutTimer);
|
|
198
|
+
this.off('ready', onReady);
|
|
199
|
+
this.off('disconnect', onDisconnect);
|
|
200
|
+
this.off('death', onDeath);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const onReady = () => {
|
|
204
|
+
cleanup();
|
|
205
|
+
resolve(child);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const onDisconnect = () => {
|
|
209
|
+
cleanup();
|
|
210
|
+
reject(new DiscordjsError(ErrorCodes.ShardingReadyDisconnected, this.id));
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const onDeath = () => {
|
|
214
|
+
cleanup();
|
|
215
|
+
reject(new DiscordjsError(ErrorCodes.ShardingReadyDied, this.id));
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const onTimeout = () => {
|
|
219
|
+
cleanup();
|
|
220
|
+
reject(new DiscordjsError(ErrorCodes.ShardingReadyTimeout, this.id));
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const spawnTimeoutTimer = setTimeout(onTimeout, timeout);
|
|
224
|
+
this.once('ready', onReady);
|
|
225
|
+
this.once('disconnect', onDisconnect);
|
|
226
|
+
this.once('death', onDeath);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Immediately kills the shard's process/worker and does not restart it.
|
|
232
|
+
*/
|
|
233
|
+
kill() {
|
|
234
|
+
if (this.process) {
|
|
235
|
+
this.process.removeListener('exit', this._exitListener);
|
|
236
|
+
this.process.kill();
|
|
237
|
+
} else {
|
|
238
|
+
this.worker.removeListener('exit', this._exitListener);
|
|
239
|
+
this.worker.terminate();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
this._handleExit(false);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Options used to respawn a shard.
|
|
247
|
+
*
|
|
248
|
+
* @typedef {Object} ShardRespawnOptions
|
|
249
|
+
* @property {number} [delay=500] How long to wait between killing the process/worker and
|
|
250
|
+
* restarting it (in milliseconds)
|
|
251
|
+
* @property {number} [timeout=30000] The amount in milliseconds to wait until the {@link Client}
|
|
252
|
+
* has become ready before resolving (`-1` or `Infinity` for no wait)
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Kills and restarts the shard's process/worker.
|
|
257
|
+
*
|
|
258
|
+
* @param {ShardRespawnOptions} [options] Options for respawning the shard
|
|
259
|
+
* @returns {Promise<ChildProcess>}
|
|
260
|
+
*/
|
|
261
|
+
async respawn({ delay = 500, timeout = 30_000 } = {}) {
|
|
262
|
+
this.kill();
|
|
263
|
+
if (delay > 0) await sleep(delay);
|
|
264
|
+
return this.spawn(timeout);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Sends a message to the shard's process/worker.
|
|
269
|
+
*
|
|
270
|
+
* @param {*} message Message to send to the shard
|
|
271
|
+
* @returns {Promise<Shard>}
|
|
272
|
+
*/
|
|
273
|
+
async send(message) {
|
|
274
|
+
return new Promise((resolve, reject) => {
|
|
275
|
+
if (this.process) {
|
|
276
|
+
this.process.send(message, err => {
|
|
277
|
+
if (err) reject(err);
|
|
278
|
+
else resolve(this);
|
|
279
|
+
});
|
|
280
|
+
} else {
|
|
281
|
+
this.worker.postMessage(message);
|
|
282
|
+
resolve(this);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Fetches a client property value of the shard.
|
|
289
|
+
*
|
|
290
|
+
* @param {string} prop Name of the client property to get, using periods for nesting
|
|
291
|
+
* @returns {Promise<*>}
|
|
292
|
+
* @example
|
|
293
|
+
* shard.fetchClientValue('guilds.cache.size')
|
|
294
|
+
* .then(count => console.log(`${count} guilds in shard ${shard.id}`))
|
|
295
|
+
* .catch(console.error);
|
|
296
|
+
*/
|
|
297
|
+
async fetchClientValue(prop) {
|
|
298
|
+
// Shard is dead (maybe respawning), don't cache anything and error immediately
|
|
299
|
+
if (!this.process && !this.worker) {
|
|
300
|
+
throw new DiscordjsError(ErrorCodes.ShardingNoChildExists, this.id);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Cached promise from previous call
|
|
304
|
+
if (this._fetches.has(prop)) return this._fetches.get(prop);
|
|
305
|
+
|
|
306
|
+
const promise = new Promise((resolve, reject) => {
|
|
307
|
+
const child = this.process ?? this.worker;
|
|
308
|
+
|
|
309
|
+
const listener = message => {
|
|
310
|
+
if (message?._fetchProp !== prop) return;
|
|
311
|
+
child.removeListener('message', listener);
|
|
312
|
+
this.decrementMaxListeners(child);
|
|
313
|
+
this._fetches.delete(prop);
|
|
314
|
+
if (message._error) reject(makeError(message._error));
|
|
315
|
+
else resolve(message._result);
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
this.incrementMaxListeners(child);
|
|
319
|
+
child.on('message', listener);
|
|
320
|
+
|
|
321
|
+
this.send({ _fetchProp: prop }).catch(error => {
|
|
322
|
+
child.removeListener('message', listener);
|
|
323
|
+
this.decrementMaxListeners(child);
|
|
324
|
+
this._fetches.delete(prop);
|
|
325
|
+
reject(error);
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
this._fetches.set(prop, promise);
|
|
330
|
+
return promise;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Evaluates a script or function on the shard, in the context of the {@link Client}.
|
|
335
|
+
*
|
|
336
|
+
* @param {string|Function} script JavaScript to run on the shard
|
|
337
|
+
* @param {*} [context] The context for the eval
|
|
338
|
+
* @returns {Promise<*>} Result of the script execution
|
|
339
|
+
*/
|
|
340
|
+
async eval(script, context) {
|
|
341
|
+
// Stringify the script if it's a Function
|
|
342
|
+
const _eval = typeof script === 'function' ? `(${script})(this, ${JSON.stringify(context)})` : script;
|
|
343
|
+
|
|
344
|
+
// Shard is dead (maybe respawning), don't cache anything and error immediately
|
|
345
|
+
if (!this.process && !this.worker) {
|
|
346
|
+
throw new DiscordjsError(ErrorCodes.ShardingNoChildExists, this.id);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Cached promise from previous call
|
|
350
|
+
if (this._evals.has(_eval)) return this._evals.get(_eval);
|
|
351
|
+
|
|
352
|
+
const promise = new Promise((resolve, reject) => {
|
|
353
|
+
const child = this.process ?? this.worker;
|
|
354
|
+
|
|
355
|
+
const listener = message => {
|
|
356
|
+
if (message?._eval !== _eval) return;
|
|
357
|
+
child.removeListener('message', listener);
|
|
358
|
+
this.decrementMaxListeners(child);
|
|
359
|
+
this._evals.delete(_eval);
|
|
360
|
+
if (message._error) reject(makeError(message._error));
|
|
361
|
+
else resolve(message._result);
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
this.incrementMaxListeners(child);
|
|
365
|
+
child.on('message', listener);
|
|
366
|
+
|
|
367
|
+
this.send({ _eval }).catch(error => {
|
|
368
|
+
child.removeListener('message', listener);
|
|
369
|
+
this.decrementMaxListeners(child);
|
|
370
|
+
this._evals.delete(_eval);
|
|
371
|
+
reject(error);
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
this._evals.set(_eval, promise);
|
|
376
|
+
return promise;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Handles a message received from the child process/worker.
|
|
381
|
+
*
|
|
382
|
+
* @param {*} message Message received
|
|
383
|
+
* @private
|
|
384
|
+
*/
|
|
385
|
+
_handleMessage(message) {
|
|
386
|
+
if (message) {
|
|
387
|
+
// Shard is ready
|
|
388
|
+
if (message._ready) {
|
|
389
|
+
this.ready = true;
|
|
390
|
+
/**
|
|
391
|
+
* Emitted upon the shard's {@link Client#event:clientReady} event.
|
|
392
|
+
*
|
|
393
|
+
* @event Shard#ready
|
|
394
|
+
*/
|
|
395
|
+
this.emit(ShardEvents.Ready);
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Shard has disconnected
|
|
400
|
+
if (message._disconnect) {
|
|
401
|
+
this.ready = false;
|
|
402
|
+
/**
|
|
403
|
+
* Emitted upon the shard's {@link WebSocketShardEvents#Closed} event.
|
|
404
|
+
*
|
|
405
|
+
* @event Shard#disconnect
|
|
406
|
+
*/
|
|
407
|
+
this.emit(ShardEvents.Disconnect);
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Shard has resumed
|
|
412
|
+
if (message._resume) {
|
|
413
|
+
this.ready = true;
|
|
414
|
+
/**
|
|
415
|
+
* Emitted upon the shard's {@link WebSocketShardEvents#Resumed} event.
|
|
416
|
+
*
|
|
417
|
+
* @event Shard#resume
|
|
418
|
+
*/
|
|
419
|
+
this.emit(ShardEvents.Resume);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Shard is requesting a property fetch
|
|
424
|
+
if (message._sFetchProp) {
|
|
425
|
+
const resp = { _sFetchProp: message._sFetchProp, _sFetchPropShard: message._sFetchPropShard };
|
|
426
|
+
this.manager.fetchClientValues(message._sFetchProp, message._sFetchPropShard).then(
|
|
427
|
+
async results => this.send({ ...resp, _result: results }),
|
|
428
|
+
async error => this.send({ ...resp, _error: makePlainError(error) }),
|
|
429
|
+
);
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Shard is requesting an eval broadcast
|
|
434
|
+
if (message._sEval) {
|
|
435
|
+
const resp = { _sEval: message._sEval, _sEvalShard: message._sEvalShard };
|
|
436
|
+
this.manager._performOnShards('eval', [message._sEval], message._sEvalShard).then(
|
|
437
|
+
async results => this.send({ ...resp, _result: results }),
|
|
438
|
+
async error => this.send({ ...resp, _error: makePlainError(error) }),
|
|
439
|
+
);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Shard is requesting a respawn of all shards
|
|
444
|
+
if (message._sRespawnAll) {
|
|
445
|
+
const { shardDelay, respawnDelay, timeout } = message._sRespawnAll;
|
|
446
|
+
this.manager.respawnAll({ shardDelay, respawnDelay, timeout }).catch(() => {
|
|
447
|
+
// Do nothing
|
|
448
|
+
});
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Emitted upon receiving a message from the child process/worker.
|
|
455
|
+
*
|
|
456
|
+
* @event Shard#message
|
|
457
|
+
* @param {*} message Message that was received
|
|
458
|
+
*/
|
|
459
|
+
this.emit(ShardEvents.Message, message);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Handles the shard's process/worker exiting.
|
|
464
|
+
*
|
|
465
|
+
* @param {boolean} [respawn=this.manager.respawn] Whether to spawn the shard again
|
|
466
|
+
* @param {number} [timeout] The amount in milliseconds to wait until the {@link Client}
|
|
467
|
+
* has become ready (`-1` or `Infinity` for no wait)
|
|
468
|
+
* @private
|
|
469
|
+
*/
|
|
470
|
+
_handleExit(respawn = this.manager.respawn, timeout = undefined) {
|
|
471
|
+
/**
|
|
472
|
+
* Emitted upon the shard's child process/worker exiting.
|
|
473
|
+
*
|
|
474
|
+
* @event Shard#death
|
|
475
|
+
* @param {ChildProcess|Worker} process Child process/worker that exited
|
|
476
|
+
*/
|
|
477
|
+
this.emit(ShardEvents.Death, this.process ?? this.worker);
|
|
478
|
+
|
|
479
|
+
this.ready = false;
|
|
480
|
+
this.process = null;
|
|
481
|
+
this.worker = null;
|
|
482
|
+
this._evals.clear();
|
|
483
|
+
this._fetches.clear();
|
|
484
|
+
|
|
485
|
+
if (respawn) this.spawn(timeout).catch(error => this.emit(ShardEvents.Error, error));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Increments max listeners by one for a given emitter, if they are not zero.
|
|
490
|
+
*
|
|
491
|
+
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
|
|
492
|
+
* @private
|
|
493
|
+
*/
|
|
494
|
+
incrementMaxListeners(emitter) {
|
|
495
|
+
const maxListeners = emitter.getMaxListeners();
|
|
496
|
+
if (maxListeners !== 0) {
|
|
497
|
+
emitter.setMaxListeners(maxListeners + 1);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Decrements max listeners by one for a given emitter, if they are not zero.
|
|
503
|
+
*
|
|
504
|
+
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
|
|
505
|
+
* @private
|
|
506
|
+
*/
|
|
507
|
+
decrementMaxListeners(emitter) {
|
|
508
|
+
const maxListeners = emitter.getMaxListeners();
|
|
509
|
+
if (maxListeners !== 0) {
|
|
510
|
+
emitter.setMaxListeners(maxListeners - 1);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|