forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicationCommands.ts
240 lines (224 loc) · 8.2 KB
/
applicationCommands.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { makeURLSearchParams, type REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPIApplicationCommandPermissionsResult,
type RESTGetAPIApplicationCommandResult,
type RESTGetAPIApplicationCommandsResult,
type RESTGetAPIGuildApplicationCommandsPermissionsResult,
type RESTPatchAPIApplicationCommandJSONBody,
type RESTPatchAPIApplicationCommandResult,
type RESTPostAPIApplicationCommandsJSONBody,
type RESTPostAPIApplicationCommandsResult,
type RESTPutAPIApplicationCommandPermissionsJSONBody,
type RESTPutAPIApplicationCommandPermissionsResult,
type RESTPutAPIApplicationCommandsJSONBody,
type RESTGetAPIApplicationCommandsQuery,
type RESTPutAPIApplicationCommandsResult,
type RESTGetAPIApplicationGuildCommandsQuery,
type Snowflake,
} from 'discord-api-types/v10';
export class ApplicationCommandsAPI {
public constructor(private readonly rest: REST) {}
/**
* Fetches all global commands for a application
*
* @param applicationId - The application id to fetch commands for
* @param options - The options to use when fetching commands
*/
public async getGlobalCommands(applicationId: Snowflake, options: RESTGetAPIApplicationCommandsQuery = {}) {
return this.rest.get(Routes.applicationCommands(applicationId), {
query: makeURLSearchParams(options),
}) as Promise<RESTGetAPIApplicationCommandsResult>;
}
/**
* Creates a new global command
*
* @param applicationId - The application id to create the command for
* @param data - The data to use when creating the command
*/
public async createGlobalCommand(applicationId: Snowflake, data: RESTPostAPIApplicationCommandsJSONBody) {
return this.rest.post(Routes.applicationCommands(applicationId), {
body: data,
}) as Promise<RESTPostAPIApplicationCommandsResult>;
}
/**
* Fetches a global command
*
* @param applicationId - The application id to fetch the command from
* @param commandId - The command id to fetch
*/
public async getGlobalCommand(applicationId: Snowflake, commandId: Snowflake) {
return this.rest.get(
Routes.applicationCommand(applicationId, commandId),
) as Promise<RESTGetAPIApplicationCommandResult>;
}
/**
* Edits a global command
*
* @param applicationId - The application id of the command
* @param commandId - The id of the command to edit
* @param data - The data to use when editing the command
*/
public async editGlobalCommand(
applicationId: Snowflake,
commandId: Snowflake,
data: RESTPatchAPIApplicationCommandJSONBody,
) {
return this.rest.patch(Routes.applicationCommand(applicationId, commandId), {
body: data,
}) as Promise<RESTPatchAPIApplicationCommandResult>;
}
/**
* Deletes a global command
*
* @param applicationId - The application id of the command
* @param commandId - The id of the command to delete
*/
public async deleteGlobalCommand(applicationId: Snowflake, commandId: Snowflake) {
await this.rest.delete(Routes.applicationCommand(applicationId, commandId));
}
/**
* Overwrites global commands
*
* @param applicationId - The application id to overwrite commands for
* @param data - The data to use when overwriting commands
*/
public async bulkOverwriteGlobalCommands(applicationId: Snowflake, data: RESTPutAPIApplicationCommandsJSONBody) {
return this.rest.put(Routes.applicationCommands(applicationId), {
body: data,
}) as Promise<RESTPutAPIApplicationCommandsResult>;
}
/**
* Fetches all commands for a guild
*
* @param applicationId - The application id to fetch commands for
* @param guildId - The guild id to fetch commands for
* @param data - The data to use when fetching commands
*/
public async getGuildCommands(
applicationId: Snowflake,
guildId: Snowflake,
data: RESTGetAPIApplicationGuildCommandsQuery = {},
) {
return this.rest.get(Routes.applicationGuildCommands(applicationId, guildId), {
query: makeURLSearchParams(data),
}) as Promise<RESTGetAPIApplicationCommandsResult>;
}
/**
* Creates a new command for a guild
*
* @param applicationId - The application id to create the command for
* @param guildId - The guild id to create the command for
* @param data - The data to use when creating the command
*/
public async createGuildCommand(
applicationId: Snowflake,
guildId: Snowflake,
data: RESTPostAPIApplicationCommandsJSONBody,
) {
return this.rest.post(Routes.applicationGuildCommands(applicationId, guildId), {
body: data,
}) as Promise<RESTPostAPIApplicationCommandsResult>;
}
/**
* Fetches a guild command
*
* @param applicationId - The application id to fetch the command from
* @param guildId - The guild id to fetch the command from
* @param commandId - The command id to fetch
*/
public async getGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) {
return this.rest.get(
Routes.applicationGuildCommand(applicationId, guildId, commandId),
) as Promise<RESTGetAPIApplicationCommandResult>;
}
/**
* Edits a guild command
*
* @param applicationId - The application id of the command
* @param guildId - The guild id of the command
* @param commandId - The command id to edit
* @param data - The data to use when editing the command
*/
public async editGuildCommand(
applicationId: Snowflake,
guildId: Snowflake,
commandId: Snowflake,
data: RESTPatchAPIApplicationCommandJSONBody,
) {
return this.rest.patch(Routes.applicationGuildCommand(applicationId, guildId, commandId), {
body: data,
}) as Promise<RESTPatchAPIApplicationCommandResult>;
}
/**
* Deletes a guild command
*
* @param applicationId - The application id of the command
* @param guildId - The guild id of the command
* @param commandId - The id of the command to delete
*/
public async deleteGuildCommand(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) {
await this.rest.delete(Routes.applicationGuildCommand(applicationId, guildId, commandId));
}
/**
* Bulk overwrites guild commands
*
* @param applicationId - The application id to overwrite commands for
* @param guildId - The guild id to overwrite commands for
* @param data - The data to use when overwriting commands
*/
public async bulkOverwriteGuildCommands(
applicationId: Snowflake,
guildId: Snowflake,
data: RESTPutAPIApplicationCommandsJSONBody,
) {
return this.rest.put(Routes.applicationGuildCommands(applicationId, guildId), {
body: data,
}) as Promise<RESTPutAPIApplicationCommandsResult>;
}
/**
* Fetches the permissions for a guild command
*
* @param applicationId - The application id to get the permissions for
* @param guildId - The guild id of the command
* @param commandId - The command id to get the permissions for
*/
public async getGuildCommandPermissions(applicationId: Snowflake, guildId: Snowflake, commandId: Snowflake) {
return this.rest.get(
Routes.applicationCommandPermissions(applicationId, guildId, commandId),
) as Promise<RESTGetAPIApplicationCommandPermissionsResult>;
}
/**
* Fetches all permissions for all commands in a guild
*
* @param applicationId - The application id to get the permissions for
* @param guildId - The guild id to get the permissions for
*/
public async getGuildCommandsPermissions(applicationId: Snowflake, guildId: Snowflake) {
return this.rest.get(
Routes.guildApplicationCommandsPermissions(applicationId, guildId),
) as Promise<RESTGetAPIGuildApplicationCommandsPermissionsResult>;
}
/**
* Edits the permissions for a guild command
*
* @param userToken - The token of the user to edit permissions on behalf of
* @param applicationId - The application id to edit the permissions for
* @param guildId - The guild id to edit the permissions for
* @param commandId - The id of the command to edit the permissions for
* @param data - The data to use when editing the permissions
*/
public async editGuildCommandPermissions(
userToken: string,
applicationId: Snowflake,
guildId: Snowflake,
commandId: Snowflake,
data: RESTPutAPIApplicationCommandPermissionsJSONBody,
) {
return this.rest.put(Routes.applicationCommandPermissions(applicationId, guildId, commandId), {
headers: { Authorization: `Bearer ${userToken.replace('Bearer ', '')}` },
auth: false,
body: data,
}) as Promise<RESTPutAPIApplicationCommandPermissionsResult>;
}
}