@velliajs/discord 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/README.md +133 -0
- package/package.json +82 -0
- package/src/client/BaseClient.js +83 -0
- package/src/client/Client.js +591 -0
- package/src/client/WebhookClient.js +103 -0
- package/src/client/actions/Action.js +119 -0
- package/src/client/actions/ActionsManager.js +79 -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 +23 -0
- package/src/client/actions/ChannelUpdate.js +33 -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 +44 -0
- package/src/client/actions/GuildEmojiCreate.js +20 -0
- package/src/client/actions/GuildEmojiDelete.js +19 -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 +31 -0
- package/src/client/actions/GuildMemberUpdate.js +44 -0
- package/src/client/actions/GuildRoleCreate.js +25 -0
- package/src/client/actions/GuildRoleDelete.js +29 -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 +19 -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 +101 -0
- package/src/client/actions/InviteCreate.js +28 -0
- package/src/client/actions/InviteDelete.js +30 -0
- package/src/client/actions/MessageCreate.js +37 -0
- package/src/client/actions/MessageDelete.js +32 -0
- package/src/client/actions/MessageDeleteBulk.js +47 -0
- package/src/client/actions/MessageReactionAdd.js +55 -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 +42 -0
- package/src/client/actions/StageInstanceCreate.js +28 -0
- package/src/client/actions/StageInstanceDelete.js +31 -0
- package/src/client/actions/StageInstanceUpdate.js +30 -0
- package/src/client/actions/ThreadCreate.js +24 -0
- package/src/client/actions/ThreadDelete.js +26 -0
- package/src/client/actions/ThreadListSync.js +60 -0
- package/src/client/actions/ThreadMemberUpdate.js +30 -0
- package/src/client/actions/ThreadMembersUpdate.js +47 -0
- package/src/client/actions/TypingStart.js +29 -0
- package/src/client/actions/UserUpdate.js +36 -0
- package/src/client/actions/VoiceStateUpdate.js +43 -0
- package/src/client/actions/WebhooksUpdate.js +20 -0
- package/src/client/voice/ClientVoiceManager.js +44 -0
- package/src/client/websocket/WebSocketManager.js +393 -0
- package/src/client/websocket/WebSocketShard.js +231 -0
- package/src/client/websocket/handlers/APPLICATION_COMMAND_PERMISSIONS_UPDATE.js +5 -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/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_UPDATE.js +16 -0
- package/src/client/websocket/handlers/GUILD_AUDIT_LOG_ENTRY_CREATE.js +5 -0
- package/src/client/websocket/handlers/GUILD_BAN_ADD.js +5 -0
- package/src/client/websocket/handlers/GUILD_BAN_REMOVE.js +5 -0
- package/src/client/websocket/handlers/GUILD_CREATE.js +26 -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_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 +5 -0
- package/src/client/websocket/handlers/INVITE_CREATE.js +5 -0
- package/src/client/websocket/handlers/INVITE_DELETE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_CREATE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_DELETE_BULK.js +5 -0
- package/src/client/websocket/handlers/MESSAGE_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 +27 -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_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 +64 -0
- package/src/errors/DJSError.js +48 -0
- package/src/errors/ErrorCodes.js +319 -0
- package/src/errors/Messages.js +170 -0
- package/src/errors/index.js +5 -0
- package/src/index.js +213 -0
- package/src/managers/ApplicationCommandManager.js +263 -0
- package/src/managers/ApplicationCommandPermissionsManager.js +434 -0
- package/src/managers/AutoModerationRuleManager.js +288 -0
- package/src/managers/BaseGuildEmojiManager.js +80 -0
- package/src/managers/BaseManager.js +19 -0
- package/src/managers/CachedManager.js +57 -0
- package/src/managers/CategoryChannelChildManager.js +77 -0
- package/src/managers/ChannelManager.js +128 -0
- package/src/managers/DataManager.js +61 -0
- package/src/managers/GuildApplicationCommandManager.js +28 -0
- package/src/managers/GuildBanManager.js +204 -0
- package/src/managers/GuildChannelManager.js +494 -0
- package/src/managers/GuildEmojiManager.js +174 -0
- package/src/managers/GuildEmojiRoleManager.js +118 -0
- package/src/managers/GuildForumThreadManager.js +83 -0
- package/src/managers/GuildInviteManager.js +213 -0
- package/src/managers/GuildManager.js +283 -0
- package/src/managers/GuildMemberManager.js +540 -0
- package/src/managers/GuildMemberRoleManager.js +204 -0
- package/src/managers/GuildScheduledEventManager.js +297 -0
- package/src/managers/GuildStickerManager.js +182 -0
- package/src/managers/GuildTextThreadManager.js +91 -0
- package/src/managers/MessageManager.js +259 -0
- package/src/managers/PermissionOverwriteManager.js +168 -0
- package/src/managers/PresenceManager.js +58 -0
- package/src/managers/ReactionManager.js +68 -0
- package/src/managers/ReactionUserManager.js +77 -0
- package/src/managers/RoleManager.js +360 -0
- package/src/managers/StageInstanceManager.js +154 -0
- package/src/managers/ThreadManager.js +204 -0
- package/src/managers/ThreadMemberManager.js +182 -0
- package/src/managers/UserManager.js +139 -0
- package/src/managers/VoiceStateManager.js +37 -0
- package/src/sharding/Shard.js +457 -0
- package/src/sharding/ShardClientUtil.js +285 -0
- package/src/sharding/ShardingManager.js +326 -0
- package/src/structures/ActionRow.js +46 -0
- package/src/structures/ActionRowBuilder.js +35 -0
- package/src/structures/AnonymousGuild.js +97 -0
- package/src/structures/ApplicationCommand.js +606 -0
- package/src/structures/ApplicationRoleConnectionMetadata.js +46 -0
- package/src/structures/Attachment.js +140 -0
- package/src/structures/AttachmentBuilder.js +116 -0
- package/src/structures/AutoModerationActionExecution.js +116 -0
- package/src/structures/AutoModerationRule.js +284 -0
- package/src/structures/AutocompleteInteraction.js +97 -0
- package/src/structures/Base.js +43 -0
- package/src/structures/BaseChannel.js +160 -0
- package/src/structures/BaseGuild.js +119 -0
- package/src/structures/BaseGuildEmoji.js +56 -0
- package/src/structures/BaseGuildTextChannel.js +186 -0
- package/src/structures/BaseGuildVoiceChannel.js +234 -0
- package/src/structures/BaseInteraction.js +344 -0
- package/src/structures/BaseSelectMenuComponent.js +56 -0
- package/src/structures/ButtonBuilder.js +44 -0
- package/src/structures/ButtonComponent.js +65 -0
- package/src/structures/ButtonInteraction.js +11 -0
- package/src/structures/CategoryChannel.js +45 -0
- package/src/structures/ChannelSelectMenuBuilder.js +31 -0
- package/src/structures/ChannelSelectMenuComponent.js +20 -0
- package/src/structures/ChannelSelectMenuInteraction.js +33 -0
- package/src/structures/ChatInputCommandInteraction.js +41 -0
- package/src/structures/ClientApplication.js +193 -0
- package/src/structures/ClientPresence.js +82 -0
- package/src/structures/ClientUser.js +185 -0
- package/src/structures/CommandInteraction.js +224 -0
- package/src/structures/CommandInteractionOptionResolver.js +308 -0
- package/src/structures/Component.js +47 -0
- package/src/structures/ContextMenuCommandInteraction.js +64 -0
- package/src/structures/DMChannel.js +129 -0
- package/src/structures/DirectoryChannel.js +36 -0
- package/src/structures/Embed.js +220 -0
- package/src/structures/EmbedBuilder.js +41 -0
- package/src/structures/Emoji.js +108 -0
- package/src/structures/ForumChannel.js +264 -0
- package/src/structures/Guild.js +1361 -0
- package/src/structures/GuildAuditLogs.js +91 -0
- package/src/structures/GuildAuditLogsEntry.js +527 -0
- package/src/structures/GuildBan.js +59 -0
- package/src/structures/GuildChannel.js +457 -0
- package/src/structures/GuildEmoji.js +148 -0
- package/src/structures/GuildMember.js +518 -0
- package/src/structures/GuildPreview.js +193 -0
- package/src/structures/GuildPreviewEmoji.js +27 -0
- package/src/structures/GuildScheduledEvent.js +439 -0
- package/src/structures/GuildTemplate.js +241 -0
- package/src/structures/Integration.js +220 -0
- package/src/structures/IntegrationApplication.js +85 -0
- package/src/structures/InteractionCollector.js +263 -0
- package/src/structures/InteractionResponse.js +102 -0
- package/src/structures/InteractionWebhook.js +59 -0
- package/src/structures/Invite.js +322 -0
- package/src/structures/InviteGuild.js +22 -0
- package/src/structures/InviteStageInstance.js +87 -0
- package/src/structures/MentionableSelectMenuBuilder.js +32 -0
- package/src/structures/MentionableSelectMenuComponent.js +11 -0
- package/src/structures/MentionableSelectMenuInteraction.js +71 -0
- package/src/structures/Message.js +997 -0
- package/src/structures/MessageCollector.js +146 -0
- package/src/structures/MessageComponentInteraction.js +107 -0
- package/src/structures/MessageContextMenuCommandInteraction.js +20 -0
- package/src/structures/MessageMentions.js +297 -0
- package/src/structures/MessagePayload.js +299 -0
- package/src/structures/MessageReaction.js +142 -0
- package/src/structures/ModalBuilder.js +34 -0
- package/src/structures/ModalSubmitFields.js +55 -0
- package/src/structures/ModalSubmitInteraction.js +121 -0
- package/src/structures/NewsChannel.js +32 -0
- package/src/structures/OAuth2Guild.js +28 -0
- package/src/structures/PartialGroupDMChannel.js +60 -0
- package/src/structures/PermissionOverwrites.js +196 -0
- package/src/structures/Presence.js +372 -0
- package/src/structures/ReactionCollector.js +229 -0
- package/src/structures/ReactionEmoji.js +31 -0
- package/src/structures/Role.js +458 -0
- package/src/structures/RoleSelectMenuBuilder.js +31 -0
- package/src/structures/RoleSelectMenuComponent.js +11 -0
- package/src/structures/RoleSelectMenuInteraction.js +33 -0
- package/src/structures/SelectMenuBuilder.js +26 -0
- package/src/structures/SelectMenuComponent.js +26 -0
- package/src/structures/SelectMenuInteraction.js +26 -0
- package/src/structures/SelectMenuOptionBuilder.js +26 -0
- package/src/structures/StageChannel.js +112 -0
- package/src/structures/StageInstance.js +167 -0
- package/src/structures/Sticker.js +272 -0
- package/src/structures/StickerPack.js +95 -0
- package/src/structures/StringSelectMenuBuilder.js +79 -0
- package/src/structures/StringSelectMenuComponent.js +20 -0
- package/src/structures/StringSelectMenuInteraction.js +21 -0
- package/src/structures/StringSelectMenuOptionBuilder.js +49 -0
- package/src/structures/Team.js +117 -0
- package/src/structures/TeamMember.js +70 -0
- package/src/structures/TextChannel.js +33 -0
- package/src/structures/TextInputBuilder.js +31 -0
- package/src/structures/TextInputComponent.js +29 -0
- package/src/structures/ThreadChannel.js +606 -0
- package/src/structures/ThreadMember.js +113 -0
- package/src/structures/Typing.js +74 -0
- package/src/structures/User.js +331 -0
- package/src/structures/UserContextMenuCommandInteraction.js +29 -0
- package/src/structures/UserSelectMenuBuilder.js +31 -0
- package/src/structures/UserSelectMenuComponent.js +11 -0
- package/src/structures/UserSelectMenuInteraction.js +51 -0
- package/src/structures/VoiceChannel.js +96 -0
- package/src/structures/VoiceRegion.js +46 -0
- package/src/structures/VoiceState.js +303 -0
- package/src/structures/Webhook.js +481 -0
- package/src/structures/WelcomeChannel.js +60 -0
- package/src/structures/WelcomeScreen.js +49 -0
- package/src/structures/Widget.js +88 -0
- package/src/structures/WidgetMember.js +99 -0
- package/src/structures/interfaces/Application.js +108 -0
- package/src/structures/interfaces/Collector.js +335 -0
- package/src/structures/interfaces/InteractionResponses.js +320 -0
- package/src/structures/interfaces/TextBasedChannel.js +413 -0
- package/src/util/APITypes.js +456 -0
- package/src/util/ActivityFlagsBitField.js +26 -0
- package/src/util/ApplicationFlagsBitField.js +26 -0
- package/src/util/BitField.js +176 -0
- package/src/util/ChannelFlagsBitField.js +41 -0
- package/src/util/Channels.js +150 -0
- package/src/util/Colors.js +73 -0
- package/src/util/Components.js +152 -0
- package/src/util/Constants.js +230 -0
- package/src/util/DataResolver.js +140 -0
- package/src/util/Enums.js +13 -0
- package/src/util/Events.js +160 -0
- package/src/util/Formatters.js +413 -0
- package/src/util/GuildMemberFlagsBitField.js +41 -0
- package/src/util/IntentsBitField.js +34 -0
- package/src/util/LimitedCollection.js +68 -0
- package/src/util/MessageFlagsBitField.js +32 -0
- package/src/util/Options.js +216 -0
- package/src/util/Partials.js +44 -0
- package/src/util/PermissionsBitField.js +104 -0
- package/src/util/ShardEvents.js +27 -0
- package/src/util/Status.js +33 -0
- package/src/util/Sweepers.js +467 -0
- package/src/util/SystemChannelFlagsBitField.js +43 -0
- package/src/util/ThreadMemberFlagsBitField.js +31 -0
- package/src/util/Transformers.js +36 -0
- package/src/util/UserFlagsBitField.js +32 -0
- package/src/util/Util.js +394 -0
- package/src/util/WebSocketShardEvents.js +25 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const process = require('node:process');
|
|
4
|
+
const { Collection } = require('@discordjs/collection');
|
|
5
|
+
const { makeURLSearchParams } = require('@discordjs/rest');
|
|
6
|
+
const { OAuth2Scopes, Routes } = require('discord-api-types/v10');
|
|
7
|
+
const BaseClient = require('./BaseClient');
|
|
8
|
+
const ActionsManager = require('./actions/ActionsManager');
|
|
9
|
+
const ClientVoiceManager = require('./voice/ClientVoiceManager');
|
|
10
|
+
const WebSocketManager = require('./websocket/WebSocketManager');
|
|
11
|
+
const { DiscordjsError, DiscordjsTypeError, DiscordjsRangeError, ErrorCodes } = require('../errors');
|
|
12
|
+
const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
|
|
13
|
+
const ChannelManager = require('../managers/ChannelManager');
|
|
14
|
+
const GuildManager = require('../managers/GuildManager');
|
|
15
|
+
const UserManager = require('../managers/UserManager');
|
|
16
|
+
const ShardClientUtil = require('../sharding/ShardClientUtil');
|
|
17
|
+
const ClientPresence = require('../structures/ClientPresence');
|
|
18
|
+
const GuildPreview = require('../structures/GuildPreview');
|
|
19
|
+
const GuildTemplate = require('../structures/GuildTemplate');
|
|
20
|
+
const Invite = require('../structures/Invite');
|
|
21
|
+
const { Sticker } = require('../structures/Sticker');
|
|
22
|
+
const StickerPack = require('../structures/StickerPack');
|
|
23
|
+
const VoiceRegion = require('../structures/VoiceRegion');
|
|
24
|
+
const Webhook = require('../structures/Webhook');
|
|
25
|
+
const Widget = require('../structures/Widget');
|
|
26
|
+
const DataResolver = require('../util/DataResolver');
|
|
27
|
+
const Events = require('../util/Events');
|
|
28
|
+
const IntentsBitField = require('../util/IntentsBitField');
|
|
29
|
+
const Options = require('../util/Options');
|
|
30
|
+
const PermissionsBitField = require('../util/PermissionsBitField');
|
|
31
|
+
const Status = require('../util/Status');
|
|
32
|
+
const Sweepers = require('../util/Sweepers');
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The main hub for interacting with the Discord API, and the starting point for any bot.
|
|
36
|
+
* @extends {BaseClient}
|
|
37
|
+
*/
|
|
38
|
+
class Client extends BaseClient {
|
|
39
|
+
/**
|
|
40
|
+
* @param {ClientOptions} options Options for the client
|
|
41
|
+
*/
|
|
42
|
+
constructor(options) {
|
|
43
|
+
super(options);
|
|
44
|
+
|
|
45
|
+
const data = require('node:worker_threads').workerData ?? process.env;
|
|
46
|
+
const defaults = Options.createDefault();
|
|
47
|
+
|
|
48
|
+
if (this.options.shards === defaults.shards) {
|
|
49
|
+
if ('SHARDS' in data) {
|
|
50
|
+
this.options.shards = JSON.parse(data.SHARDS);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (this.options.shardCount === defaults.shardCount) {
|
|
55
|
+
if ('SHARD_COUNT' in data) {
|
|
56
|
+
this.options.shardCount = Number(data.SHARD_COUNT);
|
|
57
|
+
} else if (Array.isArray(this.options.shards)) {
|
|
58
|
+
this.options.shardCount = this.options.shards.length;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const typeofShards = typeof this.options.shards;
|
|
63
|
+
|
|
64
|
+
if (typeofShards === 'undefined' && typeof this.options.shardCount === 'number') {
|
|
65
|
+
this.options.shards = Array.from({ length: this.options.shardCount }, (_, i) => i);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (typeofShards === 'number') this.options.shards = [this.options.shards];
|
|
69
|
+
|
|
70
|
+
if (Array.isArray(this.options.shards)) {
|
|
71
|
+
this.options.shards = [
|
|
72
|
+
...new Set(
|
|
73
|
+
this.options.shards.filter(item => !isNaN(item) && item >= 0 && item < Infinity && item === (item | 0)),
|
|
74
|
+
),
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this._validateOptions();
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The WebSocket manager of the client
|
|
82
|
+
* @type {WebSocketManager}
|
|
83
|
+
*/
|
|
84
|
+
this.ws = new WebSocketManager(this);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The action manager of the client
|
|
88
|
+
* @type {ActionsManager}
|
|
89
|
+
* @private
|
|
90
|
+
*/
|
|
91
|
+
this.actions = new ActionsManager(this);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The voice manager of the client
|
|
95
|
+
* @type {ClientVoiceManager}
|
|
96
|
+
*/
|
|
97
|
+
this.voice = new ClientVoiceManager(this);
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Shard helpers for the client (only if the process was spawned from a {@link ShardingManager})
|
|
101
|
+
* @type {?ShardClientUtil}
|
|
102
|
+
*/
|
|
103
|
+
this.shard = process.env.SHARDING_MANAGER
|
|
104
|
+
? ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE)
|
|
105
|
+
: null;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* All of the {@link User} objects that have been cached at any point, mapped by their ids
|
|
109
|
+
* @type {UserManager}
|
|
110
|
+
*/
|
|
111
|
+
this.users = new UserManager(this);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* All of the guilds the client is currently handling, mapped by their ids -
|
|
115
|
+
* as long as sharding isn't being used, this will be *every* guild the bot is a member of
|
|
116
|
+
* @type {GuildManager}
|
|
117
|
+
*/
|
|
118
|
+
this.guilds = new GuildManager(this);
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* All of the {@link BaseChannel}s that the client is currently handling, mapped by their ids -
|
|
122
|
+
* as long as sharding isn't being used, this will be *every* channel in *every* guild the bot
|
|
123
|
+
* is a member of. Note that DM channels will not be initially cached, and thus not be present
|
|
124
|
+
* in the Manager without their explicit fetching or use.
|
|
125
|
+
* @type {ChannelManager}
|
|
126
|
+
*/
|
|
127
|
+
this.channels = new ChannelManager(this);
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The sweeping functions and their intervals used to periodically sweep caches
|
|
131
|
+
* @type {Sweepers}
|
|
132
|
+
*/
|
|
133
|
+
this.sweepers = new Sweepers(this, this.options.sweepers);
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The presence of the Client
|
|
137
|
+
* @private
|
|
138
|
+
* @type {ClientPresence}
|
|
139
|
+
*/
|
|
140
|
+
this.presence = new ClientPresence(this, this.options.presence);
|
|
141
|
+
|
|
142
|
+
Object.defineProperty(this, 'token', { writable: true });
|
|
143
|
+
if (!this.token && 'DISCORD_TOKEN' in process.env) {
|
|
144
|
+
/**
|
|
145
|
+
* Authorization token for the logged in bot.
|
|
146
|
+
* If present, this defaults to `process.env.DISCORD_TOKEN` when instantiating the client
|
|
147
|
+
* <warn>This should be kept private at all times.</warn>
|
|
148
|
+
* @type {?string}
|
|
149
|
+
*/
|
|
150
|
+
this.token = process.env.DISCORD_TOKEN;
|
|
151
|
+
} else {
|
|
152
|
+
this.token = null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* User that the client is logged in as
|
|
157
|
+
* @type {?ClientUser}
|
|
158
|
+
*/
|
|
159
|
+
this.user = null;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The application of this bot
|
|
163
|
+
* @type {?ClientApplication}
|
|
164
|
+
*/
|
|
165
|
+
this.application = null;
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Timestamp of the time the client was last {@link Status.Ready} at
|
|
169
|
+
* @type {?number}
|
|
170
|
+
*/
|
|
171
|
+
this.readyTimestamp = null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* All custom emojis that the client has access to, mapped by their ids
|
|
176
|
+
* @type {BaseGuildEmojiManager}
|
|
177
|
+
* @readonly
|
|
178
|
+
*/
|
|
179
|
+
get emojis() {
|
|
180
|
+
const emojis = new BaseGuildEmojiManager(this);
|
|
181
|
+
for (const guild of this.guilds.cache.values()) {
|
|
182
|
+
if (guild.available) for (const emoji of guild.emojis.cache.values()) emojis.cache.set(emoji.id, emoji);
|
|
183
|
+
}
|
|
184
|
+
return emojis;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Time at which the client was last regarded as being in the {@link Status.Ready} state
|
|
189
|
+
* (each time the client disconnects and successfully reconnects, this will be overwritten)
|
|
190
|
+
* @type {?Date}
|
|
191
|
+
* @readonly
|
|
192
|
+
*/
|
|
193
|
+
get readyAt() {
|
|
194
|
+
return this.readyTimestamp && new Date(this.readyTimestamp);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* How long it has been since the client last entered the {@link Status.Ready} state in milliseconds
|
|
199
|
+
* @type {?number}
|
|
200
|
+
* @readonly
|
|
201
|
+
*/
|
|
202
|
+
get uptime() {
|
|
203
|
+
return this.readyTimestamp && Date.now() - this.readyTimestamp;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Logs the client in, establishing a WebSocket connection to Discord.
|
|
208
|
+
* @param {string} [token=this.token] Token of the account to log in with
|
|
209
|
+
* @returns {Promise<string>} Token of the account used
|
|
210
|
+
* @example
|
|
211
|
+
* client.login('my token');
|
|
212
|
+
*/
|
|
213
|
+
async login(token = this.token) {
|
|
214
|
+
if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
|
|
215
|
+
this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
|
|
216
|
+
this.rest.setToken(token);
|
|
217
|
+
this.emit(Events.Debug, `Provided token: ${this._censoredToken}`);
|
|
218
|
+
|
|
219
|
+
if (this.options.presence) {
|
|
220
|
+
this.options.ws.presence = this.presence._parse(this.options.presence);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
this.emit(Events.Debug, 'Preparing to connect to the gateway...');
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
await this.ws.connect();
|
|
227
|
+
return this.token;
|
|
228
|
+
} catch (error) {
|
|
229
|
+
this.destroy();
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Returns whether the client has logged in, indicative of being able to access
|
|
236
|
+
* properties such as `user` and `application`.
|
|
237
|
+
* @returns {boolean}
|
|
238
|
+
*/
|
|
239
|
+
isReady() {
|
|
240
|
+
return this.ws.status === Status.Ready;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Logs out, terminates the connection to Discord, and destroys the client.
|
|
245
|
+
* @returns {void}
|
|
246
|
+
*/
|
|
247
|
+
destroy() {
|
|
248
|
+
super.destroy();
|
|
249
|
+
|
|
250
|
+
this.sweepers.destroy();
|
|
251
|
+
this.ws.destroy();
|
|
252
|
+
this.token = null;
|
|
253
|
+
this.rest.setToken(null);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Options used when fetching an invite from Discord.
|
|
258
|
+
* @typedef {Object} ClientFetchInviteOptions
|
|
259
|
+
* @property {Snowflake} [guildScheduledEventId] The id of the guild scheduled event to include with
|
|
260
|
+
* the invite
|
|
261
|
+
*/
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Obtains an invite from Discord.
|
|
265
|
+
* @param {InviteResolvable} invite Invite code or URL
|
|
266
|
+
* @param {ClientFetchInviteOptions} [options] Options for fetching the invite
|
|
267
|
+
* @returns {Promise<Invite>}
|
|
268
|
+
* @example
|
|
269
|
+
* client.fetchInvite('https://discord.gg/djs')
|
|
270
|
+
* .then(invite => console.log(`Obtained invite with code: ${invite.code}`))
|
|
271
|
+
* .catch(console.error);
|
|
272
|
+
*/
|
|
273
|
+
async fetchInvite(invite, options) {
|
|
274
|
+
const code = DataResolver.resolveInviteCode(invite);
|
|
275
|
+
const query = makeURLSearchParams({
|
|
276
|
+
with_counts: true,
|
|
277
|
+
with_expiration: true,
|
|
278
|
+
guild_scheduled_event_id: options?.guildScheduledEventId,
|
|
279
|
+
});
|
|
280
|
+
const data = await this.rest.get(Routes.invite(code), { query });
|
|
281
|
+
return new Invite(this, data);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Obtains a template from Discord.
|
|
286
|
+
* @param {GuildTemplateResolvable} template Template code or URL
|
|
287
|
+
* @returns {Promise<GuildTemplate>}
|
|
288
|
+
* @example
|
|
289
|
+
* client.fetchGuildTemplate('https://discord.new/FKvmczH2HyUf')
|
|
290
|
+
* .then(template => console.log(`Obtained template with code: ${template.code}`))
|
|
291
|
+
* .catch(console.error);
|
|
292
|
+
*/
|
|
293
|
+
async fetchGuildTemplate(template) {
|
|
294
|
+
const code = DataResolver.resolveGuildTemplateCode(template);
|
|
295
|
+
const data = await this.rest.get(Routes.template(code));
|
|
296
|
+
return new GuildTemplate(this, data);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Obtains a webhook from Discord.
|
|
301
|
+
* @param {Snowflake} id The webhook's id
|
|
302
|
+
* @param {string} [token] Token for the webhook
|
|
303
|
+
* @returns {Promise<Webhook>}
|
|
304
|
+
* @example
|
|
305
|
+
* client.fetchWebhook('id', 'token')
|
|
306
|
+
* .then(webhook => console.log(`Obtained webhook with name: ${webhook.name}`))
|
|
307
|
+
* .catch(console.error);
|
|
308
|
+
*/
|
|
309
|
+
async fetchWebhook(id, token) {
|
|
310
|
+
const data = await this.rest.get(Routes.webhook(id, token), { auth: token === undefined });
|
|
311
|
+
return new Webhook(this, { token, ...data });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Obtains the available voice regions from Discord.
|
|
316
|
+
* @returns {Promise<Collection<string, VoiceRegion>>}
|
|
317
|
+
* @example
|
|
318
|
+
* client.fetchVoiceRegions()
|
|
319
|
+
* .then(regions => console.log(`Available regions are: ${regions.map(region => region.name).join(', ')}`))
|
|
320
|
+
* .catch(console.error);
|
|
321
|
+
*/
|
|
322
|
+
async fetchVoiceRegions() {
|
|
323
|
+
const apiRegions = await this.rest.get(Routes.voiceRegions());
|
|
324
|
+
const regions = new Collection();
|
|
325
|
+
for (const region of apiRegions) regions.set(region.id, new VoiceRegion(region));
|
|
326
|
+
return regions;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Obtains a sticker from Discord.
|
|
331
|
+
* @param {Snowflake} id The sticker's id
|
|
332
|
+
* @returns {Promise<Sticker>}
|
|
333
|
+
* @example
|
|
334
|
+
* client.fetchSticker('id')
|
|
335
|
+
* .then(sticker => console.log(`Obtained sticker with name: ${sticker.name}`))
|
|
336
|
+
* .catch(console.error);
|
|
337
|
+
*/
|
|
338
|
+
async fetchSticker(id) {
|
|
339
|
+
const data = await this.rest.get(Routes.sticker(id));
|
|
340
|
+
return new Sticker(this, data);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Obtains the list of sticker packs available to Nitro subscribers from Discord.
|
|
345
|
+
* @returns {Promise<Collection<Snowflake, StickerPack>>}
|
|
346
|
+
* @example
|
|
347
|
+
* client.fetchPremiumStickerPacks()
|
|
348
|
+
* .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
|
|
349
|
+
* .catch(console.error);
|
|
350
|
+
*/
|
|
351
|
+
async fetchPremiumStickerPacks() {
|
|
352
|
+
const data = await this.rest.get(Routes.nitroStickerPacks());
|
|
353
|
+
return new Collection(data.sticker_packs.map(p => [p.id, new StickerPack(this, p)]));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Obtains a guild preview from Discord, available for all guilds the bot is in and all Discoverable guilds.
|
|
358
|
+
* @param {GuildResolvable} guild The guild to fetch the preview for
|
|
359
|
+
* @returns {Promise<GuildPreview>}
|
|
360
|
+
*/
|
|
361
|
+
async fetchGuildPreview(guild) {
|
|
362
|
+
const id = this.guilds.resolveId(guild);
|
|
363
|
+
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'guild', 'GuildResolvable');
|
|
364
|
+
const data = await this.rest.get(Routes.guildPreview(id));
|
|
365
|
+
return new GuildPreview(this, data);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Obtains the widget data of a guild from Discord, available for guilds with the widget enabled.
|
|
370
|
+
* @param {GuildResolvable} guild The guild to fetch the widget data for
|
|
371
|
+
* @returns {Promise<Widget>}
|
|
372
|
+
*/
|
|
373
|
+
async fetchGuildWidget(guild) {
|
|
374
|
+
const id = this.guilds.resolveId(guild);
|
|
375
|
+
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'guild', 'GuildResolvable');
|
|
376
|
+
const data = await this.rest.get(Routes.guildWidgetJSON(id));
|
|
377
|
+
return new Widget(this, data);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Options for {@link Client#generateInvite}.
|
|
382
|
+
* @typedef {Object} InviteGenerationOptions
|
|
383
|
+
* @property {OAuth2Scopes[]} scopes Scopes that should be requested
|
|
384
|
+
* @property {PermissionResolvable} [permissions] Permissions to request
|
|
385
|
+
* @property {GuildResolvable} [guild] Guild to preselect
|
|
386
|
+
* @property {boolean} [disableGuildSelect] Whether to disable the guild selection
|
|
387
|
+
*/
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Generates a link that can be used to invite the bot to a guild.
|
|
391
|
+
* @param {InviteGenerationOptions} [options={}] Options for the invite
|
|
392
|
+
* @returns {string}
|
|
393
|
+
* @example
|
|
394
|
+
* const link = client.generateInvite({
|
|
395
|
+
* scopes: [OAuth2Scopes.ApplicationsCommands],
|
|
396
|
+
* });
|
|
397
|
+
* console.log(`Generated application invite link: ${link}`);
|
|
398
|
+
* @example
|
|
399
|
+
* const link = client.generateInvite({
|
|
400
|
+
* permissions: [
|
|
401
|
+
* PermissionFlagsBits.SendMessages,
|
|
402
|
+
* PermissionFlagsBits.ManageGuild,
|
|
403
|
+
* PermissionFlagsBits.MentionEveryone,
|
|
404
|
+
* ],
|
|
405
|
+
* scopes: [OAuth2Scopes.Bot],
|
|
406
|
+
* });
|
|
407
|
+
* console.log(`Generated bot invite link: ${link}`);
|
|
408
|
+
*/
|
|
409
|
+
generateInvite(options = {}) {
|
|
410
|
+
if (typeof options !== 'object') throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
|
|
411
|
+
if (!this.application) throw new DiscordjsError(ErrorCodes.ClientNotReady, 'generate an invite link');
|
|
412
|
+
|
|
413
|
+
const { scopes } = options;
|
|
414
|
+
if (scopes === undefined) {
|
|
415
|
+
throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes);
|
|
416
|
+
}
|
|
417
|
+
if (!Array.isArray(scopes)) {
|
|
418
|
+
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'scopes', 'Array of Invite Scopes', true);
|
|
419
|
+
}
|
|
420
|
+
if (!scopes.some(scope => [OAuth2Scopes.Bot, OAuth2Scopes.ApplicationsCommands].includes(scope))) {
|
|
421
|
+
throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes);
|
|
422
|
+
}
|
|
423
|
+
if (!scopes.includes(OAuth2Scopes.Bot) && options.permissions) {
|
|
424
|
+
throw new DiscordjsTypeError(ErrorCodes.InvalidScopesWithPermissions);
|
|
425
|
+
}
|
|
426
|
+
const validScopes = Object.values(OAuth2Scopes);
|
|
427
|
+
const invalidScope = scopes.find(scope => !validScopes.includes(scope));
|
|
428
|
+
if (invalidScope) {
|
|
429
|
+
throw new DiscordjsTypeError(ErrorCodes.InvalidElement, 'Array', 'scopes', invalidScope);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const query = makeURLSearchParams({
|
|
433
|
+
client_id: this.application.id,
|
|
434
|
+
scope: scopes.join(' '),
|
|
435
|
+
disable_guild_select: options.disableGuildSelect,
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
if (options.permissions) {
|
|
439
|
+
const permissions = PermissionsBitField.resolve(options.permissions);
|
|
440
|
+
if (permissions) query.set('permissions', permissions.toString());
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (options.guild) {
|
|
444
|
+
const guildId = this.guilds.resolveId(options.guild);
|
|
445
|
+
if (!guildId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options.guild', 'GuildResolvable');
|
|
446
|
+
query.set('guild_id', guildId);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return `${this.options.rest.api}${Routes.oauth2Authorization()}?${query}`;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
toJSON() {
|
|
453
|
+
return super.toJSON({
|
|
454
|
+
actions: false,
|
|
455
|
+
presence: false,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Partially censored client token for debug logging purposes.
|
|
461
|
+
* @type {?string}
|
|
462
|
+
* @readonly
|
|
463
|
+
* @private
|
|
464
|
+
*/
|
|
465
|
+
get _censoredToken() {
|
|
466
|
+
if (!this.token) return null;
|
|
467
|
+
|
|
468
|
+
return this.token
|
|
469
|
+
.split('.')
|
|
470
|
+
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
|
|
471
|
+
.join('.');
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Calls {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval} on a script
|
|
476
|
+
* with the client as `this`.
|
|
477
|
+
* @param {string} script Script to eval
|
|
478
|
+
* @returns {*}
|
|
479
|
+
* @private
|
|
480
|
+
*/
|
|
481
|
+
_eval(script) {
|
|
482
|
+
return eval(script);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Validates the client options.
|
|
487
|
+
* @param {ClientOptions} [options=this.options] Options to validate
|
|
488
|
+
* @private
|
|
489
|
+
*/
|
|
490
|
+
_validateOptions(options = this.options) {
|
|
491
|
+
if (options.intents === undefined) {
|
|
492
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientMissingIntents);
|
|
493
|
+
} else {
|
|
494
|
+
options.intents = new IntentsBitField(options.intents).freeze();
|
|
495
|
+
}
|
|
496
|
+
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount) || options.shardCount < 1) {
|
|
497
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'shardCount', 'a number greater than or equal to 1');
|
|
498
|
+
}
|
|
499
|
+
if (options.shards && !(options.shards === 'auto' || Array.isArray(options.shards))) {
|
|
500
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'shards', "'auto', a number or array of numbers");
|
|
501
|
+
}
|
|
502
|
+
if (options.shards && !options.shards.length) throw new DiscordjsRangeError(ErrorCodes.ClientInvalidProvidedShards);
|
|
503
|
+
if (typeof options.makeCache !== 'function') {
|
|
504
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'makeCache', 'a function');
|
|
505
|
+
}
|
|
506
|
+
if (typeof options.sweepers !== 'object' || options.sweepers === null) {
|
|
507
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'sweepers', 'an object');
|
|
508
|
+
}
|
|
509
|
+
if (!Array.isArray(options.partials)) {
|
|
510
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'partials', 'an Array');
|
|
511
|
+
}
|
|
512
|
+
if (typeof options.waitGuildTimeout !== 'number' || isNaN(options.waitGuildTimeout)) {
|
|
513
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'waitGuildTimeout', 'a number');
|
|
514
|
+
}
|
|
515
|
+
if (typeof options.failIfNotExists !== 'boolean') {
|
|
516
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean');
|
|
517
|
+
}
|
|
518
|
+
if (
|
|
519
|
+
(typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) ||
|
|
520
|
+
options.allowedMentions === null
|
|
521
|
+
) {
|
|
522
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object');
|
|
523
|
+
}
|
|
524
|
+
if (typeof options.presence !== 'object' || options.presence === null) {
|
|
525
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'presence', 'an object');
|
|
526
|
+
}
|
|
527
|
+
if (typeof options.ws !== 'object' || options.ws === null) {
|
|
528
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object');
|
|
529
|
+
}
|
|
530
|
+
if (typeof options.rest !== 'object' || options.rest === null) {
|
|
531
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object');
|
|
532
|
+
}
|
|
533
|
+
if (typeof options.jsonTransformer !== 'function') {
|
|
534
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'jsonTransformer', 'a function');
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
module.exports = Client;
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* @class SnowflakeUtil
|
|
543
|
+
* @classdesc This class is an alias for {@link https://www.npmjs.com/package/@sapphire/snowflake @sapphire/snowflake}'s
|
|
544
|
+
* `DiscordSnowflake` class.
|
|
545
|
+
*
|
|
546
|
+
* Check their documentation
|
|
547
|
+
* {@link https://www.sapphirejs.dev/docs/Documentation/api-utilities/classes/sapphire_snowflake.Snowflake here}
|
|
548
|
+
* ({@link https://www.sapphirejs.dev/docs/Guide/utilities/snowflake guide})
|
|
549
|
+
* to see what you can do.
|
|
550
|
+
* @hideconstructor
|
|
551
|
+
*/
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* A {@link https://developer.twitter.com/en/docs/twitter-ids Twitter snowflake},
|
|
555
|
+
* except the epoch is 2015-01-01T00:00:00.000Z.
|
|
556
|
+
*
|
|
557
|
+
* If we have a snowflake '266241948824764416' we can represent it as binary:
|
|
558
|
+
* ```
|
|
559
|
+
* 64 22 17 12 0
|
|
560
|
+
* 000000111011000111100001101001000101000000 00001 00000 000000000000
|
|
561
|
+
* number of milliseconds since Discord epoch worker pid increment
|
|
562
|
+
* ```
|
|
563
|
+
* @typedef {string} Snowflake
|
|
564
|
+
*/
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Emitted for general debugging information.
|
|
568
|
+
* @event Client#debug
|
|
569
|
+
* @param {string} info The debug information
|
|
570
|
+
*/
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Emitted for general warnings.
|
|
574
|
+
* @event Client#warn
|
|
575
|
+
* @param {string} info The warning
|
|
576
|
+
*/
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* @external Collection
|
|
580
|
+
* @see {@link https://discord.js.org/docs/packages/collection/stable/Collection:Class}
|
|
581
|
+
*/
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* @external ImageURLOptions
|
|
585
|
+
* @see {@link https://discord.js.org/docs/packages/rest/stable/ImageURLOptions:Interface}
|
|
586
|
+
*/
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* @external BaseImageURLOptions
|
|
590
|
+
* @see {@link https://discord.js.org/docs/packages/rest/stable/BaseImageURLOptions:Interface}
|
|
591
|
+
*/
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BaseClient = require('./BaseClient');
|
|
4
|
+
const { DiscordjsError, ErrorCodes } = require('../errors');
|
|
5
|
+
const Webhook = require('../structures/Webhook');
|
|
6
|
+
const { parseWebhookURL } = require('../util/Util');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The webhook client.
|
|
10
|
+
* @implements {Webhook}
|
|
11
|
+
* @extends {BaseClient}
|
|
12
|
+
*/
|
|
13
|
+
class WebhookClient extends BaseClient {
|
|
14
|
+
/**
|
|
15
|
+
* Represents the credentials used for a webhook in the form of its id and token.
|
|
16
|
+
* @typedef {Object} WebhookClientDataIdWithToken
|
|
17
|
+
* @property {Snowflake} id The webhook's id
|
|
18
|
+
* @property {string} token The webhook's token
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Represents the credentials used for a webhook in the form of a URL.
|
|
23
|
+
* @typedef {Object} WebhookClientDataURL
|
|
24
|
+
* @property {string} url The full URL for the webhook
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Represents the credentials used for a webhook.
|
|
29
|
+
* @typedef {WebhookClientDataIdWithToken|WebhookClientDataURL} WebhookClientData
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Options for a webhook client.
|
|
34
|
+
* @typedef {Object} WebhookClientOptions
|
|
35
|
+
* @property {MessageMentionOptions} [allowedMentions] Default value for {@link BaseMessageOptions#allowedMentions}
|
|
36
|
+
* @property {RESTOptions} [rest] Options for the REST manager
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {WebhookClientData} data The data of the webhook
|
|
41
|
+
* @param {WebhookClientOptions} [options] Options for the webhook client
|
|
42
|
+
*/
|
|
43
|
+
constructor(data, options) {
|
|
44
|
+
super(options);
|
|
45
|
+
Object.defineProperty(this, 'client', { value: this });
|
|
46
|
+
let { id, token } = data;
|
|
47
|
+
|
|
48
|
+
if ('url' in data) {
|
|
49
|
+
const parsed = parseWebhookURL(data.url);
|
|
50
|
+
if (!parsed) {
|
|
51
|
+
throw new DiscordjsError(ErrorCodes.WebhookURLInvalid);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
({ id, token } = parsed);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.id = id;
|
|
58
|
+
Object.defineProperty(this, 'token', { value: token, writable: true, configurable: true });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The options the webhook client was instantiated with.
|
|
63
|
+
* @type {WebhookClientOptions}
|
|
64
|
+
* @name WebhookClient#options
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
// These are here only for documentation purposes - they are implemented by Webhook
|
|
68
|
+
/* eslint-disable no-empty-function, valid-jsdoc */
|
|
69
|
+
/**
|
|
70
|
+
* Sends a message with this webhook.
|
|
71
|
+
* @param {string|MessagePayload|WebhookMessageCreateOptions} options The content for the reply
|
|
72
|
+
* @returns {Promise<APIMessage>}
|
|
73
|
+
*/
|
|
74
|
+
send() {}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Gets a message that was sent by this webhook.
|
|
78
|
+
* @param {Snowflake} message The id of the message to fetch
|
|
79
|
+
* @param {WebhookFetchMessageOptions} [options={}] The options to provide to fetch the message.
|
|
80
|
+
* @returns {Promise<APIMessage>} Returns the message sent by this webhook
|
|
81
|
+
*/
|
|
82
|
+
fetchMessage() {}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Edits a message that was sent by this webhook.
|
|
86
|
+
* @param {MessageResolvable} message The message to edit
|
|
87
|
+
* @param {string|MessagePayload|WebhookMessageEditOptions} options The options to provide
|
|
88
|
+
* @returns {Promise<APIMessage>} Returns the message edited by this webhook
|
|
89
|
+
*/
|
|
90
|
+
editMessage() {}
|
|
91
|
+
|
|
92
|
+
sendSlackMessage() {}
|
|
93
|
+
edit() {}
|
|
94
|
+
delete() {}
|
|
95
|
+
deleteMessage() {}
|
|
96
|
+
get createdTimestamp() {}
|
|
97
|
+
get createdAt() {}
|
|
98
|
+
get url() {}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
Webhook.applyToClass(WebhookClient);
|
|
102
|
+
|
|
103
|
+
module.exports = WebhookClient;
|