forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.ts
112 lines (102 loc) · 3.36 KB
/
user.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { makeURLSearchParams, type REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPICurrentUserGuildsQuery,
type RESTGetAPICurrentUserGuildsResult,
type RESTGetAPICurrentUserResult,
type RESTGetAPIUserResult,
type RESTGetCurrentUserGuildMemberResult,
type RESTPatchAPICurrentUserJSONBody,
type RESTPatchAPICurrentUserResult,
type RESTPatchAPIGuildMemberJSONBody,
type RESTPatchAPIGuildMemberResult,
type RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody,
type RESTPatchAPIGuildVoiceStateCurrentMemberResult,
type RESTPostAPICurrentUserCreateDMChannelResult,
type Snowflake,
} from 'discord-api-types/v10';
export class UsersAPI {
public constructor(private readonly rest: REST) {}
/**
* Fetches a user by their id
*
* @param userId - The id of the user to fetch
*/
public async get(userId: Snowflake) {
return this.rest.get(Routes.user(userId)) as Promise<RESTGetAPIUserResult>;
}
/**
* Returns the user object of the requester's account
*/
public async getCurrent() {
return this.rest.get(Routes.user('@me')) as Promise<RESTGetAPICurrentUserResult>;
}
/**
* Returns a list of partial guild objects the current user is a member of
*
* @param options - The options to use when fetching the current user's guilds
*/
public async getGuilds(options: RESTGetAPICurrentUserGuildsQuery = {}) {
return this.rest.get(Routes.userGuilds(), {
query: makeURLSearchParams(options),
}) as Promise<RESTGetAPICurrentUserGuildsResult>;
}
/**
* Leaves the guild with the given id
*
* @param guildId - The id of the guild
*/
public async leaveGuild(guildId: Snowflake) {
await this.rest.delete(Routes.userGuild(guildId));
}
/**
* Edits the current user
*
* @param user - The new data for the current user
*/
public async edit(user: RESTPatchAPICurrentUserJSONBody) {
return this.rest.patch(Routes.user('@me'), { body: user }) as Promise<RESTPatchAPICurrentUserResult>;
}
/**
* Fetches the guild member for the current user
*
* @param guildId - The id of the guild
*/
public async getGuildMember(guildId: Snowflake) {
return this.rest.get(Routes.userGuildMember(guildId)) as Promise<RESTGetCurrentUserGuildMemberResult>;
}
/**
* Edits the guild member for the current user
*
* @param guildId - The id of the guild
* @param member - The new data for the guild member
* @param reason - The reason for editing this guild member
*/
public async editGuildMember(guildId: Snowflake, member: RESTPatchAPIGuildMemberJSONBody = {}, reason?: string) {
return this.rest.patch(Routes.guildMember(guildId, '@me'), {
reason,
body: member,
}) as Promise<RESTPatchAPIGuildMemberResult>;
}
/**
* Sets the voice state for the current user
*
* @param guildId - The id of the guild
* @param options - The options to use when setting the voice state
*/
public async setVoiceState(guildId: Snowflake, options: RESTPatchAPIGuildVoiceStateCurrentMemberJSONBody = {}) {
return this.rest.patch(Routes.guildVoiceState(guildId, '@me'), {
body: options,
}) as Promise<RESTPatchAPIGuildVoiceStateCurrentMemberResult>;
}
/**
* Opens a new DM channel with a user
*
* @param userId - The id of the user to open a DM channel with
*/
public async createDM(userId: Snowflake) {
return this.rest.post(Routes.userChannels(), {
body: { recipient_id: userId },
}) as Promise<RESTPostAPICurrentUserCreateDMChannelResult>;
}
}