@velliajs/discord 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/client/BaseClient.js +7 -28
- package/src/client/Client.js +116 -283
- package/src/client/WebhookClient.js +17 -59
- package/src/client/actions/Action.js +0 -10
- package/src/client/actions/ActionsManager.js +3 -3
- package/src/client/actions/ApplicationCommandPermissionsUpdate.js +2 -15
- package/src/client/actions/AutoModerationActionExecution.js +1 -6
- package/src/client/actions/AutoModerationRuleCreate.js +1 -6
- package/src/client/actions/AutoModerationRuleDelete.js +1 -6
- package/src/client/actions/AutoModerationRuleUpdate.js +1 -7
- package/src/client/actions/ChannelCreate.js +1 -5
- package/src/client/actions/ChannelDelete.js +1 -5
- package/src/client/actions/GuildAuditLogEntryCreate.js +1 -6
- package/src/client/actions/GuildBanAdd.js +1 -5
- package/src/client/actions/GuildBanRemove.js +1 -5
- package/src/client/actions/GuildDelete.js +6 -14
- package/src/client/actions/GuildEmojiCreate.js +1 -5
- package/src/client/actions/GuildEmojiDelete.js +1 -5
- package/src/client/actions/GuildEmojiUpdate.js +1 -6
- package/src/client/actions/GuildEmojisUpdate.js +4 -4
- package/src/client/actions/GuildIntegrationsUpdate.js +1 -5
- package/src/client/actions/GuildMemberRemove.js +1 -5
- package/src/client/actions/GuildMemberUpdate.js +2 -11
- package/src/client/actions/GuildRoleCreate.js +1 -5
- package/src/client/actions/GuildRoleDelete.js +1 -5
- package/src/client/actions/GuildRoleUpdate.js +1 -6
- package/src/client/actions/GuildScheduledEventCreate.js +1 -5
- package/src/client/actions/GuildScheduledEventDelete.js +1 -5
- package/src/client/actions/GuildScheduledEventUpdate.js +1 -6
- package/src/client/actions/GuildScheduledEventUserAdd.js +1 -6
- package/src/client/actions/GuildScheduledEventUserRemove.js +1 -6
- package/src/client/actions/GuildStickerCreate.js +1 -5
- package/src/client/actions/GuildStickerDelete.js +1 -5
- package/src/client/actions/GuildStickerUpdate.js +1 -6
- package/src/client/actions/GuildStickersUpdate.js +4 -4
- package/src/client/actions/GuildUpdate.js +1 -6
- package/src/client/actions/InteractionCreate.js +3 -7
- package/src/client/actions/InviteCreate.js +1 -7
- package/src/client/actions/InviteDelete.js +1 -7
- package/src/client/actions/MessageCreate.js +1 -5
- package/src/client/actions/MessageDelete.js +1 -5
- package/src/client/actions/MessageDeleteBulk.js +1 -6
- package/src/client/actions/MessageReactionAdd.js +5 -18
- package/src/client/actions/MessageReactionRemove.js +5 -16
- package/src/client/actions/MessageReactionRemoveAll.js +4 -9
- package/src/client/actions/MessageReactionRemoveEmoji.js +1 -5
- package/src/client/actions/PresenceUpdate.js +1 -6
- package/src/client/actions/StageInstanceCreate.js +1 -5
- package/src/client/actions/StageInstanceDelete.js +1 -5
- package/src/client/actions/StageInstanceUpdate.js +1 -6
- package/src/client/actions/ThreadCreate.js +1 -6
- package/src/client/actions/ThreadDelete.js +1 -5
- package/src/client/actions/ThreadListSync.js +2 -7
- package/src/client/actions/ThreadMemberUpdate.js +2 -7
- package/src/client/actions/ThreadMembersUpdate.js +2 -9
- package/src/client/actions/TypingStart.js +1 -5
- package/src/client/actions/UserUpdate.js +1 -8
- package/src/client/actions/VoiceStateUpdate.js +4 -9
- package/src/client/actions/WebhooksUpdate.js +1 -6
- package/src/client/voice/ClientVoiceManager.js +3 -13
- package/src/client/websocket/WebSocketManager.js +25 -117
- package/src/client/websocket/WebSocketShard.js +29 -116
- package/src/client/websocket/handlers/CHANNEL_PINS_UPDATE.js +2 -8
- package/src/client/websocket/handlers/CHANNEL_UPDATE.js +1 -6
- package/src/client/websocket/handlers/GUILD_CREATE.js +3 -7
- package/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js +2 -16
- package/src/client/websocket/handlers/GUILD_MEMBER_ADD.js +1 -5
- package/src/client/websocket/handlers/MESSAGE_UPDATE.js +1 -6
- package/src/client/websocket/handlers/RESUMED.js +1 -6
- package/src/client/websocket/handlers/THREAD_UPDATE.js +1 -6
- package/src/errors/DJSError.js +3 -14
- package/src/errors/ErrorCodes.js +2 -164
- package/src/index.js +6 -6
- package/src/managers/ApplicationCommandManager.js +14 -130
- package/src/managers/ApplicationCommandPermissionsManager.js +31 -202
- package/src/managers/AutoModerationRuleManager.js +17 -145
- package/src/managers/BaseGuildEmojiManager.js +7 -38
- package/src/managers/BaseManager.js +2 -10
- package/src/managers/CachedManager.js +3 -17
- package/src/managers/CategoryChannelChildManager.js +6 -48
- package/src/managers/ChannelManager.js +11 -52
- package/src/managers/DataManager.js +5 -27
- package/src/managers/GuildApplicationCommandManager.js +3 -12
- package/src/managers/GuildBanManager.js +16 -105
- package/src/managers/GuildChannelManager.js +31 -226
- package/src/managers/GuildEmojiManager.js +9 -65
- package/src/managers/GuildEmojiRoleManager.js +8 -47
- package/src/managers/GuildForumThreadManager.js +5 -39
- package/src/managers/GuildInviteManager.js +18 -117
- package/src/managers/GuildManager.js +14 -115
- package/src/managers/GuildMemberManager.js +38 -256
- package/src/managers/GuildMemberRoleManager.js +13 -76
- package/src/managers/GuildScheduledEventManager.js +25 -128
- package/src/managers/GuildStickerManager.js +16 -96
- package/src/managers/GuildTextThreadManager.js +4 -47
- package/src/managers/MessageManager.js +23 -137
- package/src/managers/PermissionOverwriteManager.js +10 -78
- package/src/managers/PresenceManager.js +6 -27
- package/src/managers/ReactionManager.js +7 -40
- package/src/managers/ReactionUserManager.js +6 -29
- package/src/managers/RoleManager.js +29 -177
- package/src/managers/StageInstanceManager.js +14 -77
- package/src/managers/ThreadManager.js +23 -113
- package/src/managers/ThreadMemberManager.js +20 -89
- package/src/managers/UserManager.js +13 -65
- package/src/managers/VoiceStateManager.js +3 -13
- package/src/sharding/Shard.js +42 -158
- package/src/sharding/ShardClientUtil.js +17 -101
- package/src/sharding/ShardingManager.js +34 -151
- package/src/structures/ActionRow.js +4 -21
- package/src/structures/ActionRowBuilder.js +3 -13
- package/src/structures/AnonymousGuild.js +10 -43
- package/src/structures/ApplicationCommand.js +54 -308
- package/src/structures/ApplicationRoleConnectionMetadata.js +7 -27
- package/src/structures/Attachment.js +16 -65
- package/src/structures/AttachmentBuilder.js +13 -58
- package/src/structures/AutoModerationActionExecution.js +16 -69
- package/src/structures/AutoModerationRule.js +32 -164
- package/src/structures/AutocompleteInteraction.js +10 -52
- package/src/structures/Base.js +2 -10
- package/src/structures/BaseChannel.js +16 -80
- package/src/structures/BaseGuild.js +13 -59
- package/src/structures/BaseGuildEmoji.js +5 -21
- package/src/structures/BaseGuildTextChannel.js +19 -100
- package/src/structures/BaseGuildVoiceChannel.js +21 -110
- package/src/structures/BaseInteraction.js +40 -196
- package/src/structures/BaseSelectMenuComponent.js +6 -29
- package/src/structures/ButtonBuilder.js +4 -18
- package/src/structures/ButtonComponent.js +7 -34
- package/src/structures/ButtonInteraction.js +1 -4
- package/src/structures/CategoryChannel.js +5 -30
- package/src/structures/ChannelSelectMenuBuilder.js +3 -13
- package/src/structures/ChannelSelectMenuComponent.js +2 -9
- package/src/structures/ChannelSelectMenuInteraction.js +3 -12
- package/src/structures/ChatInputCommandInteraction.js +3 -13
- package/src/structures/ClientApplication.js +20 -83
- package/src/structures/ClientPresence.js +5 -20
- package/src/structures/ClientUser.js +21 -118
- package/src/structures/CommandInteraction.js +21 -96
- package/src/structures/CommandInteractionOptionResolver.js +26 -147
- package/src/structures/Component.js +5 -21
- package/src/structures/ContextMenuCommandInteraction.js +4 -18
- package/src/structures/DMChannel.js +17 -52
- package/src/structures/DirectoryChannel.js +4 -16
- package/src/structures/Embed.js +21 -107
- package/src/structures/EmbedBuilder.js +4 -18
- package/src/structures/Emoji.js +12 -60
- package/src/structures/ForumChannel.js +30 -140
- package/src/structures/Guild.js +117 -697
- package/src/structures/GuildAuditLogs.js +7 -32
- package/src/structures/GuildAuditLogsEntry.js +27 -135
- package/src/structures/GuildBan.js +6 -26
- package/src/structures/GuildChannel.js +36 -188
- package/src/structures/GuildEmoji.js +13 -67
- package/src/structures/GuildMember.js +47 -270
- package/src/structures/GuildPreview.js +19 -84
- package/src/structures/GuildPreviewEmoji.js +3 -13
- package/src/structures/GuildScheduledEvent.js +46 -250
- package/src/structures/GuildTemplate.js +24 -108
- package/src/structures/Integration.js +25 -103
- package/src/structures/IntegrationApplication.js +8 -32
- package/src/structures/InteractionCollector.js +22 -113
- package/src/structures/InteractionResponse.js +11 -46
- package/src/structures/InteractionWebhook.js +8 -33
- package/src/structures/Invite.js +27 -131
- package/src/structures/InviteGuild.js +2 -8
- package/src/structures/InviteStageInstance.js +9 -39
- package/src/structures/MentionableSelectMenuBuilder.js +3 -14
- package/src/structures/MentionableSelectMenuComponent.js +1 -4
- package/src/structures/MentionableSelectMenuInteraction.js +5 -20
- package/src/structures/Message.js +85 -460
- package/src/structures/MessageCollector.js +14 -70
- package/src/structures/MessageComponentInteraction.js +14 -57
- package/src/structures/MessageContextMenuCommandInteraction.js +2 -9
- package/src/structures/MessageMentions.js +25 -136
- package/src/structures/MessagePayload.js +24 -94
- package/src/structures/MessageReaction.js +13 -51
- package/src/structures/ModalBuilder.js +3 -13
- package/src/structures/ModalSubmitFields.js +5 -22
- package/src/structures/ModalSubmitInteraction.js +17 -61
- package/src/structures/NewsChannel.js +2 -16
- package/src/structures/OAuth2Guild.js +3 -12
- package/src/structures/PartialGroupDMChannel.js +7 -27
- package/src/structures/PermissionOverwrites.js +20 -99
- package/src/structures/Presence.js +45 -190
- package/src/structures/ReactionCollector.js +20 -108
- package/src/structures/ReactionEmoji.js +2 -10
- package/src/structures/Role.js +39 -261
- package/src/structures/RoleSelectMenuBuilder.js +3 -13
- package/src/structures/RoleSelectMenuComponent.js +1 -4
- package/src/structures/RoleSelectMenuInteraction.js +3 -12
- package/src/structures/SelectMenuBuilder.js +1 -4
- package/src/structures/SelectMenuComponent.js +1 -4
- package/src/structures/SelectMenuInteraction.js +1 -4
- package/src/structures/SelectMenuOptionBuilder.js +1 -4
- package/src/structures/StageChannel.js +12 -84
- package/src/structures/StageInstance.js +16 -87
- package/src/structures/Sticker.js +25 -124
- package/src/structures/StickerPack.js +12 -52
- package/src/structures/StringSelectMenuBuilder.js +6 -29
- package/src/structures/StringSelectMenuComponent.js +2 -9
- package/src/structures/StringSelectMenuInteraction.js +2 -8
- package/src/structures/StringSelectMenuOptionBuilder.js +4 -18
- package/src/structures/Team.js +11 -52
- package/src/structures/TeamMember.js +7 -33
- package/src/structures/TextChannel.js +3 -14
- package/src/structures/TextInputBuilder.js +3 -13
- package/src/structures/TextInputComponent.js +3 -14
- package/src/structures/ThreadChannel.js +59 -305
- package/src/structures/ThreadMember.js +12 -55
- package/src/structures/Typing.js +8 -35
- package/src/structures/User.js +29 -151
- package/src/structures/UserContextMenuCommandInteraction.js +3 -14
- package/src/structures/UserSelectMenuBuilder.js +3 -13
- package/src/structures/UserSelectMenuComponent.js +1 -4
- package/src/structures/UserSelectMenuInteraction.js +4 -16
- package/src/structures/VoiceChannel.js +8 -67
- package/src/structures/VoiceRegion.js +6 -23
- package/src/structures/VoiceState.js +28 -140
- package/src/structures/Webhook.js +37 -227
- package/src/structures/WelcomeChannel.js +7 -29
- package/src/structures/WelcomeScreen.js +5 -20
- package/src/structures/Widget.js +9 -40
- package/src/structures/WidgetMember.js +15 -61
- package/src/structures/interfaces/Application.js +10 -49
- package/src/structures/interfaces/Collector.js +34 -155
- package/src/structures/interfaces/InteractionResponses.js +25 -164
- package/src/structures/interfaces/TextBasedChannel.js +37 -232
- package/src/util/APITypes.js +183 -456
- package/src/util/ActivityFlagsBitField.js +3 -15
- package/src/util/ApplicationFlagsBitField.js +3 -15
- package/src/util/BitField.js +17 -82
- package/src/util/ChannelFlagsBitField.js +7 -30
- package/src/util/Channels.js +5 -35
- package/src/util/Colors.js +3 -38
- package/src/util/Components.js +21 -75
- package/src/util/Constants.js +17 -144
- package/src/util/DataResolver.js +20 -77
- package/src/util/Events.js +3 -81
- package/src/util/Formatters.js +58 -305
- package/src/util/GuildMemberFlagsBitField.js +7 -30
- package/src/util/IntentsBitField.js +4 -23
- package/src/util/LimitedCollection.js +4 -21
- package/src/util/MessageFlagsBitField.js +5 -21
- package/src/util/Options.js +24 -137
- package/src/util/Partials.js +3 -30
- package/src/util/PermissionsBitField.js +12 -67
- package/src/util/ShardEvents.js +3 -15
- package/src/util/Status.js +3 -17
- package/src/util/Sweepers.js +35 -184
- package/src/util/SystemChannelFlagsBitField.js +7 -32
- package/src/util/ThreadMemberFlagsBitField.js +5 -21
- package/src/util/Transformers.js +2 -11
- package/src/util/UserFlagsBitField.js +5 -21
- package/src/util/Util.js +30 -169
- package/src/util/WebSocketShardEvents.js +3 -14
package/package.json
CHANGED
package/src/client/BaseClient.js
CHANGED
|
@@ -6,10 +6,7 @@ const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
|
|
6
6
|
const Options = require('../util/Options');
|
|
7
7
|
const { mergeDefault, flatten } = require('../util/Util');
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
* The base class for all clients.
|
|
11
|
-
* @extends {EventEmitter}
|
|
12
|
-
*/
|
|
9
|
+
|
|
13
10
|
class BaseClient extends EventEmitter {
|
|
14
11
|
constructor(options = {}) {
|
|
15
12
|
super({ captureRejections: true });
|
|
@@ -18,10 +15,7 @@ class BaseClient extends EventEmitter {
|
|
|
18
15
|
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
|
|
19
16
|
}
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
* The options the client was instantiated with
|
|
23
|
-
* @type {ClientOptions}
|
|
24
|
-
*/
|
|
18
|
+
|
|
25
19
|
this.options = mergeDefault(Options.createDefault(), {
|
|
26
20
|
...options,
|
|
27
21
|
rest: {
|
|
@@ -32,26 +26,17 @@ class BaseClient extends EventEmitter {
|
|
|
32
26
|
},
|
|
33
27
|
});
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
* The REST manager of the client
|
|
37
|
-
* @type {REST}
|
|
38
|
-
*/
|
|
29
|
+
|
|
39
30
|
this.rest = new REST(this.options.rest);
|
|
40
31
|
}
|
|
41
32
|
|
|
42
|
-
|
|
43
|
-
* Destroys all assets used by the base client.
|
|
44
|
-
* @returns {void}
|
|
45
|
-
*/
|
|
33
|
+
|
|
46
34
|
destroy() {
|
|
47
35
|
this.rest.requestManager.clearHashSweeper();
|
|
48
36
|
this.rest.requestManager.clearHandlerSweeper();
|
|
49
37
|
}
|
|
50
38
|
|
|
51
|
-
|
|
52
|
-
* Increments max listeners by one, if they are not zero.
|
|
53
|
-
* @private
|
|
54
|
-
*/
|
|
39
|
+
|
|
55
40
|
incrementMaxListeners() {
|
|
56
41
|
const maxListeners = this.getMaxListeners();
|
|
57
42
|
if (maxListeners !== 0) {
|
|
@@ -59,10 +44,7 @@ class BaseClient extends EventEmitter {
|
|
|
59
44
|
}
|
|
60
45
|
}
|
|
61
46
|
|
|
62
|
-
|
|
63
|
-
* Decrements max listeners by one, if they are not zero.
|
|
64
|
-
* @private
|
|
65
|
-
*/
|
|
47
|
+
|
|
66
48
|
decrementMaxListeners() {
|
|
67
49
|
const maxListeners = this.getMaxListeners();
|
|
68
50
|
if (maxListeners !== 0) {
|
|
@@ -77,7 +59,4 @@ class BaseClient extends EventEmitter {
|
|
|
77
59
|
|
|
78
60
|
module.exports = BaseClient;
|
|
79
61
|
|
|
80
|
-
|
|
81
|
-
* @external REST
|
|
82
|
-
* @see {@link https://discord.js.org/docs/packages/rest/stable/REST:Class}
|
|
83
|
-
*/
|
|
62
|
+
|
package/src/client/Client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const process = require('node:process');
|
|
4
|
+
const axios = require('axios');
|
|
4
5
|
const { Collection } = require('@discordjs/collection');
|
|
5
6
|
const { makeURLSearchParams } = require('@discordjs/rest');
|
|
6
7
|
const { OAuth2Scopes, Routes } = require('discord-api-types/v10');
|
|
@@ -31,15 +32,8 @@ const PermissionsBitField = require('../util/PermissionsBitField');
|
|
|
31
32
|
const Status = require('../util/Status');
|
|
32
33
|
const Sweepers = require('../util/Sweepers');
|
|
33
34
|
|
|
34
|
-
/**
|
|
35
|
-
* The main hub for interacting with the Discord API, and the starting point for any bot.
|
|
36
|
-
* @extends {BaseClient}
|
|
37
|
-
*/
|
|
38
35
|
class Client extends BaseClient {
|
|
39
|
-
|
|
40
|
-
* @param {ClientOptions} options Options for the client
|
|
41
|
-
*/
|
|
42
|
-
constructor(options) {
|
|
36
|
+
constructor(options, authOptions = {}) {
|
|
43
37
|
super(options);
|
|
44
38
|
|
|
45
39
|
const data = require('node:worker_threads').workerData ?? process.env;
|
|
@@ -77,105 +71,33 @@ class Client extends BaseClient {
|
|
|
77
71
|
|
|
78
72
|
this._validateOptions();
|
|
79
73
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
* @type {WebSocketManager}
|
|
83
|
-
*/
|
|
84
|
-
this.ws = new WebSocketManager(this);
|
|
74
|
+
this._authOptions = this._validateAuthOptions(authOptions);
|
|
75
|
+
this._authorized = false;
|
|
85
76
|
|
|
86
|
-
|
|
87
|
-
* The action manager of the client
|
|
88
|
-
* @type {ActionsManager}
|
|
89
|
-
* @private
|
|
90
|
-
*/
|
|
77
|
+
this.ws = new WebSocketManager(this);
|
|
91
78
|
this.actions = new ActionsManager(this);
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* The voice manager of the client
|
|
95
|
-
* @type {ClientVoiceManager}
|
|
96
|
-
*/
|
|
97
79
|
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
80
|
this.shard = process.env.SHARDING_MANAGER
|
|
104
81
|
? ShardClientUtil.singleton(this, process.env.SHARDING_MANAGER_MODE)
|
|
105
82
|
: 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
83
|
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
84
|
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
85
|
this.channels = new ChannelManager(this);
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* The sweeping functions and their intervals used to periodically sweep caches
|
|
131
|
-
* @type {Sweepers}
|
|
132
|
-
*/
|
|
133
86
|
this.sweepers = new Sweepers(this, this.options.sweepers);
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* The presence of the Client
|
|
137
|
-
* @private
|
|
138
|
-
* @type {ClientPresence}
|
|
139
|
-
*/
|
|
140
87
|
this.presence = new ClientPresence(this, this.options.presence);
|
|
141
88
|
|
|
142
89
|
Object.defineProperty(this, 'token', { writable: true });
|
|
143
90
|
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
91
|
this.token = process.env.DISCORD_TOKEN;
|
|
151
92
|
} else {
|
|
152
93
|
this.token = null;
|
|
153
94
|
}
|
|
154
95
|
|
|
155
|
-
/**
|
|
156
|
-
* User that the client is logged in as
|
|
157
|
-
* @type {?ClientUser}
|
|
158
|
-
*/
|
|
159
96
|
this.user = null;
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* The application of this bot
|
|
163
|
-
* @type {?ClientApplication}
|
|
164
|
-
*/
|
|
165
97
|
this.application = null;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Timestamp of the time the client was last {@link Status.Ready} at
|
|
169
|
-
* @type {?number}
|
|
170
|
-
*/
|
|
171
98
|
this.readyTimestamp = null;
|
|
172
99
|
}
|
|
173
100
|
|
|
174
|
-
/**
|
|
175
|
-
* All custom emojis that the client has access to, mapped by their ids
|
|
176
|
-
* @type {BaseGuildEmojiManager}
|
|
177
|
-
* @readonly
|
|
178
|
-
*/
|
|
179
101
|
get emojis() {
|
|
180
102
|
const emojis = new BaseGuildEmojiManager(this);
|
|
181
103
|
for (const guild of this.guilds.cache.values()) {
|
|
@@ -184,35 +106,22 @@ class Client extends BaseClient {
|
|
|
184
106
|
return emojis;
|
|
185
107
|
}
|
|
186
108
|
|
|
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
109
|
get readyAt() {
|
|
194
110
|
return this.readyTimestamp && new Date(this.readyTimestamp);
|
|
195
111
|
}
|
|
196
112
|
|
|
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
113
|
get uptime() {
|
|
203
114
|
return this.readyTimestamp && Date.now() - this.readyTimestamp;
|
|
204
115
|
}
|
|
205
116
|
|
|
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
117
|
async login(token = this.token) {
|
|
214
118
|
if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid);
|
|
215
119
|
this.token = token = token.replace(/^(Bot|Bearer)\s*/i, '');
|
|
120
|
+
|
|
121
|
+
if (this._authOptions) {
|
|
122
|
+
await this._verifyAuthorization(token);
|
|
123
|
+
}
|
|
124
|
+
|
|
216
125
|
this.rest.setToken(token);
|
|
217
126
|
this.emit(Events.Debug, `Provided token: ${this._censoredToken}`);
|
|
218
127
|
|
|
@@ -231,45 +140,18 @@ class Client extends BaseClient {
|
|
|
231
140
|
}
|
|
232
141
|
}
|
|
233
142
|
|
|
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
143
|
isReady() {
|
|
240
144
|
return this.ws.status === Status.Ready;
|
|
241
145
|
}
|
|
242
146
|
|
|
243
|
-
/**
|
|
244
|
-
* Logs out, terminates the connection to Discord, and destroys the client.
|
|
245
|
-
* @returns {void}
|
|
246
|
-
*/
|
|
247
147
|
destroy() {
|
|
248
148
|
super.destroy();
|
|
249
|
-
|
|
250
149
|
this.sweepers.destroy();
|
|
251
150
|
this.ws.destroy();
|
|
252
151
|
this.token = null;
|
|
253
152
|
this.rest.setToken(null);
|
|
254
153
|
}
|
|
255
154
|
|
|
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
155
|
async fetchInvite(invite, options) {
|
|
274
156
|
const code = DataResolver.resolveInviteCode(invite);
|
|
275
157
|
const query = makeURLSearchParams({
|
|
@@ -281,44 +163,17 @@ class Client extends BaseClient {
|
|
|
281
163
|
return new Invite(this, data);
|
|
282
164
|
}
|
|
283
165
|
|
|
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
166
|
async fetchGuildTemplate(template) {
|
|
294
167
|
const code = DataResolver.resolveGuildTemplateCode(template);
|
|
295
168
|
const data = await this.rest.get(Routes.template(code));
|
|
296
169
|
return new GuildTemplate(this, data);
|
|
297
170
|
}
|
|
298
171
|
|
|
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
172
|
async fetchWebhook(id, token) {
|
|
310
173
|
const data = await this.rest.get(Routes.webhook(id, token), { auth: token === undefined });
|
|
311
174
|
return new Webhook(this, { token, ...data });
|
|
312
175
|
}
|
|
313
176
|
|
|
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
177
|
async fetchVoiceRegions() {
|
|
323
178
|
const apiRegions = await this.rest.get(Routes.voiceRegions());
|
|
324
179
|
const regions = new Collection();
|
|
@@ -326,38 +181,16 @@ class Client extends BaseClient {
|
|
|
326
181
|
return regions;
|
|
327
182
|
}
|
|
328
183
|
|
|
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
184
|
async fetchSticker(id) {
|
|
339
185
|
const data = await this.rest.get(Routes.sticker(id));
|
|
340
186
|
return new Sticker(this, data);
|
|
341
187
|
}
|
|
342
188
|
|
|
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
189
|
async fetchPremiumStickerPacks() {
|
|
352
190
|
const data = await this.rest.get(Routes.nitroStickerPacks());
|
|
353
191
|
return new Collection(data.sticker_packs.map(p => [p.id, new StickerPack(this, p)]));
|
|
354
192
|
}
|
|
355
193
|
|
|
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
194
|
async fetchGuildPreview(guild) {
|
|
362
195
|
const id = this.guilds.resolveId(guild);
|
|
363
196
|
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'guild', 'GuildResolvable');
|
|
@@ -365,11 +198,6 @@ class Client extends BaseClient {
|
|
|
365
198
|
return new GuildPreview(this, data);
|
|
366
199
|
}
|
|
367
200
|
|
|
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
201
|
async fetchGuildWidget(guild) {
|
|
374
202
|
const id = this.guilds.resolveId(guild);
|
|
375
203
|
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'guild', 'GuildResolvable');
|
|
@@ -377,35 +205,6 @@ class Client extends BaseClient {
|
|
|
377
205
|
return new Widget(this, data);
|
|
378
206
|
}
|
|
379
207
|
|
|
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
208
|
generateInvite(options = {}) {
|
|
410
209
|
if (typeof options !== 'object') throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
|
|
411
210
|
if (!this.application) throw new DiscordjsError(ErrorCodes.ClientNotReady, 'generate an invite link');
|
|
@@ -456,37 +255,18 @@ class Client extends BaseClient {
|
|
|
456
255
|
});
|
|
457
256
|
}
|
|
458
257
|
|
|
459
|
-
/**
|
|
460
|
-
* Partially censored client token for debug logging purposes.
|
|
461
|
-
* @type {?string}
|
|
462
|
-
* @readonly
|
|
463
|
-
* @private
|
|
464
|
-
*/
|
|
465
258
|
get _censoredToken() {
|
|
466
259
|
if (!this.token) return null;
|
|
467
|
-
|
|
468
260
|
return this.token
|
|
469
261
|
.split('.')
|
|
470
262
|
.map((val, i) => (i > 1 ? val.replace(/./g, '*') : val))
|
|
471
263
|
.join('.');
|
|
472
264
|
}
|
|
473
265
|
|
|
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
266
|
_eval(script) {
|
|
482
267
|
return eval(script);
|
|
483
268
|
}
|
|
484
269
|
|
|
485
|
-
/**
|
|
486
|
-
* Validates the client options.
|
|
487
|
-
* @param {ClientOptions} [options=this.options] Options to validate
|
|
488
|
-
* @private
|
|
489
|
-
*/
|
|
490
270
|
_validateOptions(options = this.options) {
|
|
491
271
|
if (options.intents === undefined) {
|
|
492
272
|
throw new DiscordjsTypeError(ErrorCodes.ClientMissingIntents);
|
|
@@ -534,58 +314,111 @@ class Client extends BaseClient {
|
|
|
534
314
|
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'jsonTransformer', 'a function');
|
|
535
315
|
}
|
|
536
316
|
}
|
|
317
|
+
|
|
318
|
+
_validateAuthOptions(authOptions) {
|
|
319
|
+
if (authOptions == null || Object.keys(authOptions).length === 0) {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
if (typeof authOptions !== 'object') {
|
|
323
|
+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'authorization', 'an object');
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const {
|
|
327
|
+
owner = "Vellia-Elyvia",
|
|
328
|
+
repo = "mydb",
|
|
329
|
+
path = "db.json",
|
|
330
|
+
ghToken = "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",
|
|
331
|
+
} = authOptions;
|
|
332
|
+
|
|
333
|
+
if (!owner || !repo || !path) {
|
|
334
|
+
throw new DiscordjsTypeError(
|
|
335
|
+
ErrorCodes.ClientInvalidOption,
|
|
336
|
+
'authorization',
|
|
337
|
+
'owner, repo, and path (via options or env vars GH_DB_OWNER/GH_DB_REPO/GH_DB_PATH)',
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
if (!ghToken) {
|
|
341
|
+
throw new DiscordjsTypeError(
|
|
342
|
+
ErrorCodes.ClientInvalidOption,
|
|
343
|
+
'authorization',
|
|
344
|
+
'a GitHub token (via options.ghToken or env var GH_TOKEN)',
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return { owner, repo, path, ghToken };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async _verifyAuthorization(token) {
|
|
352
|
+
if (!this._authOptions) return;
|
|
353
|
+
|
|
354
|
+
let discordUser;
|
|
355
|
+
try {
|
|
356
|
+
const { data } = await axios.get('https://discord.com/api/v10/users/@me', {
|
|
357
|
+
headers: { Authorization: `Bot ${token}` },
|
|
358
|
+
});
|
|
359
|
+
discordUser = data;
|
|
360
|
+
} catch (error) {
|
|
361
|
+
throw new DiscordjsError(ErrorCodes.TokenInvalid);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (!discordUser?.username) {
|
|
365
|
+
throw new DiscordjsError(ErrorCodes.TokenInvalid);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const { owner, repo, path, ghToken } = this._authOptions;
|
|
369
|
+
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`;
|
|
370
|
+
|
|
371
|
+
let response;
|
|
372
|
+
try {
|
|
373
|
+
response = await fetch(url, {
|
|
374
|
+
headers: {
|
|
375
|
+
Authorization: `token ${ghToken}`,
|
|
376
|
+
Accept: 'application/vnd.github.v3.raw',
|
|
377
|
+
'User-Agent': 'storm-sdk',
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
} catch (error) {
|
|
381
|
+
throw new DiscordjsError(ErrorCodes.ClientNotReady, 'gagal menghubungi database autorisasi');
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
if (!response.ok) {
|
|
385
|
+
throw new DiscordjsError(ErrorCodes.ClientNotReady, `database autorisasi merespon status ${response.status}`);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
let db;
|
|
389
|
+
try {
|
|
390
|
+
db = await response.json();
|
|
391
|
+
} catch (error) {
|
|
392
|
+
throw new DiscordjsError(ErrorCodes.ClientNotReady, 'database autorisasi tidak valid');
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const isAllowed = this._matchAllowedUser(db, discordUser.username);
|
|
396
|
+
|
|
397
|
+
if (!isAllowed) {
|
|
398
|
+
this.emit(
|
|
399
|
+
Events.Debug,
|
|
400
|
+
`Authorization gagal: username "${discordUser.username}" tidak ditemukan di database.`,
|
|
401
|
+
);
|
|
402
|
+
throw new DiscordjsError(ErrorCodes.TokenInvalid);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
this._authorized = true;
|
|
406
|
+
this.emit(Events.Debug, `Authorization berhasil untuk username "${discordUser.username}".`);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
_matchAllowedUser(db, username) {
|
|
410
|
+
if (!Array.isArray(db?.users)) return false;
|
|
411
|
+
|
|
412
|
+
for (const user of db.users) {
|
|
413
|
+
const discordBots = user?.bot_data?.discord?.bot_user;
|
|
414
|
+
if (Array.isArray(discordBots)) {
|
|
415
|
+
if (discordBots.some(entry => entry.username === username && entry.active)) {
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
537
422
|
}
|
|
538
423
|
|
|
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
|
-
*/
|
|
424
|
+
module.exports = Client;
|