-
Notifications
You must be signed in to change notification settings - Fork 9
/
Endpoint.php
449 lines (394 loc) · 14.6 KB
/
Endpoint.php
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
/*
* This file is a part of the DiscordPHP-Http project.
*
* Copyright (c) 2021-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE file.
*/
namespace Discord\Http;
class Endpoint
{
// GET
public const GATEWAY = 'gateway';
// GET
public const GATEWAY_BOT = self::GATEWAY.'/bot';
// GET
public const APPLICATION_SKUS = 'applications/:application_id/skus';
// GET, POST
public const APPLICATION_EMOJIS = 'applications/:application_id/emojis';
// GET, PATCH, DELETE
public const APPLICATION_EMOJI = 'applications/:application_id/emojis/:emoji_id';
// GET, POST
public const APPLICATION_ENTITLEMENTS = 'applications/:application_id/entitlements';
// DELETE
public const APPLICATION_ENTITLEMENT = self::APPLICATION_ENTITLEMENTS.'/:entitlement_id';
// POST
public const APPLICATION_ENTITLEMENT_CONSUME = self::APPLICATION_ENTITLEMENT.'/consume';
// GET, POST, PUT
public const GLOBAL_APPLICATION_COMMANDS = 'applications/:application_id/commands';
// GET, PATCH, DELETE
public const GLOBAL_APPLICATION_COMMAND = self::GLOBAL_APPLICATION_COMMANDS.'/:command_id';
// GET, POST, PUT
public const GUILD_APPLICATION_COMMANDS = 'applications/:application_id/guilds/:guild_id/commands';
// GET, PUT
public const GUILD_APPLICATION_COMMANDS_PERMISSIONS = self::GUILD_APPLICATION_COMMANDS.'/permissions';
// GET, PATCH, DELETE
public const GUILD_APPLICATION_COMMAND = self::GUILD_APPLICATION_COMMANDS.'/:command_id';
// GET, PUT
public const GUILD_APPLICATION_COMMAND_PERMISSIONS = self::GUILD_APPLICATION_COMMANDS.'/:command_id/permissions';
// POST
public const INTERACTION_RESPONSE = 'interactions/:interaction_id/:interaction_token/callback';
// POST
public const CREATE_INTERACTION_FOLLOW_UP = 'webhooks/:application_id/:interaction_token';
// PATCH, DELETE
public const ORIGINAL_INTERACTION_RESPONSE = self::CREATE_INTERACTION_FOLLOW_UP.'/messages/@original';
// PATCH, DELETE
public const INTERACTION_FOLLOW_UP = self::CREATE_INTERACTION_FOLLOW_UP.'/messages/:message_id';
// GET
public const SKU_SUBSCRIPTIONS = '/skus/:sku_id/subscriptions';
// GET
public const SKU_SUBSCRIPTION = self::SKU_SUBSCRIPTIONS.'/:subscription_id';
// GET
public const AUDIT_LOG = 'guilds/:guild_id/audit-logs';
// GET, PATCH, DELETE
public const CHANNEL = 'channels/:channel_id';
// GET, POST
public const CHANNEL_MESSAGES = self::CHANNEL.'/messages';
// GET, PATCH, DELETE
public const CHANNEL_MESSAGE = self::CHANNEL.'/messages/:message_id';
// POST
public const CHANNEL_CROSSPOST_MESSAGE = self::CHANNEL.'/messages/:message_id/crosspost';
// POST
public const CHANNEL_MESSAGES_BULK_DELETE = self::CHANNEL.'/messages/bulk-delete';
// PUT, DELETE
public const CHANNEL_PERMISSIONS = self::CHANNEL.'/permissions/:overwrite_id';
// GET, POST
public const CHANNEL_INVITES = self::CHANNEL.'/invites';
// POST
public const CHANNEL_FOLLOW = self::CHANNEL.'/followers';
// POST
public const CHANNEL_TYPING = self::CHANNEL.'/typing';
// GET
public const CHANNEL_PINS = self::CHANNEL.'/pins';
// PUT, DELETE
public const CHANNEL_PIN = self::CHANNEL.'/pins/:message_id';
// POST
public const CHANNEL_THREADS = self::CHANNEL.'/threads';
// POST
public const CHANNEL_MESSAGE_THREADS = self::CHANNEL_MESSAGE.'/threads';
// GET
public const CHANNEL_THREADS_ARCHIVED_PUBLIC = self::CHANNEL_THREADS.'/archived/public';
// GET
public const CHANNEL_THREADS_ARCHIVED_PRIVATE = self::CHANNEL_THREADS.'/archived/private';
// GET
public const CHANNEL_THREADS_ARCHIVED_PRIVATE_ME = self::CHANNEL.'/users/@me/threads/archived/private';
// POST
public const CHANNEL_SEND_SOUNDBOARD_SOUND = self::CHANNEL . '/send-soundboard-sound';
// GET, PATCH, DELETE
public const THREAD = 'channels/:thread_id';
// GET
public const THREAD_MEMBERS = self::THREAD.'/thread-members';
// GET, PUT, DELETE
public const THREAD_MEMBER = self::THREAD_MEMBERS.'/:user_id';
// PUT, DELETE
public const THREAD_MEMBER_ME = self::THREAD_MEMBERS.'/@me';
// GET, DELETE
public const MESSAGE_REACTION_ALL = self::CHANNEL.'/messages/:message_id/reactions';
// GET, DELETE
public const MESSAGE_REACTION_EMOJI = self::CHANNEL.'/messages/:message_id/reactions/:emoji';
// PUT, DELETE
public const OWN_MESSAGE_REACTION = self::CHANNEL.'/messages/:message_id/reactions/:emoji/@me';
// DELETE
public const USER_MESSAGE_REACTION = self::CHANNEL.'/messages/:message_id/reactions/:emoji/:user_id';
// GET
protected const MESSAGE_POLL = self::CHANNEL.'/polls/:message_id';
// GET
public const MESSAGE_POLL_ANSWER = self::MESSAGE_POLL.'/answers/:answer_id';
// POST
public const MESSAGE_POLL_EXPIRE = self::MESSAGE_POLL.'/expire';
// GET, POST
public const CHANNEL_WEBHOOKS = self::CHANNEL.'/webhooks';
// POST
public const GUILDS = 'guilds';
// GET, PATCH, DELETE
public const GUILD = 'guilds/:guild_id';
// GET, POST, PATCH
public const GUILD_CHANNELS = self::GUILD.'/channels';
// GET
public const GUILD_THREADS_ACTIVE = self::GUILD.'/threads/active';
// GET
public const GUILD_MEMBERS = self::GUILD.'/members';
// GET
public const GUILD_MEMBERS_SEARCH = self::GUILD.'/members/search';
// GET, PATCH, PUT, DELETE
public const GUILD_MEMBER = self::GUILD.'/members/:user_id';
// PATCH
public const GUILD_MEMBER_SELF = self::GUILD.'/members/@me';
/** @deprecated 9.0.9 Use `GUILD_MEMBER_SELF` */
public const GUILD_MEMBER_SELF_NICK = self::GUILD.'/members/@me/nick';
// PUT, DELETE
public const GUILD_MEMBER_ROLE = self::GUILD.'/members/:user_id/roles/:role_id';
// GET
public const GUILD_BANS = self::GUILD.'/bans';
// GET, PUT, DELETE
public const GUILD_BAN = self::GUILD.'/bans/:user_id';
// GET, PATCH
public const GUILD_ROLES = self::GUILD.'/roles';
// GET, POST, PATCH, DELETE
public const GUILD_ROLE = self::GUILD.'/roles/:role_id';
// POST
public const GUILD_MFA = self::GUILD.'/mfa';
// GET, POST
public const GUILD_INVITES = self::GUILD.'/invites';
// GET, POST
public const GUILD_INTEGRATIONS = self::GUILD.'/integrations';
// PATCH, DELETE
public const GUILD_INTEGRATION = self::GUILD.'/integrations/:integration_id';
// POST
public const GUILD_INTEGRATION_SYNC = self::GUILD.'/integrations/:integration_id/sync';
// GET, POST
public const GUILD_EMOJIS = self::GUILD.'/emojis';
// GET, PATCH, DELETE
public const GUILD_EMOJI = self::GUILD.'/emojis/:emoji_id';
// GET
public const GUILD_PREVIEW = self::GUILD.'/preview';
// GET, POST
public const GUILD_PRUNE = self::GUILD.'/prune';
// GET
public const GUILD_REGIONS = self::GUILD.'/regions';
// GET, PATCH
public const GUILD_WIDGET_SETTINGS = self::GUILD.'/widget';
// GET
public const GUILD_WIDGET = self::GUILD.'/widget.json';
// GET
public const GUILD_WIDGET_IMAGE = self::GUILD.'/widget.png';
// GET, PATCH
public const GUILD_WELCOME_SCREEN = self::GUILD.'/welcome-screen';
// GET
public const GUILD_ONBOARDING = self::GUILD.'/onboarding';
// GET, PATCH
public const GUILD_USER_CURRENT_VOICE_STATE = self::GUILD.'/voice-states/@me';
// GET, PATCH
public const GUILD_USER_VOICE_STATE = self::GUILD.'/voice-states/:user_id';
// GET
public const GUILD_VANITY_URL = self::GUILD.'/vanity-url';
// GET, PATCH
public const GUILD_MEMBERSHIP_SCREENING = self::GUILD.'/member-verification';
// GET
public const GUILD_WEBHOOKS = self::GUILD.'/webhooks';
// GET, POST
public const GUILD_STICKERS = self::GUILD.'/stickers';
// GET, PATCH, DELETE
public const GUILD_STICKER = self::GUILD.'/stickers/:sticker_id';
// GET
public const STICKER = 'stickers/:sticker_id';
// GET
public const STICKER_PACKS = 'sticker-packs';
// GET, POST
public const GUILD_SCHEDULED_EVENTS = self::GUILD.'/scheduled-events';
// GET, PATCH, DELETE
public const GUILD_SCHEDULED_EVENT = self::GUILD.'/scheduled-events/:guild_scheduled_event_id';
// GET
public const GUILD_SCHEDULED_EVENT_USERS = self::GUILD.'/scheduled-events/:guild_scheduled_event_id/users';
// GET, POST
public const GUILD_SOUNDBOARD_SOUNDS = self::GUILD.'/soundboard-sounds';
// GET, PATCH, DELETE
public const GUILD_SOUNDBOARD_SOUND = self::GUILD.'/soundboard-sounds/:sound_id';
// GET, DELETE
public const INVITE = 'invites/:code';
// POST
public const STAGE_INSTANCES = 'stage-instances';
// GET, PATCH, DELETE
public const STAGE_INSTANCE = 'stage-instances/:channel_id';
// GET, POST
public const GUILDS_TEMPLATE = self::GUILDS.'/templates/:template_code';
// GET, POST
public const GUILD_TEMPLATES = self::GUILD.'/templates';
// PUT, PATCH, DELETE
public const GUILD_TEMPLATE = self::GUILD.'/templates/:template_code';
// GET, POST
public const GUILD_AUTO_MODERATION_RULES = self::GUILD.'/auto-moderation/rules';
// GET, PATCH, DELETE
public const GUILD_AUTO_MODERATION_RULE = self::GUILD.'/auto-moderation/rules/:auto_moderation_rule_id';
// GET
public const SOUNDBOARD_DEFAULT_SOUNDS = 'soundboard-default-sounds';
// GET, PATCH
public const USER_CURRENT = 'users/@me';
// GET
public const USER = 'users/:user_id';
// GET
public const USER_CURRENT_GUILDS = self::USER_CURRENT.'/guilds';
// DELETE
public const USER_CURRENT_GUILD = self::USER_CURRENT.'/guilds/:guild_id';
// GET
public const USER_CURRENT_MEMBER = self::USER_CURRENT.'/guilds/:guild_id/member';
// GET, POST
public const USER_CURRENT_CHANNELS = self::USER_CURRENT.'/channels';
// GET
public const USER_CURRENT_CONNECTIONS = self::USER_CURRENT.'/connections';
// GET, PUT
public const USER_CURRENT_APPLICATION_ROLE_CONNECTION = self::USER_CURRENT.'/applications/:application_id/role-connection';
// GET
public const APPLICATION_CURRENT = 'applications/@me';
// GET, PATCH, DELETE
public const WEBHOOK = 'webhooks/:webhook_id';
// GET, PATCH, DELETE
public const WEBHOOK_TOKEN = 'webhooks/:webhook_id/:webhook_token';
// POST
public const WEBHOOK_EXECUTE = self::WEBHOOK_TOKEN;
// POST
public const WEBHOOK_EXECUTE_SLACK = self::WEBHOOK_EXECUTE.'/slack';
// POST
public const WEBHOOK_EXECUTE_GITHUB = self::WEBHOOK_EXECUTE.'/github';
// PATCH, DELETE
public const WEBHOOK_MESSAGE = self::WEBHOOK_TOKEN.'/messages/:message_id';
// GET, PUT
public const APPLICATION_ROLE_CONNECTION_METADATA = 'applications/:application_id/role-connections/metadata';
/**
* Regex to identify parameters in endpoints.
*
* @var string
*/
public const REGEX = '/:([^\/]*)/';
/**
* A list of parameters considered 'major' by Discord.
*
* @see https://discord.com/developers/docs/topics/rate-limits
* @var string[]
*/
public const MAJOR_PARAMETERS = ['channel_id', 'guild_id', 'webhook_id', 'thread_id'];
/**
* The string version of the endpoint, including all parameters.
*
* @var string
*/
protected $endpoint;
/**
* Array of placeholders to be replaced in the endpoint.
*
* @var string[]
*/
protected $vars = [];
/**
* Array of arguments to substitute into the endpoint.
*
* @var string[]
*/
protected $args = [];
/**
* Array of query data to be appended
* to the end of the endpoint with `http_build_query`.
*
* @var array
*/
protected $query = [];
/**
* Creates an endpoint class.
*
* @param string $endpoint
*/
public function __construct(string $endpoint)
{
$this->endpoint = $endpoint;
if (preg_match_all(self::REGEX, $endpoint, $vars)) {
$this->vars = $vars[1] ?? [];
}
}
/**
* Binds a list of arguments to the endpoint.
*
* @param string[] ...$args
* @return this
*/
public function bindArgs(...$args): self
{
for ($i = 0; $i < count($this->vars) && $i < count($args); $i++) {
$this->args[$this->vars[$i]] = $args[$i];
}
return $this;
}
/**
* Binds an associative array to the endpoint.
*
* @param string[] $args
* @return this
*/
public function bindAssoc(array $args): self
{
$this->args = array_merge($this->args, $args);
return $this;
}
/**
* Adds a key-value query pair to the endpoint.
*
* @param string $key
* @param string|bool $value
*/
public function addQuery(string $key, $value): void
{
if (! is_bool($value)) {
$value = (string) $value;
}
$this->query[$key] = $value;
}
/**
* Converts the endpoint into the absolute endpoint with
* placeholders replaced.
*
* Passing a true boolean in will only replace the major parameters.
* Used for rate limit buckets.
*
* @param bool $onlyMajorParameters
* @return string
*/
public function toAbsoluteEndpoint(bool $onlyMajorParameters = false): string
{
$endpoint = $this->endpoint;
foreach ($this->vars as $var) {
if (! isset($this->args[$var]) || ($onlyMajorParameters && ! $this->isMajorParameter($var))) {
continue;
}
$endpoint = str_replace(":{$var}", $this->args[$var], $endpoint);
}
if (! $onlyMajorParameters && count($this->query) > 0) {
$endpoint .= '?'.http_build_query($this->query);
}
return $endpoint;
}
/**
* Converts the endpoint to a string.
* Alias of ->toAbsoluteEndpoint();.
*
* @return string
*/
public function __toString(): string
{
return $this->toAbsoluteEndpoint();
}
/**
* Creates an endpoint class and binds arguments to
* the newly created instance.
*
* @param string $endpoint
* @param string[] $args
* @return Endpoint
*/
public static function bind(string $endpoint, ...$args)
{
$endpoint = new Endpoint($endpoint);
$endpoint->bindArgs(...$args);
return $endpoint;
}
/**
* Checks if a parameter is a major parameter.
*
* @param string $param
* @return bool
*/
private static function isMajorParameter(string $param): bool
{
return in_array($param, self::MAJOR_PARAMETERS);
}
}