discord.js-selfv13 13.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of discord.js-selfv13 might be problematic. Click here for more details.
- package/.dccache +1 -0
- package/LICENSE +190 -0
- package/README.md +87 -0
- package/browser.js +9 -0
- package/deploy/deploy-key.enc +0 -0
- package/deploy/deploy.sh +90 -0
- package/deploy/test.sh +34 -0
- package/docs/README.md +1 -0
- package/docs/examples/attachments.md +163 -0
- package/docs/examples/avatars.js +29 -0
- package/docs/examples/embed.js +38 -0
- package/docs/examples/greeting.js +30 -0
- package/docs/examples/moderation.md +145 -0
- package/docs/examples/ping.js +29 -0
- package/docs/examples/webhook.js +12 -0
- package/docs/general/faq.md +23 -0
- package/docs/general/updating.md +181 -0
- package/docs/general/welcome.md +95 -0
- package/docs/index.yml +30 -0
- package/docs/logo.svg +19 -0
- package/docs/topics/voice.md +113 -0
- package/docs/topics/web.md +38 -0
- package/package.json +147 -0
- package/src/client/Client.js +564 -0
- package/src/client/ClientDataManager.js +150 -0
- package/src/client/ClientDataResolver.js +376 -0
- package/src/client/ClientManager.js +74 -0
- package/src/client/WebhookClient.js +118 -0
- package/src/client/actions/Action.js +23 -0
- package/src/client/actions/ActionsManager.js +40 -0
- package/src/client/actions/ChannelCreate.js +11 -0
- package/src/client/actions/ChannelDelete.js +30 -0
- package/src/client/actions/ChannelUpdate.js +74 -0
- package/src/client/actions/GuildBanRemove.js +13 -0
- package/src/client/actions/GuildChannelsPositionUpdate.js +19 -0
- package/src/client/actions/GuildDelete.js +57 -0
- package/src/client/actions/GuildEmojiCreate.js +17 -0
- package/src/client/actions/GuildEmojiDelete.js +18 -0
- package/src/client/actions/GuildEmojiUpdate.js +17 -0
- package/src/client/actions/GuildEmojisUpdate.js +38 -0
- package/src/client/actions/GuildMemberGet.js +10 -0
- package/src/client/actions/GuildMemberRemove.js +41 -0
- package/src/client/actions/GuildRoleCreate.js +26 -0
- package/src/client/actions/GuildRoleDelete.js +42 -0
- package/src/client/actions/GuildRoleUpdate.js +41 -0
- package/src/client/actions/GuildRolesPositionUpdate.js +19 -0
- package/src/client/actions/GuildSync.js +29 -0
- package/src/client/actions/GuildUpdate.js +34 -0
- package/src/client/actions/MessageCreate.js +53 -0
- package/src/client/actions/MessageDelete.js +35 -0
- package/src/client/actions/MessageDeleteBulk.js +26 -0
- package/src/client/actions/MessageReactionAdd.js +37 -0
- package/src/client/actions/MessageReactionRemove.js +37 -0
- package/src/client/actions/MessageReactionRemoveAll.js +25 -0
- package/src/client/actions/MessageUpdate.js +40 -0
- package/src/client/actions/Ready.js +1 -0
- package/src/client/actions/Ready.js.bak +65 -0
- package/src/client/actions/UserGet.js +11 -0
- package/src/client/actions/UserNoteUpdate.js +30 -0
- package/src/client/actions/UserUpdate.js +33 -0
- package/src/client/rest/APIRequest.js +56 -0
- package/src/client/rest/DiscordAPIError.js +60 -0
- package/src/client/rest/RESTManager.js +58 -0
- package/src/client/rest/RESTMethods.js +1006 -0
- package/src/client/rest/RequestHandlers/Burst.js +90 -0
- package/src/client/rest/RequestHandlers/RequestHandler.js +54 -0
- package/src/client/rest/RequestHandlers/Sequential.js +132 -0
- package/src/client/rest/UserAgentManager.js +25 -0
- package/src/client/voice/ClientVoiceManager.js +81 -0
- package/src/client/voice/VoiceBroadcast.js +366 -0
- package/src/client/voice/VoiceConnection.js +530 -0
- package/src/client/voice/VoiceUDPClient.js +127 -0
- package/src/client/voice/VoiceWebSocket.js +246 -0
- package/src/client/voice/dispatcher/StreamDispatcher.js +331 -0
- package/src/client/voice/opus/BaseOpusEngine.js +60 -0
- package/src/client/voice/opus/NodeOpusEngine.js +40 -0
- package/src/client/voice/opus/OpusEngineList.js +28 -0
- package/src/client/voice/opus/OpusScriptEngine.js +45 -0
- package/src/client/voice/player/AudioPlayer.js +170 -0
- package/src/client/voice/receiver/VoiceReadable.js +17 -0
- package/src/client/voice/receiver/VoiceReceiver.js +219 -0
- package/src/client/voice/util/SecretKey.js +16 -0
- package/src/client/voice/util/Secretbox.js +33 -0
- package/src/client/voice/util/VolumeInterface.js +86 -0
- package/src/client/websocket/WebSocketConnection.js +506 -0
- package/src/client/websocket/WebSocketManager.js +90 -0
- package/src/client/websocket/packets/WebSocketPacketManager.js +110 -0
- package/src/client/websocket/packets/handlers/AbstractHandler.js +11 -0
- package/src/client/websocket/packets/handlers/ChannelCreate.js +17 -0
- package/src/client/websocket/packets/handlers/ChannelDelete.js +20 -0
- package/src/client/websocket/packets/handlers/ChannelPinsUpdate.js +37 -0
- package/src/client/websocket/packets/handlers/ChannelUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildBanAdd.js +23 -0
- package/src/client/websocket/packets/handlers/GuildBanRemove.js +20 -0
- package/src/client/websocket/packets/handlers/GuildCreate.js +22 -0
- package/src/client/websocket/packets/handlers/GuildDelete.js +19 -0
- package/src/client/websocket/packets/handlers/GuildEmojisUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildIntegrationsUpdate.js +19 -0
- package/src/client/websocket/packets/handlers/GuildMemberAdd.js +17 -0
- package/src/client/websocket/packets/handlers/GuildMemberRemove.js +13 -0
- package/src/client/websocket/packets/handlers/GuildMemberUpdate.js +18 -0
- package/src/client/websocket/packets/handlers/GuildMembersChunk.js +33 -0
- package/src/client/websocket/packets/handlers/GuildRoleCreate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildRoleDelete.js +11 -0
- package/src/client/websocket/packets/handlers/GuildRoleUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/GuildSync.js +11 -0
- package/src/client/websocket/packets/handlers/GuildUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/MessageCreate.js +19 -0
- package/src/client/websocket/packets/handlers/MessageDelete.js +19 -0
- package/src/client/websocket/packets/handlers/MessageDeleteBulk.js +17 -0
- package/src/client/websocket/packets/handlers/MessageReactionAdd.js +11 -0
- package/src/client/websocket/packets/handlers/MessageReactionRemove.js +11 -0
- package/src/client/websocket/packets/handlers/MessageReactionRemoveAll.js +11 -0
- package/src/client/websocket/packets/handlers/MessageUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/PresenceUpdate.js +76 -0
- package/src/client/websocket/packets/handlers/Ready.js +83 -0
- package/src/client/websocket/packets/handlers/RelationshipAdd.js +19 -0
- package/src/client/websocket/packets/handlers/RelationshipRemove.js +19 -0
- package/src/client/websocket/packets/handlers/Resumed.js +28 -0
- package/src/client/websocket/packets/handlers/TypingStart.js +68 -0
- package/src/client/websocket/packets/handlers/UserGuildSettingsUpdate.js +21 -0
- package/src/client/websocket/packets/handlers/UserNoteUpdate.js +12 -0
- package/src/client/websocket/packets/handlers/UserSettingsUpdate.js +18 -0
- package/src/client/websocket/packets/handlers/UserUpdate.js +11 -0
- package/src/client/websocket/packets/handlers/VoiceServerUpdate.js +19 -0
- package/src/client/websocket/packets/handlers/VoiceStateUpdate.js +52 -0
- package/src/client/websocket/packets/handlers/WebhooksUpdate.js +19 -0
- package/src/index.js +66 -0
- package/src/sharding/Shard.js +282 -0
- package/src/sharding/ShardClientUtil.js +146 -0
- package/src/sharding/ShardingManager.js +220 -0
- package/src/structures/Attachment.js +75 -0
- package/src/structures/CategoryChannel.js +22 -0
- package/src/structures/Channel.js +78 -0
- package/src/structures/ClientUser.js +447 -0
- package/src/structures/ClientUserChannelOverride.js +30 -0
- package/src/structures/ClientUserGuildSettings.js +60 -0
- package/src/structures/ClientUserSettings.js +80 -0
- package/src/structures/DMChannel.js +76 -0
- package/src/structures/Emoji.js +256 -0
- package/src/structures/GroupDMChannel.js +246 -0
- package/src/structures/Guild.js +1461 -0
- package/src/structures/GuildAuditLogs.js +371 -0
- package/src/structures/GuildChannel.js +537 -0
- package/src/structures/GuildMember.js +613 -0
- package/src/structures/Invite.js +164 -0
- package/src/structures/Message.js +605 -0
- package/src/structures/MessageAttachment.js +68 -0
- package/src/structures/MessageCollector.js +100 -0
- package/src/structures/MessageEmbed.js +386 -0
- package/src/structures/MessageMentions.js +144 -0
- package/src/structures/MessageReaction.js +96 -0
- package/src/structures/NewsChannel.js +24 -0
- package/src/structures/OAuth2Application.js +148 -0
- package/src/structures/PartialGuild.js +51 -0
- package/src/structures/PartialGuildChannel.js +44 -0
- package/src/structures/PermissionOverwrites.js +69 -0
- package/src/structures/Presence.js +241 -0
- package/src/structures/ReactionCollector.js +85 -0
- package/src/structures/ReactionEmoji.js +49 -0
- package/src/structures/RichEmbed.js +295 -0
- package/src/structures/Role.js +376 -0
- package/src/structures/StoreChannel.js +25 -0
- package/src/structures/TextChannel.js +154 -0
- package/src/structures/User.js +329 -0
- package/src/structures/UserConnection.js +48 -0
- package/src/structures/UserProfile.js +62 -0
- package/src/structures/VoiceChannel.js +146 -0
- package/src/structures/VoiceRegion.js +50 -0
- package/src/structures/Webhook.js +304 -0
- package/src/structures/interfaces/Collector.js +179 -0
- package/src/structures/interfaces/TextBasedChannel.js +635 -0
- package/src/structures/shared/resolvePermissions.js +26 -0
- package/src/util/Collection.js +532 -0
- package/src/util/Constants.js +845 -0
- package/src/util/Permissions.js +306 -0
- package/src/util/Snowflake.js +82 -0
- package/src/util/Util.js +221 -0
- package/test/random.js +207 -0
- package/test/shard.js +31 -0
- package/test/sharder.js +7 -0
- package/test/voice.js +78 -0
- package/test/webpack.html +31 -0
- package/tsconfig.json +13 -0
- package/tslint.json +62 -0
- package/typings/discord.js-test.ts +69 -0
- package/typings/index.d.ts +2190 -0
- package/webpack.config.js +62 -0
@@ -0,0 +1,146 @@
|
|
1
|
+
const Util = require('../util/Util');
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Helper class for sharded clients spawned as a child process, such as from a ShardingManager.
|
5
|
+
*/
|
6
|
+
class ShardClientUtil {
|
7
|
+
/**
|
8
|
+
* @param {Client} client The client of the current shard
|
9
|
+
*/
|
10
|
+
constructor(client) {
|
11
|
+
this.client = client;
|
12
|
+
process.on('message', this._handleMessage.bind(this));
|
13
|
+
client.on('ready', () => { process.send({ _ready: true }); });
|
14
|
+
client.on('disconnect', () => { process.send({ _disconnect: true }); });
|
15
|
+
client.on('reconnecting', () => { process.send({ _reconnecting: true }); });
|
16
|
+
}
|
17
|
+
|
18
|
+
/**
|
19
|
+
* ID of this shard
|
20
|
+
* @type {number}
|
21
|
+
* @readonly
|
22
|
+
*/
|
23
|
+
get id() {
|
24
|
+
return this.client.options.shardId;
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Total number of shards
|
29
|
+
* @type {number}
|
30
|
+
* @readonly
|
31
|
+
*/
|
32
|
+
get count() {
|
33
|
+
return this.client.options.shardCount;
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Sends a message to the master process.
|
38
|
+
* @param {*} message Message to send
|
39
|
+
* @returns {Promise<void>}
|
40
|
+
*/
|
41
|
+
send(message) {
|
42
|
+
return new Promise((resolve, reject) => {
|
43
|
+
process.send(message, err => {
|
44
|
+
if (err) reject(err); else resolve();
|
45
|
+
});
|
46
|
+
});
|
47
|
+
}
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Fetches a client property value of each shard.
|
51
|
+
* @param {string} prop Name of the client property to get, using periods for nesting
|
52
|
+
* @returns {Promise<Array>}
|
53
|
+
* @example
|
54
|
+
* client.shard.fetchClientValues('guilds.size')
|
55
|
+
* .then(results => {
|
56
|
+
* console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`);
|
57
|
+
* })
|
58
|
+
* .catch(console.error);
|
59
|
+
*/
|
60
|
+
fetchClientValues(prop) {
|
61
|
+
return new Promise((resolve, reject) => {
|
62
|
+
const listener = message => {
|
63
|
+
if (!message || message._sFetchProp !== prop) return;
|
64
|
+
process.removeListener('message', listener);
|
65
|
+
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error));
|
66
|
+
};
|
67
|
+
process.on('message', listener);
|
68
|
+
|
69
|
+
this.send({ _sFetchProp: prop }).catch(err => {
|
70
|
+
process.removeListener('message', listener);
|
71
|
+
reject(err);
|
72
|
+
});
|
73
|
+
});
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Evaluates a script on all shards, in the context of the Clients.
|
78
|
+
* @param {string} script JavaScript to run on each shard
|
79
|
+
* @returns {Promise<Array>} Results of the script execution
|
80
|
+
*/
|
81
|
+
broadcastEval(script) {
|
82
|
+
return new Promise((resolve, reject) => {
|
83
|
+
const listener = message => {
|
84
|
+
if (!message || message._sEval !== script) return;
|
85
|
+
process.removeListener('message', listener);
|
86
|
+
if (!message._error) resolve(message._result); else reject(Util.makeError(message._error));
|
87
|
+
};
|
88
|
+
process.on('message', listener);
|
89
|
+
|
90
|
+
this.send({ _sEval: script }).catch(err => {
|
91
|
+
process.removeListener('message', listener);
|
92
|
+
reject(err);
|
93
|
+
});
|
94
|
+
});
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Handles an IPC message.
|
99
|
+
* @param {*} message Message received
|
100
|
+
* @private
|
101
|
+
*/
|
102
|
+
_handleMessage(message) {
|
103
|
+
if (!message) return;
|
104
|
+
if (message._fetchProp) {
|
105
|
+
const props = message._fetchProp.split('.');
|
106
|
+
let value = this.client;
|
107
|
+
for (const prop of props) value = value[prop];
|
108
|
+
this._respond('fetchProp', { _fetchProp: message._fetchProp, _result: value });
|
109
|
+
} else if (message._eval) {
|
110
|
+
try {
|
111
|
+
this._respond('eval', { _eval: message._eval, _result: this.client._eval(message._eval) });
|
112
|
+
} catch (err) {
|
113
|
+
this._respond('eval', { _eval: message._eval, _error: Util.makePlainError(err) });
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
/**
|
119
|
+
* Sends a message to the master process, emitting an error from the client upon failure.
|
120
|
+
* @param {string} type Type of response to send
|
121
|
+
* @param {*} message Message to send
|
122
|
+
* @private
|
123
|
+
*/
|
124
|
+
_respond(type, message) {
|
125
|
+
this.send(message).catch(err => {
|
126
|
+
err.message = `Error when sending ${type} response to master process: ${err.message}`;
|
127
|
+
this.client.emit('error', err);
|
128
|
+
});
|
129
|
+
}
|
130
|
+
|
131
|
+
/**
|
132
|
+
* Creates/gets the singleton of this class.
|
133
|
+
* @param {Client} client The client to use
|
134
|
+
* @returns {ShardClientUtil}
|
135
|
+
*/
|
136
|
+
static singleton(client) {
|
137
|
+
if (!this._singleton) {
|
138
|
+
this._singleton = new this(client);
|
139
|
+
} else {
|
140
|
+
client.emit('warn', 'Multiple clients created in child process; only the first will handle sharding helpers.');
|
141
|
+
}
|
142
|
+
return this._singleton;
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
module.exports = ShardClientUtil;
|
@@ -0,0 +1,220 @@
|
|
1
|
+
const path = require('path');
|
2
|
+
const fs = require('fs');
|
3
|
+
const EventEmitter = require('events').EventEmitter;
|
4
|
+
const Shard = require('./Shard');
|
5
|
+
const Collection = require('../util/Collection');
|
6
|
+
const Util = require('../util/Util');
|
7
|
+
|
8
|
+
/**
|
9
|
+
* This is a utility class that can be used to help you spawn shards of your client. Each shard is completely separate
|
10
|
+
* from the other. The Shard Manager takes a path to a file and spawns it under the specified amount of shards safely.
|
11
|
+
* If you do not select an amount of shards, the manager will automatically decide the best amount.
|
12
|
+
* @extends {EventEmitter}
|
13
|
+
*/
|
14
|
+
class ShardingManager extends EventEmitter {
|
15
|
+
/**
|
16
|
+
* @param {string} file Path to your shard script file
|
17
|
+
* @param {Object} [options] Options for the sharding manager
|
18
|
+
* @param {number|string} [options.totalShards='auto'] Number of shards to spawn, or "auto"
|
19
|
+
* @param {boolean} [options.respawn=true] Whether shards should automatically respawn upon exiting
|
20
|
+
* @param {string[]} [options.shardArgs=[]] Arguments to pass to the shard script when spawning
|
21
|
+
* @param {string} [options.token] Token to use for automatic shard count and passing to shards
|
22
|
+
*/
|
23
|
+
constructor(file, options = {}) {
|
24
|
+
super();
|
25
|
+
options = Util.mergeDefault({
|
26
|
+
totalShards: 'auto',
|
27
|
+
respawn: true,
|
28
|
+
shardArgs: [],
|
29
|
+
token: null,
|
30
|
+
}, options);
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Path to the shard script file
|
34
|
+
* @type {string}
|
35
|
+
*/
|
36
|
+
this.file = file;
|
37
|
+
if (!file) throw new Error('File must be specified.');
|
38
|
+
if (!path.isAbsolute(file)) this.file = path.resolve(process.cwd(), file);
|
39
|
+
const stats = fs.statSync(this.file);
|
40
|
+
if (!stats.isFile()) throw new Error('File path does not point to a file.');
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Amount of shards that this manager is going to spawn
|
44
|
+
* @type {number|string}
|
45
|
+
*/
|
46
|
+
this.totalShards = options.totalShards;
|
47
|
+
if (this.totalShards !== 'auto') {
|
48
|
+
if (typeof this.totalShards !== 'number' || isNaN(this.totalShards)) {
|
49
|
+
throw new TypeError('Amount of shards must be a number.');
|
50
|
+
}
|
51
|
+
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
52
|
+
if (this.totalShards !== Math.floor(this.totalShards)) {
|
53
|
+
throw new RangeError('Amount of shards must be an integer.');
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
/**
|
58
|
+
* Whether shards should automatically respawn upon exiting
|
59
|
+
* @type {boolean}
|
60
|
+
*/
|
61
|
+
this.respawn = options.respawn;
|
62
|
+
|
63
|
+
/**
|
64
|
+
* An array of arguments to pass to shards
|
65
|
+
* @type {string[]}
|
66
|
+
*/
|
67
|
+
this.shardArgs = options.shardArgs;
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Arguments for the shard's process executable
|
71
|
+
* @type {?string[]}
|
72
|
+
*/
|
73
|
+
this.execArgv = options.execArgv;
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Token to use for obtaining the automatic shard count, and passing to shards
|
77
|
+
* @type {?string}
|
78
|
+
*/
|
79
|
+
this.token = options.token ? options.token.replace(/^Bot\s*/i, '') : null;
|
80
|
+
|
81
|
+
/**
|
82
|
+
* A collection of shards that this manager has spawned
|
83
|
+
* @type {Collection<number, Shard>}
|
84
|
+
*/
|
85
|
+
this.shards = new Collection();
|
86
|
+
}
|
87
|
+
|
88
|
+
/**
|
89
|
+
* Spawns a single shard.
|
90
|
+
* @param {number} id The ID of the shard to spawn. **This is usually not necessary**
|
91
|
+
* @returns {Promise<Shard>}
|
92
|
+
*/
|
93
|
+
createShard(id = this.shards.size) {
|
94
|
+
const shard = new Shard(this, id, this.shardArgs);
|
95
|
+
this.shards.set(id, shard);
|
96
|
+
/**
|
97
|
+
* Emitted upon launching a shard.
|
98
|
+
* @event ShardingManager#launch
|
99
|
+
* @param {Shard} shard Shard that was launched
|
100
|
+
*/
|
101
|
+
this.emit('launch', shard);
|
102
|
+
return Promise.resolve(shard);
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Spawns multiple shards.
|
107
|
+
* @param {number} [amount=this.totalShards] Number of shards to spawn
|
108
|
+
* @param {number} [delay=7500] How long to wait in between spawning each shard (in milliseconds)
|
109
|
+
* @returns {Promise<Collection<number, Shard>>}
|
110
|
+
*/
|
111
|
+
spawn(amount = this.totalShards, delay = 7500) {
|
112
|
+
if (amount === 'auto') {
|
113
|
+
return Util.fetchRecommendedShards(this.token).then(count => {
|
114
|
+
this.totalShards = count;
|
115
|
+
return this._spawn(count, delay);
|
116
|
+
});
|
117
|
+
} else {
|
118
|
+
if (typeof amount !== 'number' || isNaN(amount)) throw new TypeError('Amount of shards must be a number.');
|
119
|
+
if (amount < 1) throw new RangeError('Amount of shards must be at least 1.');
|
120
|
+
if (amount !== Math.floor(amount)) throw new TypeError('Amount of shards must be an integer.');
|
121
|
+
return this._spawn(amount, delay);
|
122
|
+
}
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Actually spawns shards, unlike that poser above >:(
|
127
|
+
* @param {number} amount Number of shards to spawn
|
128
|
+
* @param {number} delay How long to wait in between spawning each shard (in milliseconds)
|
129
|
+
* @returns {Promise<Collection<number, Shard>>}
|
130
|
+
* @private
|
131
|
+
*/
|
132
|
+
_spawn(amount, delay) {
|
133
|
+
return new Promise(resolve => {
|
134
|
+
if (this.shards.size >= amount) throw new Error(`Already spawned ${this.shards.size} shards.`);
|
135
|
+
this.totalShards = amount;
|
136
|
+
|
137
|
+
this.createShard();
|
138
|
+
if (this.shards.size >= this.totalShards) {
|
139
|
+
resolve(this.shards);
|
140
|
+
return;
|
141
|
+
}
|
142
|
+
|
143
|
+
if (delay <= 0) {
|
144
|
+
while (this.shards.size < this.totalShards) this.createShard();
|
145
|
+
resolve(this.shards);
|
146
|
+
} else {
|
147
|
+
const interval = setInterval(() => {
|
148
|
+
this.createShard();
|
149
|
+
if (this.shards.size >= this.totalShards) {
|
150
|
+
clearInterval(interval);
|
151
|
+
resolve(this.shards);
|
152
|
+
}
|
153
|
+
}, delay);
|
154
|
+
}
|
155
|
+
});
|
156
|
+
}
|
157
|
+
|
158
|
+
/**
|
159
|
+
* Send a message to all shards.
|
160
|
+
* @param {*} message Message to be sent to the shards
|
161
|
+
* @returns {Promise<Shard[]>}
|
162
|
+
*/
|
163
|
+
broadcast(message) {
|
164
|
+
const promises = [];
|
165
|
+
for (const shard of this.shards.values()) promises.push(shard.send(message));
|
166
|
+
return Promise.all(promises);
|
167
|
+
}
|
168
|
+
|
169
|
+
/**
|
170
|
+
* Evaluates a script on all shards, in the context of the Clients.
|
171
|
+
* @param {string} script JavaScript to run on each shard
|
172
|
+
* @returns {Promise<Array>} Results of the script execution
|
173
|
+
*/
|
174
|
+
broadcastEval(script) {
|
175
|
+
const promises = [];
|
176
|
+
for (const shard of this.shards.values()) promises.push(shard.eval(script));
|
177
|
+
return Promise.all(promises);
|
178
|
+
}
|
179
|
+
|
180
|
+
/**
|
181
|
+
* Fetches a client property value of each shard.
|
182
|
+
* @param {string} prop Name of the client property to get, using periods for nesting
|
183
|
+
* @returns {Promise<Array>}
|
184
|
+
* @example
|
185
|
+
* manager.fetchClientValues('guilds.size')
|
186
|
+
* .then(results => {
|
187
|
+
* console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`);
|
188
|
+
* })
|
189
|
+
* .catch(console.error);
|
190
|
+
*/
|
191
|
+
fetchClientValues(prop) {
|
192
|
+
if (this.shards.size === 0) return Promise.reject(new Error('No shards have been spawned.'));
|
193
|
+
if (this.shards.size !== this.totalShards) return Promise.reject(new Error('Still spawning shards.'));
|
194
|
+
const promises = [];
|
195
|
+
for (const shard of this.shards.values()) promises.push(shard.fetchClientValue(prop));
|
196
|
+
return Promise.all(promises);
|
197
|
+
}
|
198
|
+
|
199
|
+
/**
|
200
|
+
* Kills all running shards and respawns them.
|
201
|
+
* @param {number} [shardDelay=5000] How long to wait between shards (in milliseconds)
|
202
|
+
* @param {number} [respawnDelay=500] How long to wait between killing a shard's process and restarting it
|
203
|
+
* (in milliseconds)
|
204
|
+
* @param {boolean} [waitForReady=true] Whether to wait for a shard to become ready before continuing to another
|
205
|
+
* @param {number} [currentShardIndex=0] The shard index to start respawning at
|
206
|
+
* @returns {Promise<Collection<number, Shard>>}
|
207
|
+
*/
|
208
|
+
respawnAll(shardDelay = 5000, respawnDelay = 500, waitForReady = true, currentShardIndex = 0) {
|
209
|
+
let s = 0;
|
210
|
+
const shard = this.shards.get(currentShardIndex);
|
211
|
+
const promises = [shard.respawn(respawnDelay, waitForReady)];
|
212
|
+
if (++s < this.shards.size && shardDelay > 0) promises.push(Util.delayFor(shardDelay));
|
213
|
+
return Promise.all(promises).then(() => {
|
214
|
+
if (++currentShardIndex === this.shards.size) return this.shards;
|
215
|
+
return this.respawnAll(shardDelay, respawnDelay, waitForReady, currentShardIndex);
|
216
|
+
});
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
module.exports = ShardingManager;
|
@@ -0,0 +1,75 @@
|
|
1
|
+
/**
|
2
|
+
* Represents an attachment in a message.
|
3
|
+
* @param {BufferResolvable|Stream} file The file
|
4
|
+
* @param {string} [name] The name of the file, if any
|
5
|
+
*/
|
6
|
+
class Attachment {
|
7
|
+
constructor(file, name) {
|
8
|
+
this.file = null;
|
9
|
+
if (name) this.setAttachment(file, name);
|
10
|
+
else this._attach(file);
|
11
|
+
}
|
12
|
+
|
13
|
+
/**
|
14
|
+
* The name of the file
|
15
|
+
* @type {?string}
|
16
|
+
* @readonly
|
17
|
+
*/
|
18
|
+
get name() {
|
19
|
+
return this.file.name;
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* The file
|
24
|
+
* @type {?BufferResolvable|Stream}
|
25
|
+
* @readonly
|
26
|
+
*/
|
27
|
+
get attachment() {
|
28
|
+
return this.file.attachment;
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Set the file of this attachment.
|
33
|
+
* @param {BufferResolvable|Stream} file The file
|
34
|
+
* @param {string} name The name of the file
|
35
|
+
* @returns {Attachment} This attachment
|
36
|
+
*/
|
37
|
+
setAttachment(file, name) {
|
38
|
+
this.file = { attachment: file, name };
|
39
|
+
return this;
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Set the file of this attachment.
|
44
|
+
* @param {BufferResolvable|Stream} attachment The file
|
45
|
+
* @returns {Attachment} This attachment
|
46
|
+
*/
|
47
|
+
setFile(attachment) {
|
48
|
+
this.file = { attachment };
|
49
|
+
return this;
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Set the name of this attachment.
|
54
|
+
* @param {string} name The name of the image
|
55
|
+
* @returns {Attachment} This attachment
|
56
|
+
*/
|
57
|
+
setName(name) {
|
58
|
+
this.file.name = name;
|
59
|
+
return this;
|
60
|
+
}
|
61
|
+
|
62
|
+
/**
|
63
|
+
* Set the file of this attachment.
|
64
|
+
* @param {BufferResolvable|Stream} file The file
|
65
|
+
* @param {string} name The name of the file
|
66
|
+
* @returns {void}
|
67
|
+
* @private
|
68
|
+
*/
|
69
|
+
_attach(file, name) {
|
70
|
+
if (typeof file === 'string') this.file = file;
|
71
|
+
else this.setAttachment(file, name);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
module.exports = Attachment;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
const GuildChannel = require('./GuildChannel');
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Represents a guild category channel on Discord.
|
5
|
+
* @extends {GuildChannel}
|
6
|
+
*/
|
7
|
+
class CategoryChannel extends GuildChannel {
|
8
|
+
constructor(guild, data) {
|
9
|
+
super(guild, data);
|
10
|
+
this.type = 'category';
|
11
|
+
}
|
12
|
+
/**
|
13
|
+
* The channels that are part of this category
|
14
|
+
* @type {?Collection<Snowflake, GuildChannel>}
|
15
|
+
* @readonly
|
16
|
+
*/
|
17
|
+
get children() {
|
18
|
+
return this.guild.channels.filter(c => c.parentID === this.id);
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
module.exports = CategoryChannel;
|
@@ -0,0 +1,78 @@
|
|
1
|
+
const Snowflake = require('../util/Snowflake');
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Represents any channel on Discord.
|
5
|
+
*/
|
6
|
+
class Channel {
|
7
|
+
constructor(client, data) {
|
8
|
+
/**
|
9
|
+
* The client that instantiated the Channel
|
10
|
+
* @name Channel#client
|
11
|
+
* @type {Client}
|
12
|
+
* @readonly
|
13
|
+
*/
|
14
|
+
Object.defineProperty(this, 'client', { value: client });
|
15
|
+
|
16
|
+
/**
|
17
|
+
* The type of the channel, either:
|
18
|
+
* * `dm` - a DM channel
|
19
|
+
* * `group` - a Group DM channel
|
20
|
+
* * `text` - a guild text channel
|
21
|
+
* * `voice` - a guild voice channel
|
22
|
+
* * `category` - a guild category channel
|
23
|
+
* * `news` - a guild news channel
|
24
|
+
* * `store` - a guild store channel
|
25
|
+
* @type {string}
|
26
|
+
*/
|
27
|
+
this.type = null;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Whether the channel has been deleted
|
31
|
+
* @type {boolean}
|
32
|
+
*/
|
33
|
+
this.deleted = false;
|
34
|
+
|
35
|
+
if (data) this.setup(data);
|
36
|
+
}
|
37
|
+
|
38
|
+
setup(data) {
|
39
|
+
/**
|
40
|
+
* The unique ID of the channel
|
41
|
+
* @type {Snowflake}
|
42
|
+
*/
|
43
|
+
this.id = data.id;
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* The timestamp the channel was created at
|
48
|
+
* @type {number}
|
49
|
+
* @readonly
|
50
|
+
*/
|
51
|
+
get createdTimestamp() {
|
52
|
+
return Snowflake.deconstruct(this.id).timestamp;
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* The time the channel was created
|
57
|
+
* @type {Date}
|
58
|
+
* @readonly
|
59
|
+
*/
|
60
|
+
get createdAt() {
|
61
|
+
return new Date(this.createdTimestamp);
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Deletes the channel.
|
66
|
+
* @returns {Promise<Channel>}
|
67
|
+
* @example
|
68
|
+
* // Delete the channel
|
69
|
+
* channel.delete()
|
70
|
+
* .then(console.log)
|
71
|
+
* .catch(console.error);
|
72
|
+
*/
|
73
|
+
delete() {
|
74
|
+
return this.client.rest.methods.deleteChannel(this);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
module.exports = Channel;
|