Debian Codegraph @

include/dpp/presence.h

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22 #pragma once
23 #include <dpp/export.h>
24 #include <dpp/snowflake.h>
25 #include <dpp/emoji.h>
26 #include <dpp/json_fwd.h>
27 #include <unordered_map>
28 #include <dpp/json_interface.h>
29
30 namespace dpp {
31
32 /**
33 * @brief Presence flags bitmask
34 */
35 enum presence_flags {
36 /**
37 * @brief Desktop: Online.
38 */
39 p_desktop_online = 0b00000001,
40
41 /**
42 * @brief Desktop: DND.
43 */
44 p_desktop_dnd = 0b00000010,
45
46 /**
47 * @brief Desktop: Idle.
48 */
49 p_desktop_idle = 0b00000011,
50
51 /**
52 * @brief Web: Online.
53 */
54 p_web_online = 0b00000100,
55
56 /**
57 * @brief Web: DND.
58 */
59 p_web_dnd = 0b00001000,
60
61 /**
62 * @brief Web: Idle.
63 */
64 p_web_idle = 0b00001100,
65
66 /**
67 * @brief Mobile: Online.
68 */
69 p_mobile_online = 0b00010000,
70
71 /**
72 * @brief Mobile: DND.
73 */
74 p_mobile_dnd = 0b00100000,
75
76 /**
77 * @brief Mobile: Idle.
78 */
79 p_mobile_idle = 0b00110000,
80
81 /**
82 * @brief General: Online.
83 */
84 p_status_online = 0b01000000,
85
86 /**
87 * @brief General: DND.
88 */
89 p_status_dnd = 0b10000000,
90
91 /**
92 * @brief General: Idle.
93 */
94 p_status_idle = 0b11000000
95 };
96
97 /**
98 * @brief Online presence status values
99 */
100 enum presence_status : uint8_t {
101 /**
102 * @brief Offline.
103 */
104 ps_offline = 0,
105
106 /**
107 * @brief Online.
108 */
109 ps_online = 1,
110
111 /**
112 * @brief DND.
113 */
114 ps_dnd = 2,
115
116 /**
117 * @brief Idle.
118 */
119 ps_idle = 3,
120
121 /**
122 * @brief Invisible (show as offline).
123 */
124 ps_invisible = 4,
125 };
126
127 /**
128 * @brief Bit shift for desktop status.
129 */
130 #define PF_SHIFT_DESKTOP 0
131
132 /**
133 * @brief Bit shift for web status.
134 */
135 #define PF_SHIFT_WEB 2
136
137 /**
138 * @brief Bit shift for mobile status.
139 */
140 #define PF_SHIFT_MOBILE 4
141
142 /**
143 * @brief Bit shift for main status.
144 */
145 #define PF_SHIFT_MAIN 6
146
147 /**
148 * @brief Bit mask for status.
149 */
150 #define PF_STATUS_MASK 0b00000011
151
152 /**
153 * @brief Bit mask for clearing desktop status.
154 */
155 #define PF_CLEAR_DESKTOP 0b11111100
156
157 /**
158 * @brief Bit mask for clearing web status.
159 */
160 #define PF_CLEAR_WEB 0b11110011
161
162 /**
163 * @brief Bit mask for clearing mobile status.
164 */
165 #define PF_CLEAR_MOBILE 0b11001111
166
167 /**
168 * @brief Bit mask for clearing main status.
169 */
170 #define PF_CLEAR_STATUS 0b00111111
171
172 /**
173 * @brief Game types
174 */
175 enum activity_type : uint8_t {
176 /**
177 * @brief "Playing ..."
178 */
179 at_game = 0,
180
181 /**
182 * @brief "Streaming ..."
183 */
184 at_streaming = 1,
185
186 /**
187 * @brief "Listening to..."
188 */
189 at_listening = 2,
190
191 /**
192 * @brief "Watching..."
193 */
194 at_watching = 3,
195
196 /**
197 * @brief "Emoji..."
198 */
199 at_custom = 4,
200
201 /**
202 * @brief "Competing in..."
203 */
204 at_competing = 5
205 };
206
207 /**
208 * @brief Activity types for rich presence
209 */
210 enum activity_flags {
211 /**
212 * @brief In an instance.
213 */
214 af_instance = 0b000000001,
215
216 /**
217 * @brief Joining.
218 */
219 af_join = 0b000000010,
220
221 /**
222 * @brief Spectating.
223 */
224 af_spectate = 0b000000100,
225
226 /**
227 * @brief Sending join request.
228 */
229 af_join_request = 0b000001000,
230
231 /**
232 * @brief Synchronising.
233 */
234 af_sync = 0b000010000,
235
236 /**
237 * @brief Playing.
238 */
239 af_play = 0b000100000,
240
241 /**
242 * @brief Party privacy friends.
243 */
244 af_party_privacy_friends = 0b001000000,
245
246 /**
247 * @brief Party privacy voice channel.
248 */
249 af_party_privacy_voice_channel = 0b010000000,
250
251 /**
252 * @brief Embedded.
253 */
254 af_embedded = 0b100000000
255 };
256
257 /**
258 * @brief An activity button is a custom button shown in the rich presence. Can be to join a game or whatever
259 */
260 struct DPP_EXPORT activity_button {
261 public:
262 /**
263 * @brief The text shown on the button (1-32 characters).
264 */
265 std::string label;
266
267 /**
268 * @brief The url opened when clicking the button (1-512 characters, can be empty).
269 *
270 * @note Bots cannot access the activity button URLs.
271 */
272 std::string url;
273
274 /** Constructor */
275 activity_button() = default;
276 };
277
278 /**
279 * @brief An activity asset are the images and the hover text displayed in the rich presence
280 */
281 struct DPP_EXPORT activity_assets {
282 public:
283 /**
284 * @brief The large asset image which usually contain snowflake ID or prefixed image ID.
285 */
286 std::string large_image;
287
288 /**
289 * @brief Text displayed when hovering over the large image of the activity.
290 */
291 std::string large_text;
292
293 /**
294 * @brief The small asset image which usually contain snowflake ID or prefixed image ID.
295 */
296 std::string small_image;
297
298 /**
299 * @brief Text displayed when hovering over the small image of the activity.
300 */
301 std::string small_text;
302
303 /** Constructor */
304 activity_assets() = default;
305 };
306
307 /**
308 * @brief Secrets for Rich Presence joining and spectating.
309 */
310 struct DPP_EXPORT activity_secrets {
311 public:
312 /**
313 * @brief The secret for joining a party.
314 */
315 std::string join;
316
317 /**
318 * @brief The secret for spectating a game.
319 */
320 std::string spectate;
321
322 /**
323 * @brief The secret for a specific instanced match.
324 */
325 std::string match;
326
327 /** Constructor */
328 activity_secrets() = default;
329 };
330
331 /**
332 * @brief Information for the current party of the player
333 */
334 struct DPP_EXPORT activity_party {
335 public:
336 /**
337 * @brief The ID of the party.
338 */
339 snowflake id;
340
341 /**
342 * @brief The party's current size.
343 * Used to show the party's current size.
344 */
345 int32_t current_size;
346
347 /**
348 * @brief The party's maximum size.
349 * Used to show the party's maximum size.
350 */
351 int32_t maximum_size;
352
353 /** Constructor */
354 activity_party();
355 };
356
357 /**
358 * @brief An activity is a representation of what a user is doing. It might be a game, or a website, or a movie. Whatever.
359 */
360 class DPP_EXPORT activity {
361 public:
362 /**
363 * @brief Name of activity.
364 * e.g. "Fortnite", "Mr Boom's Firework Factory", etc.
365 */
366 std::string name;
367
368 /**
369 * @brief State of activity or the custom user status.
370 * e.g. "Waiting in lobby".
371 */
372 std::string state;
373
374 /**
375 * @brief What the player is currently doing.
376 */
377 std::string details;
378
379 /**
380 * @brief Images for the presence and their hover texts.
381 */
382 activity_assets assets;
383
384 /**
385 * @brief URL of activity (this is also named details).
386 *
387 * @note Only applicable for certain sites such a YouTube
388 */
389 std::string url;
390
391 /**
392 * @brief The custom buttons shown in the Rich Presence (max 2).
393 */
394 std::vector<activity_button> buttons;
395
396 /**
397 * @brief The emoji used for the custom status.
398 */
399 dpp::emoji emoji;
400
401 /**
402 * @brief Information of the current party if there is one.
403 */
404 activity_party party;
405
406 /**
407 * @brief Secrets for rich presence joining and spectating.
408 */
409 activity_secrets secrets;
410
411 /**
412 * @brief Activity type.
413 */
414 activity_type type;
415
416 /**
417 * @brief Time activity was created.
418 */
419 time_t created_at;
420
421 /**
422 * @brief Start time.
423 * e.g. when game was started.
424 */
425 time_t start;
426
427 /**
428 * @brief End time.
429 * e.g. for songs on spotify.
430 */
431 time_t end;
432
433 /**
434 * @brief Creating application.
435 * e.g. a linked account on the user's client.
436 */
437 snowflake application_id;
438
439 /**
440 * @brief Flags bitmask from dpp::activity_flags.
441 */
442 uint8_t flags;
443
444 /**
445 * @brief Whether or not the activity is an instanced game session.
446 */
447 bool is_instance;
448
449 /**
450 * @brief Get the assets large image url if they have one, otherwise returns an empty string. In case of prefixed image IDs (mp:{image_id}) it returns an empty string.
451 *
452 * @see https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-asset-image
453 *
454 * @param size The size of the image in pixels. It can be any power of two between 16 and 4096,
455 * otherwise the default sized image is returned.
456 * @param format The format to use for the avatar. It can be one of `i_webp`, `i_jpg` or `i_png`.
457 * @return std::string image url or an empty string, if required attributes are missing or an invalid format was passed
458 */
459 std::string get_large_asset_url(uint16_t size = 0, const image_type format = i_png) const;
460
461 /**
462 * @brief Get the assets small image url if they have one, otherwise returns an empty string. In case of prefixed image IDs (mp:{image_id}) it returns an empty string.
463 *
464 * @see https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-asset-image
465 *
466 * @param size The size of the image in pixels. It can be any power of two between 16 and 4096,
467 * otherwise the default sized image is returned.
468 * @param format The format to use for the avatar. It can be one of `i_webp`, `i_jpg` or `i_png`.
469 * @return std::string image url or an empty string, if required attributes are missing or an invalid format was passed
470 */
471 std::string get_small_asset_url(uint16_t size = 0, const image_type format = i_png) const;
472
473 activity();
474
475 /**
476 * @brief Construct a new activity
477 *
478 * @param typ activity type
479 * @param nam Name of the activity
480 * @param stat State of the activity
481 * @param url_ url of the activity, only works for certain sites, such as YouTube
482 */
483 activity(const activity_type typ, const std::string& nam, const std::string& stat, const std::string& url_);
484 };
485
486 /**
487 * @brief Represents user presence, e.g. what game they are playing and if they are online
488 */
489 class DPP_EXPORT presence : public json_interface<presence> {
490 protected:
491 friend struct json_interface<presence>;
492
493 /** Fill this object from json.
494 * @param j JSON object to fill from
495 * @return A reference to self
496 */
497 presence& fill_from_json_impl(nlohmann::json* j);
498
499 /** Build JSON from this object.
500 *
501 * @note This excludes any part of the presence object that are not valid for websockets and bots,
502 * and includes websocket opcode 3. You will not get what you expect if you call this on a user's
503 * presence received from on_presence_update or on_guild_create!
504 *
505 * @param with_id Add ID to output
506 * @return The JSON text of the presence
507 */
508 virtual json to_json_impl(bool with_id = false) const;
509
510 public:
511 /**
512 * @brief The user the presence applies to.
513 */
514 snowflake user_id;
515
516 /**
517 * @brief Guild ID.
518 *
519 * @note Apparently, Discord supports this internally, but the client doesn't...
520 */
521 snowflake guild_id;
522
523 /**
524 * @brief Flags bitmask containing dpp::presence_flags
525 */
526 uint8_t flags;
527
528 /**
529 * @brief List of activities.
530 */
531 std::vector<activity> activities;
532
533 /** Constructor */
534 presence();
535
536 /**
537 * @brief Construct a new presence object with some parameters for sending to a websocket
538 *
539 * @param status Status of the activity
540 * @param type Type of activity
541 * @param activity_description Description of the activity
542 */
543 presence(presence_status status, activity_type type, const std::string& activity_description);
544
545 /**
546 * @brief Construct a new presence object with some parameters for sending to a websocket.
547 *
548 * @param status Status of the activity
549 * @param a Activity itself
550 */
551 presence(presence_status status, const activity& a);
552
553 /** Destructor */
554 ~presence();
555
556 /**
557 * @brief The users status on desktop
558 * @return The user's status on desktop
559 */
560 presence_status desktop_status() const;
561
562 /**
563 * @brief The user's status on web
564 * @return The user's status on web
565 */
566 presence_status web_status() const;
567
568 /**
569 * @brief The user's status on mobile
570 * @return The user's status on mobile
571 */
572 presence_status mobile_status() const;
573
574 /**
575 * @brief The user's status as shown to other users
576 * @return The user's status as shown to other users
577 */
578 presence_status status() const;
579
580 /**
581 * @brief Build JSON from this object.
582 *
583 * @note This excludes any part of the presence object that are not valid for websockets and bots,
584 * and includes websocket opcode 3. You will not get what you expect if you call this on a user's
585 * presence received from on_presence_update or on_guild_create!
586 *
587 * @param with_id Add ID to output
588 * @return The JSON of the presence
589 */
590 json to_json(bool with_id = false) const; // Intentional shadow of json_interface, mostly present for documentation
591 };
592
593 /**
594 * @brief A container of presences
595 */
596 typedef std::unordered_map<snowflake, presence> presence_map;
597
598 }