Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promisified Guild#fetchAllMembers #576

Merged
merged 8 commits into from Dec 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/gateway/Shard.js
Expand Up @@ -144,6 +144,15 @@ class Shard extends EventEmitter {
this.connecting = false;
this.ready = false;
this.preReady = false;
if(this.requestMembersPromise !== undefined) {
for(const guildID in this.requestMembersPromise) {
if(!this.requestMembersPromise.hasOwnProperty(guildID))
LJNeon marked this conversation as resolved.
Show resolved Hide resolved
continue;
clearTimeout(this.requestMembersPromise[guildID].timeout);
this.requestMembersPromise[guildID].res();
LJNeon marked this conversation as resolved.
Show resolved Hide resolved
}
}
this.requestMembersPromise = {};
this.getAllUsersCount = {};
this.getAllUsersQueue = [];
this.getAllUsersLength = 1;
Expand Down Expand Up @@ -1134,6 +1143,11 @@ class Shard extends EventEmitter {
if(this.getAllUsersCount.hasOwnProperty(guild.id)) {
if(this.getAllUsersCount[guild.id] <= 1) {
delete this.getAllUsersCount[guild.id];
if(this.requestMembersPromise.hasOwnProperty(guild.id)) {
clearTimeout(this.requestMembersPromise[guild.id].timeout);
this.requestMembersPromise[guild.id].res();
delete this.requestMembersPromise[guild.id];
}
this.checkReady();
} else {
--this.getAllUsersCount[guild.id];
Expand Down Expand Up @@ -1495,6 +1509,14 @@ class Shard extends EventEmitter {
this.getAllUsersLength = 1 + guildID.length + 3;
} else if(this.ready) {
this.requestGuildMembers([guildID]);

return new Promise(res => this.requestMembersPromise[guildID] = {
res,
timeout: setTimeout(() => {
delete this.requestMembersPromise[guildID];
res();
}, this.client.options.requestTimeout)
});
} else {
this.getAllUsersQueue.push(guildID);
this.getAllUsersLength += guildID.length + 3;
Expand Down
3 changes: 2 additions & 1 deletion lib/structures/Guild.js
Expand Up @@ -149,9 +149,10 @@ class Guild extends Base {

/**
* Request all guild members from Discord
* @returns {Promise} Resolves once all guild members are fetched
*/
fetchAllMembers() {
this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000)); // TODO Promise with chunk timeout
return this.shard.getGuildMembers(this.id, Math.ceil(this.memberCount / 1000));
}

get iconURL() {
Expand Down