mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
feat: remove datastores and implement Managers (#3696)
* Initial commit: add 5 initial managers - Base manager - GuildChannelManager - MessageManager - PresenceManager - Reaction Manager - Added LimitedCollection * Add GuildEmojiManager, various fixes * Modify some managers and add guildmembermanager * Initial integration * Delete old stores * Integration part two, removed LRUCollection - Most of the integration has been finished - TODO typings - Removed LRUCollection, needless sweeping * Typings + stuff i somehow missed in ChannelManager * LimitedCollection typings/ final changes * Various jsdoc and syntactical fixes, Removed Util.mixin() * tslint fix * Grammatical and logical changes * Delete temporary file placed by mistake * Grammatical changes * Add missing type * Update jsdoc examples * fix: ChannelManager#remove should call cache#delete not cache#remove * fix recursive require * Fix missed cache in util * fix: more missed cache * Remove accidental _fetchMany change from #3645 * fix: use .cache.delete() over .remove() * fix: missing cache in ReactionCollector * fix: missed cache in client * fix: members is a collection not a manager Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * fix: various docs and cache fixes * fix: missed cache * fix: missing _roles * Final testing and debugging * LimitedCollection: return the Collection instead of undefined on .set * Add cache to BaseManager in typings * Commit fixes i forgot to stage yesterday * Update invite events * Account for new commit * fix: MessageReactionRemoveAll should call .cache.clear() * fix: add .cache at various places, correct return type * docs: remove mentions of 'store' * Add extra documented properties to typings Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
105
src/managers/GuildManager.js
Normal file
105
src/managers/GuildManager.js
Normal file
@@ -0,0 +1,105 @@
|
||||
'use strict';
|
||||
|
||||
const BaseManager = require('./BaseManager');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const { Events } = require('../util/Constants');
|
||||
const Guild = require('../structures/Guild');
|
||||
const GuildChannel = require('../structures/GuildChannel');
|
||||
const GuildMember = require('../structures/GuildMember');
|
||||
const Role = require('../structures/Role');
|
||||
|
||||
/**
|
||||
* Manages API methods for Guilds and stores their cache.
|
||||
* @extends {BaseManager}
|
||||
*/
|
||||
class GuildManager extends BaseManager {
|
||||
constructor(client, iterable) {
|
||||
super(client, iterable, Guild);
|
||||
}
|
||||
|
||||
/**
|
||||
* The cache of this Manager
|
||||
* @property {Collection<Snowflake, Guild>} cache
|
||||
* @memberof GuildManager
|
||||
* @instance
|
||||
*/
|
||||
|
||||
/**
|
||||
* Data that resolves to give a Guild object. This can be:
|
||||
* * A Guild object
|
||||
* * A GuildChannel object
|
||||
* * A Role object
|
||||
* * A Snowflake
|
||||
* @typedef {Guild|GuildChannel|GuildMember|Role|Snowflake} GuildResolvable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resolves a GuildResolvable to a Guild object.
|
||||
* @method resolve
|
||||
* @memberof GuildManager
|
||||
* @instance
|
||||
* @param {GuildResolvable} guild The guild resolvable to identify
|
||||
* @returns {?Guild}
|
||||
*/
|
||||
resolve(guild) {
|
||||
if (guild instanceof GuildChannel ||
|
||||
guild instanceof GuildMember ||
|
||||
guild instanceof Role) return super.resolve(guild.guild);
|
||||
return super.resolve(guild);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a GuildResolvable to a Guild ID string.
|
||||
* @method resolveID
|
||||
* @memberof GuildManager
|
||||
* @instance
|
||||
* @param {GuildResolvable} guild The guild resolvable to identify
|
||||
* @returns {?Snowflake}
|
||||
*/
|
||||
resolveID(guild) {
|
||||
if (guild instanceof GuildChannel ||
|
||||
guild instanceof GuildMember ||
|
||||
guild instanceof Role) return super.resolveID(guild.guild.id);
|
||||
return super.resolveID(guild);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.cache.has(data.id)) return resolve(this.client.guilds.cache.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 = GuildManager;
|
||||
Reference in New Issue
Block a user