-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
UserManager.ts
53 lines (47 loc) · 1.74 KB
/
UserManager.ts
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
import { Collection } from "@discordjs/collection";
import { Server } from "../../structures/Server";
import { User } from "../../structures/User";
import { CacheableStructManager } from "./CacheableStructManager";
/**
* A manager for interacting with users. You can retrieve users from the .cache property.
* At this point in time, Users cache population is heavily reliant on the Member cache.
*
* @extends CacheableStructManager
*/
export class GlobalUserManager extends CacheableStructManager<string, User> {
/**
* Fetches client user.
*
* @param force Whether to force a fetch from the API.
* @returns A Promise that resolves with the fetched user.
*/
async fetchClient(force?: boolean): Promise<User> {
if (!force) {
const existingUser = this.client.users.cache.get(this.client.user?.id!);
if (existingUser) return existingUser;
}
const data = await this.client.rest.router.users.userRead({
userId: "@me",
});
return new User(this.client, data.user);
}
/**
* Fetches a user's servers (atm, only the logged in client is supported)
*
* @param userId The ID of the user to fetch servers for.
* @returns A promise that resolves to a Collection with the returned servers.
*/
async fetchServers(userId: string): Promise<Collection<string, Server>> {
if (userId !== this.client.user?.id) throw new Error("Only the logged in client is supported at this time.");
const data = await this.client.rest.router.users.userServerReadMany({
userId,
});
const servers = new Collection<string, Server>();
for (const server of data.servers) {
const createdServer = new Server(this.client, server);
servers.set(server.id, createdServer);
this.client.servers.cache.set(server.id, createdServer);
}
return servers;
}
}