PresenceManager.js (1743B)
1 'use strict'; 2 3 const BaseManager = require('./BaseManager'); 4 const { Presence } = require('../structures/Presence'); 5 6 /** 7 * Manages API methods for Presences and holds their cache. 8 * @extends {BaseManager} 9 */ 10 class PresenceManager extends BaseManager { 11 constructor(client, iterable) { 12 super(client, iterable, Presence); 13 } 14 15 /** 16 * The cache of Presences 17 * @type {Collection<Snowflake, Presence>} 18 * @name PresenceManager#cache 19 */ 20 21 add(data, cache) { 22 const existing = this.cache.get(data.user.id); 23 return existing ? existing.patch(data) : super.add(data, cache, { id: data.user.id }); 24 } 25 26 /** 27 * Data that can be resolved to a Presence object. This can be: 28 * * A Presence 29 * * A UserResolvable 30 * * A Snowflake 31 * @typedef {Presence|UserResolvable|Snowflake} PresenceResolvable 32 */ 33 34 /** 35 * Resolves a PresenceResolvable to a Presence object. 36 * @param {PresenceResolvable} presence The presence resolvable to resolve 37 * @returns {?Presence} 38 */ 39 resolve(presence) { 40 const presenceResolvable = super.resolve(presence); 41 if (presenceResolvable) return presenceResolvable; 42 const UserResolvable = this.client.users.resolveID(presence); 43 return super.resolve(UserResolvable) || null; 44 } 45 46 /** 47 * Resolves a PresenceResolvable to a Presence ID string. 48 * @param {PresenceResolvable} presence The presence resolvable to resolve 49 * @returns {?Snowflake} 50 */ 51 resolveID(presence) { 52 const presenceResolvable = super.resolveID(presence); 53 if (presenceResolvable) return presenceResolvable; 54 const userResolvable = this.client.users.resolveID(presence); 55 return this.cache.has(userResolvable) ? userResolvable : null; 56 } 57 } 58 59 module.exports = PresenceManager;