mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
Wait for the websocket event when creating a guild (#1524)
* Wait for the websocket event when creating a guild * using TOOK_TOO_LONG error * resolve after timeout with an unavailable guild object
This commit is contained in:
@@ -298,11 +298,30 @@ class ClientUser extends User {
|
|||||||
*/
|
*/
|
||||||
createGuild(name, { region, icon = null } = {}) {
|
createGuild(name, { region, icon = null } = {}) {
|
||||||
if (!icon || (typeof icon === 'string' && icon.startsWith('data:'))) {
|
if (!icon || (typeof icon === 'string' && icon.startsWith('data:'))) {
|
||||||
return this.client.api.guilds.post({ data: { name, region, icon } })
|
return new Promise((resolve, reject) =>
|
||||||
.then(data => this.client.dataManager.newGuild(data));
|
this.client.api.guilds.post({ data: { name, region, icon } })
|
||||||
|
.then(data => {
|
||||||
|
if (this.client.guilds.has(data.id)) return resolve(this.client.guilds.get(data.id));
|
||||||
|
|
||||||
|
const handleGuild = guild => {
|
||||||
|
if (guild.id === data.id) {
|
||||||
|
this.client.removeListener(Constants.Events.GUILD_CREATE, handleGuild);
|
||||||
|
this.client.clearTimeout(timeout);
|
||||||
|
resolve(guild);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.client.on(Constants.Events.GUILD_CREATE, handleGuild);
|
||||||
|
|
||||||
|
const timeout = this.client.setTimeout(() => {
|
||||||
|
this.client.removeListener(Constants.Events.GUILD_CREATE, handleGuild);
|
||||||
|
resolve(this.client.dataManager.newGuild(data));
|
||||||
|
}, 10000);
|
||||||
|
return undefined;
|
||||||
|
}, reject)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return this.client.resolver.resolveBuffer(icon)
|
return this.client.resolver.resolveBuffer(icon)
|
||||||
.then(data => this.createGuild(name, region, this.client.resolver.resolveBase64(data) || null));
|
.then(data => this.createGuild(name, { region, icon: this.client.resolver.resolveBase64(data) || null }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ class Guild {
|
|||||||
*/
|
*/
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
} else {
|
} else {
|
||||||
this.available = true;
|
|
||||||
this.setup(data);
|
this.setup(data);
|
||||||
|
if (!data.channels) this.available = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user