Files
discord.js/src/stores/GuildStore.js
bdistin 5afd77ab73 refactor: remove user bot methods (#2559)
* [WIP] Remove user bots

* more backend userbot removal

* Add mfaEnabled back

* revert client presences store removal

* partially revert getAuth changes

* remove more no longer used children of ClientUserGuildSettings

* fix a bug with this pr and TextBasedChannel.applyToClass

* remove a syncGuilds reference

* more user bot data handling

* various guildSync cleanup

* bots can't call logout

Had the user/bot portions of the code mixed up. Though, does this need to be a promise anymore?

* make ClientManager#destroy() sync

It nolonger needs to be a promise, and nothing depended on it being a promise that I can tell.

* requested change

* Fix massive error

* no longer used as it's userbot only
2018-07-25 22:14:23 -04:00

80 lines
2.5 KiB
JavaScript

const DataStore = require('./DataStore');
const DataResolver = require('../util/DataResolver');
const { Events } = require('../util/Constants');
const Guild = require('../structures/Guild');
/**
* Stores guilds.
* @extends {DataStore}
*/
class GuildStore extends DataStore {
constructor(client, iterable) {
super(client, iterable, Guild);
}
/**
* Data that resolves to give a Guild object. This can be:
* * A Guild object
* * A Snowflake
* @typedef {Guild|Snowflake} GuildResolvable
*/
/**
* Resolves a GuildResolvable to a Guild object.
* @method resolve
* @memberof GuildStore
* @instance
* @param {GuildResolvable} guild The guild resolvable to identify
* @returns {?Guild}
*/
/**
* Resolves a GuildResolvable to a Guild ID string.
* @method resolveID
* @memberof GuildStore
* @instance
* @param {GuildResolvable} guild The guild resolvable to identify
* @returns {?Snowflake}
*/
/**
* Creates a guild.
* <warn>This is only available to bots in fewer than 10 guilds.</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;