-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathuser.h
584 lines (496 loc) · 14.4 KB
/
user.h
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#pragma once
#include <dpp/export.h>
#include <dpp/json_fwd.h>
#include <dpp/snowflake.h>
#include <dpp/managed.h>
#include <dpp/utility.h>
#include <dpp/json_interface.h>
namespace dpp {
constexpr uint32_t MAX_AVATAR_SIZE = 10240 * 1000; // 10240KB.
/**
* @brief Various bitmask flags used to represent information about a dpp::user
*/
enum user_flags : uint32_t {
/**
* @brief User is a bot.
*/
u_bot = 0b00000000000000000000000000000001,
/**
* @brief User is a system user (Clyde!).
*/
u_system = 0b00000000000000000000000000000010,
/**
* @brief User has multi-factor authentication enabled.
*/
u_mfa_enabled = 0b00000000000000000000000000000100,
/**
* @brief User is verified (verified email address).
*/
u_verified = 0b00000000000000000000000000001000,
/**
* @brief User has full nitro.
*/
u_nitro_full = 0b00000000000000000000000000010000,
/**
* @brief User has nitro classic.
*/
u_nitro_classic = 0b00000000000000000000000000100000,
/**
* @brief User is discord staff.
*/
u_discord_employee = 0b00000000000000000000000001000000,
/**
* @brief User owns a partnered server.
*/
u_partnered_owner = 0b00000000000000000000000010000000,
/**
* @brief User is a member of hypesquad events.
*/
u_hypesquad_events = 0b00000000000000000000000100000000,
/**
* @brief User has BugHunter level 1.
*/
u_bughunter_1 = 0b00000000000000000000001000000000,
/**
* @brief User is a member of House Bravery.
*/
u_house_bravery = 0b00000000000000000000010000000000,
/**
* @brief User is a member of House Brilliance.
*/
u_house_brilliance = 0b00000000000000000000100000000000,
/**
* @brief User is a member of House Balance.
*/
u_house_balance = 0b00000000000000000001000000000000,
/**
* @brief User is an early supporter.
*/
u_early_supporter = 0b00000000000000000010000000000000,
/**
* @brief User is a team user.
*/
u_team_user = 0b00000000000000000100000000000000,
/**
* @brief User is has Bug Hunter level 2.
*/
u_bughunter_2 = 0b00000000000000001000000000000000,
/**
* @brief User is a verified bot.
*/
u_verified_bot = 0b00000000000000010000000000000000,
/**
* @brief User has the Early Verified Bot Developer badge.
*/
u_verified_bot_dev = 0b00000000000000100000000000000000,
/**
* @brief User's icon is animated.
*/
u_animated_icon = 0b00000000000001000000000000000000,
/**
* @brief User is a certified moderator.
*/
u_certified_moderator = 0b00000000000010000000000000000000,
/**
* @brief User is a bot using HTTP interactions.
*
* @note shows online even when not connected to a websocket.
*/
u_bot_http_interactions = 0b00000000000100000000000000000000,
/**
* @brief User has nitro basic.
*/
u_nitro_basic = 0b00000000001000000000000000000000,
/**
* @brief User has the active developer badge.
*/
u_active_developer = 0b00000000010000000000000000000000,
/**
* @brief User's banner is animated.
*/
u_animated_banner = 0b00000000100000000000000000000000,
};
/**
* @brief Represents a user on discord. May or may not be a member of a dpp::guild.
*/
class DPP_EXPORT user : public managed, public json_interface<user> {
protected:
friend struct json_interface<user>;
/** Fill this record from json.
* @param j The json to fill this record from
* @return Reference to self
*/
user& fill_from_json_impl(nlohmann::json* j);
/**
* @brief Convert to JSON
*
* @param with_id include ID in output
* @return json JSON output
*/
virtual json to_json_impl(bool with_id = true) const;
public:
/**
* @brief Discord username.
*/
std::string username;
/**
* @brief Global display name.
*/
std::string global_name;
/**
* @brief Avatar hash.
*/
utility::iconhash avatar;
/**
* @brief Avatar decoration hash.
*/
utility::iconhash avatar_decoration;
/**
* @brief Flags built from a bitmask of values in dpp::user_flags.
*/
uint32_t flags;
/**
* @brief Discriminator (aka tag), 4 digits usually displayed with leading zeroes.
*
* @note To print the discriminator with leading zeroes, use format_username().
* 0 for users that have migrated to the new username format.
*/
uint16_t discriminator;
/**
* @brief Reference count of how many guilds this user is in.
*/
uint8_t refcount;
/**
* @brief Construct a new user object
*/
user();
/**
* @brief Destroy the user object
*/
virtual ~user() = default;
/**
* @brief Create a mentionable user.
* @param id The ID of the user.
* @return std::string The formatted mention of the user.
*/
static std::string get_mention(const snowflake& id);
/**
* @brief Get the avatar url of the user
*
* @note If the user doesn't have an avatar, the default user avatar url is returned which is always in `png` format!
*
* @param size The size of the avatar in pixels. It can be any power of two between 16 and 4096,
* otherwise the default sized avatar is returned.
* @param format The format to use for the avatar. It can be one of `i_webp`, `i_jpg`, `i_png` or `i_gif`.
* When passing `i_gif`, it returns an empty string for non-animated images. Consider using the `prefer_animated` parameter instead.
* @param prefer_animated Whether you prefer gif format.
* If true, it'll return gif format whenever the image is available as animated.
* @return std::string avatar url or an empty string, if required attributes are missing or an invalid format was passed
*/
std::string get_avatar_url(uint16_t size = 0, const image_type format = i_png, bool prefer_animated = true) const;
/**
* @brief Get the default avatar url of the user. This is calculated by the discriminator.
*
* @return std::string avatar url or an empty string, if the discriminator is empty
*/
std::string get_default_avatar_url() const;
/**
* @brief Get the avatar decoration url of the user if they have one, otherwise returns an empty string.
*
* @param size The size of the avatar decoration in pixels. It can be any power of two between 16 and 4096,
* otherwise the default sized avatar decoration is returned.
* @return std::string avatar url or an empty string
*/
std::string get_avatar_decoration_url(uint16_t size = 0) const;
/**
* @brief Return a ping/mention for the user
*
* @return std::string mention
*/
std::string get_mention() const;
/**
* @brief Returns URL to user
*
* @return string of URL to user
*/
std::string get_url() const;
/**
* @brief Return true if user has the active Developer badge
*
* @return true if has active developer
*/
bool is_active_developer() const;
/**
* @brief User is a bot
*
* @return True if the user is a bot
*/
bool is_bot() const;
/**
* @brief User is a system user (Clyde)
*
* @return true if user is a system user
*/
bool is_system() const;
/**
* @brief User has multi-factor authentication enabled
*
* @return true if multi-factor is enabled
*/
bool is_mfa_enabled() const;
/**
* @brief Return true if user has verified account
*
* @return true if verified
*/
bool is_verified() const;
/**
* @brief Return true if user has full nitro.
* This is mutually exclusive with full nitro.
*
* @return true if user has full nitro
*/
bool has_nitro_full() const;
/**
* @brief Return true if user has nitro classic.
* This is mutually exclusive with nitro classic.
*
* @return true if user has nitro classic
*/
bool has_nitro_classic() const;
/**
* @brief Return true if user has nitro basic.
* This is mutually exclusive with nitro basic.
*
* @return true if user has nitro basic
*/
bool has_nitro_basic() const;
/**
* @brief Return true if user is a discord employee
*
* @return true if user is discord staff
*/
bool is_discord_employee() const;
/**
* @brief Return true if user owns a partnered server
*
* @return true if user has partnered server
*/
bool is_partnered_owner() const;
/**
* @brief Return true if user has hypesquad events
*
* @return true if has hypesquad events
*/
bool has_hypesquad_events() const;
/**
* @brief Return true if user has the bughunter level 1 badge
*
* @return true if has bughunter level 1
*/
bool is_bughunter_1() const;
/**
* @brief Return true if user is in house bravery
*
* @return true if in house bravery
*/
bool is_house_bravery() const;
/**
* @brief Return true if user is in house brilliance
*
* @return true if in house brilliance
*/
bool is_house_brilliance() const;
/**
* @brief Return true if user is in house balance
*
* @return true if in house brilliance
*/
bool is_house_balance() const;
/**
* @brief Return true if user is an early supporter
*
* @return true if early supporter
*/
bool is_early_supporter() const;
/**
* @brief Return true if user is a team user
*
* @return true if a team user
*/
bool is_team_user() const;
/**
* @brief Return true if user has the bughunter level 2 badge
*
* @return true if has bughunter level 2
*/
bool is_bughunter_2() const;
/**
* @brief Return true if user has the verified bot badge
*
* @return true if verified bot
*/
bool is_verified_bot() const;
/**
* @brief Return true if user is an early verified bot developer
*
* @return true if verified bot developer
*/
bool is_verified_bot_dev() const;
/**
* @brief Return true if user is a certified moderator
*
* @return true if certified moderator
*/
bool is_certified_moderator() const;
/**
* @brief Return true if user is a bot which exclusively uses HTTP interactions.
* Bots using HTTP interactions are always considered online even when not connected to a websocket.
*
* @return true if is a http interactions only bot
*/
bool is_bot_http_interactions() const;
/**
* @brief Return true if user has an animated icon
*
* @return true if icon is animated (gif)
*/
bool has_animated_icon() const;
/**
* @brief Format a username into user\#discriminator
*
* For example Brain#0001
*
* @note This will, most often, return something like Brain#0000 due to discriminators slowly being removed.
* Some accounts, along with most bots, still have discriminators, so they will still show as Bot#1234.
*
* @return Formatted username and discriminator
*/
std::string format_username() const;
};
/**
* @brief A user with additional fields only available via the oauth2 identify scope.
* These are not included in dpp::user as additional scopes are needed to fetch them
* which bots do not normally have.
*/
class DPP_EXPORT user_identified : public user, public json_interface<user_identified> {
protected:
friend struct json_interface<user_identified>;
/** Fill this record from json.
* @param j The json to fill this record from
* @return Reference to self
*/
user_identified& fill_from_json_impl(nlohmann::json* j);
/**
* @brief Convert to JSON
*
* @param with_id include ID in output
* @return json JSON output
*/
virtual json to_json_impl(bool with_id = true) const;
public:
/**
* @brief Optional: The user's chosen language option identify.
*/
std::string locale;
/**
* @brief Optional: The user's email.
*
* @note This may be empty.
*/
std::string email;
/**
* @brief Optional: The user's banner hash identify.
*
* @note This may be empty.
*/
utility::iconhash banner;
/**
* @brief Optional: The user's banner color encoded as an integer representation of hexadecimal color code identify.
*
* @note This may be empty.
*/
uint32_t accent_color;
/**
* @brief Optional: Whether the email on this account has been verified email.
*/
bool verified;
/**
* @brief Construct a new user identified object
*/
user_identified();
/**
* @brief Construct a new user identified object from a user object
*
* @param u user object
*/
user_identified(const user& u);
/**
* @brief Destroy the user identified object
*/
virtual ~user_identified() = default;
using json_interface<user_identified>::fill_from_json;
using json_interface<user_identified>::build_json;
using json_interface<user_identified>::to_json;
/**
* @brief Return true if user has an animated banner
*
* @return true if banner is animated (gif)
*/
bool has_animated_banner() const;
/**
* @brief Get the user identified's banner url if they have one, otherwise returns an empty string
*
* @param size The size of the banner in pixels. It can be any power of two between 16 and 4096,
* otherwise the default sized banner is returned.
* @param format The format to use for the avatar. It can be one of `i_webp`, `i_jpg`, `i_png` or `i_gif`.
* When passing `i_gif`, it returns an empty string for non-animated images. Consider using the `prefer_animated` parameter instead.
* @param prefer_animated Whether you prefer gif format.
* If true, it'll return gif format whenever the image is available as animated.
* @return std::string banner url or an empty string, if required attributes are missing or an invalid format was passed
*/
std::string get_banner_url(uint16_t size = 0, const image_type format = i_png, bool prefer_animated = true) const;
};
/**
* @brief helper function to deserialize a user from json
*
* @see https://github.com/nlohmann/json#arbitrary-types-conversions
*
* @param j output json object
* @param u user to be deserialized
*/
void from_json(const nlohmann::json& j, user& u);
/**
* @brief helper function to deserialize a user_identified from json
*
* @see https://github.com/nlohmann/json#arbitrary-types-conversions
*
* @param j output json object
* @param u user to be deserialized
*/
void from_json(const nlohmann::json& j, user_identified& u);
/**
* @brief A group of users.
*/
typedef std::unordered_map<snowflake, user> user_map;
}