mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Experimental ClientOptions.fetch_all_members
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -35,7 +35,7 @@ class ClientManager {
|
||||
})
|
||||
.catch(reject);
|
||||
|
||||
setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 15);
|
||||
setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 300);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -62,6 +62,8 @@ class WebSocketManager {
|
||||
this.ws.onclose = (d) => this.eventClose(d);
|
||||
this.ws.onmessage = (e) => this.eventMessage(e);
|
||||
this.ws.onerror = (e) => this.eventError(e);
|
||||
this._queue = [];
|
||||
this._remaining = 3;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,8 +72,23 @@ class WebSocketManager {
|
||||
* @returns {null}
|
||||
*/
|
||||
send(data) {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.send(JSON.stringify(data));
|
||||
this._queue.push(JSON.stringify(data));
|
||||
this.doQueue();
|
||||
}
|
||||
|
||||
doQueue() {
|
||||
const item = this._queue[0];
|
||||
if (this.ws.readyState === WebSocket.OPEN && item) {
|
||||
if (this._remaining === 0) {
|
||||
return setTimeout(() => {
|
||||
this.doQueue();
|
||||
}, 1000);
|
||||
}
|
||||
this._remaining--;
|
||||
this.ws.send(item);
|
||||
this._queue.shift();
|
||||
this.doQueue();
|
||||
setTimeout(() => this._remaining++, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +144,7 @@ class WebSocketManager {
|
||||
* @returns {null}
|
||||
*/
|
||||
eventClose(event) {
|
||||
console.log(event.code);
|
||||
if (event.code === 4004) {
|
||||
throw Constants.Errors.BAD_LOGIN;
|
||||
}
|
||||
@@ -172,27 +190,38 @@ class WebSocketManager {
|
||||
this.tryReconnect();
|
||||
}
|
||||
|
||||
_emitReady() {
|
||||
/**
|
||||
* Emitted when the Client becomes ready to start working
|
||||
*
|
||||
* @event Client#ready
|
||||
*/
|
||||
this.status = Constants.Status.READY;
|
||||
this.client.emit(Constants.Events.READY);
|
||||
this.packetManager.handleQueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs on new packets before `READY` to see if the Client is ready yet, if it is prepares
|
||||
* the `READY` event.
|
||||
* @returns {null}
|
||||
*/
|
||||
checkIfReady() {
|
||||
if (this.status !== Constants.Status.READY) {
|
||||
if (this.status !== Constants.Status.READY && this.status !== Constants.Status.NEARLY) {
|
||||
let unavailableCount = 0;
|
||||
for (const guildID of this.client.guilds.keys()) {
|
||||
unavailableCount += this.client.guilds.get(guildID).available ? 0 : 1;
|
||||
}
|
||||
|
||||
if (unavailableCount === 0) {
|
||||
this.status = Constants.Status.READY;
|
||||
/**
|
||||
* Emitted when the Client becomes ready to start working
|
||||
*
|
||||
* @event Client#ready
|
||||
*/
|
||||
this.client.emit(Constants.Events.READY);
|
||||
this.packetManager.handleQueue();
|
||||
this.status = Constants.Status.NEARLY;
|
||||
if (this.client.options.fetch_all_members) {
|
||||
const promises = this.client.guilds.array().map(g => g.fetchMembers());
|
||||
return Promise.all(promises).then(() => this._emitReady()).catch(e => {
|
||||
this.client.emit('warn', `error on pre-ready guild member fetching - ${e}`);
|
||||
this._emitReady();
|
||||
});
|
||||
}
|
||||
this._emitReady();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ const BeforeReadyWhitelist = [
|
||||
Constants.WSEvents.READY,
|
||||
Constants.WSEvents.GUILD_CREATE,
|
||||
Constants.WSEvents.GUILD_DELETE,
|
||||
Constants.WSEvents.GUILD_MEMBERS_CHUNK,
|
||||
];
|
||||
|
||||
class WebSocketPacketManager {
|
||||
|
||||
@@ -10,6 +10,7 @@ class GuildMembersChunkHandler extends AbstractHandler {
|
||||
const client = this.packetManager.client;
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
const members = [];
|
||||
|
||||
if (guild) {
|
||||
for (const member of data.members) {
|
||||
members.push(guild._addMember(member, true));
|
||||
|
||||
@@ -582,6 +582,9 @@ class Guild {
|
||||
if (this._fetchWaiter) {
|
||||
throw new Error('already fetching guild members');
|
||||
}
|
||||
if (this.memberCount === this.members.size) {
|
||||
return resolve(this);
|
||||
}
|
||||
this._fetchWaiter = resolve;
|
||||
this.client.ws.send({
|
||||
op: Constants.OPCodes.REQUEST_GUILD_MEMBERS,
|
||||
@@ -592,7 +595,7 @@ class Guild {
|
||||
},
|
||||
});
|
||||
this._checkChunks();
|
||||
setTimeout(() => reject(new Error('members not here in time')), 10000);
|
||||
setTimeout(() => reject(new Error('members not here in time')), 120 * 1000);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
* api_request_method: 'sequential',
|
||||
* shard_id: 0,
|
||||
* shard_count: 0,
|
||||
* fetch_all_members: false,
|
||||
* };
|
||||
* ```
|
||||
* @typedef {Object} ClientOptions
|
||||
@@ -41,6 +42,7 @@ exports.DefaultOptions = {
|
||||
api_request_method: 'sequential',
|
||||
shard_id: 0,
|
||||
shard_count: 0,
|
||||
fetch_all_members: false,
|
||||
};
|
||||
|
||||
exports.Status = {
|
||||
@@ -48,6 +50,7 @@ exports.Status = {
|
||||
CONNECTING: 1,
|
||||
RECONNECTING: 2,
|
||||
IDLE: 3,
|
||||
NEARLY: 4,
|
||||
};
|
||||
|
||||
exports.ChannelTypes = {
|
||||
|
||||
Reference in New Issue
Block a user