-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerManager.ts
67 lines (57 loc) · 2.12 KB
/
PlayerManager.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { BaseManager } from './BaseManager';
import { Client } from '../Client';
import { Player, Util } from '../classes/index';
import { HypixelTSError } from '../errors/index';
import type { APIPlayer, GetRecentlyPlayedGamesResponse, GetStatusResponse } from '../typings';
/**
* Player related endpoints.
* @category Managers
* @group Managers
*/
export class PlayerManager extends BaseManager {
public constructor(client: Client) {
super(client, true);
}
/**
* Fetch a player using their username or uuid
* @param identifier The username/uuid of the player
*/
public async fetch(identifier: string) {
if (!identifier) throw new HypixelTSError('METHOD_MISSING_OPTION', 'PlayerManager', 'fetch', 'identifier');
const uuid = await this.getUUID(identifier);
const { player } = await this.makeGetRequest<{ player: APIPlayer }>(`/player?uuid=${uuid}`);
return new Player(this.client, player);
}
/**
* Fetch the recently played games of a player
* @param identifier The username/uuid of the player
*/
public async getRecentlyPlayedGames(identifier: string) {
if (!identifier) throw new HypixelTSError('METHOD_MISSING_OPTION', 'PlayerManager', 'getRecentlyPlayedGames', 'identifier');
const uuid = await this.getUUID(identifier);
const { games } = await this.makeGetRequest<{ games: GetRecentlyPlayedGamesResponse[] }>(`/recentgames?uuid=${uuid}`);
return games;
}
/**
* Fetch the status of a player
* @param identifier The username/uuid of the player
*/
public async getStatus(identifier: string) {
if (!identifier) throw new HypixelTSError('METHOD_MISSING_OPTION', 'PlayerManager', 'getStatus', 'identifier');
const uuid = await this.getUUID(identifier);
const { session } = await this.makeGetRequest<{ session: GetStatusResponse }>(`/status?uuid=${uuid}`);
return session;
}
/**
* Resolves the username to a uuid for use in requests
* @param identifier The username (or uuid) of the player
*/
private async getUUID(identifier: string) {
const isUUID = Util.isUUID(identifier);
if (!isUUID) {
const uuid = await Util.getUUID(identifier);
return uuid;
}
return identifier;
}
}