mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
refactor: more oop with stores (#2216)
* refactor: more oop with stores * forgot bulk delete * Revert "forgot bulk delete" This reverts commit 1b4fb999ee07b358ee6e1af9efb8981b84f83af1. * appease linter * missed some shh * fail
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const { Events } = require('../util/Constants');
|
||||
const Guild = require('../structures/Guild');
|
||||
|
||||
/**
|
||||
@@ -35,6 +37,44 @@ class GuildStore extends DataStore {
|
||||
* @param {GuildResolvable} guild The guild resolvable to identify
|
||||
* @returns {?Snowflake}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a guild.
|
||||
* <warn>This is only available when using a user account.</warn>
|
||||
* @param {string} name The name of the guild
|
||||
* @param {Object} [options] Options for the creating
|
||||
* @param {string} [options.region] The region for the server, defaults to the closest one available
|
||||
* @param {BufferResolvable|Base64Resolvable} [options.icon=null] The icon for the guild
|
||||
* @returns {Promise<Guild>} The guild that was created
|
||||
*/
|
||||
create(name, { region, icon = null } = {}) {
|
||||
if (!icon || (typeof icon === 'string' && icon.startsWith('data:'))) {
|
||||
return new Promise((resolve, reject) =>
|
||||
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(Events.GUILD_CREATE, handleGuild);
|
||||
this.client.clearTimeout(timeout);
|
||||
resolve(guild);
|
||||
}
|
||||
};
|
||||
this.client.on(Events.GUILD_CREATE, handleGuild);
|
||||
|
||||
const timeout = this.client.setTimeout(() => {
|
||||
this.client.removeListener(Events.GUILD_CREATE, handleGuild);
|
||||
resolve(this.client.guilds.add(data));
|
||||
}, 10000);
|
||||
return undefined;
|
||||
}, reject)
|
||||
);
|
||||
}
|
||||
|
||||
return DataResolver.resolveImage(icon)
|
||||
.then(data => this.create(name, { region, icon: data || null }));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildStore;
|
||||
|
||||
Reference in New Issue
Block a user