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