aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
user.hpp
1 //
2 // user.hpp
3 // ********
4 //
5 // Copyright (c) 2019 Sharon W (sharon at aegis dot gg)
6 //
7 // Distributed under the MIT License. (See accompanying file LICENSE)
8 //
9 
10 #pragma once
11 
12 #include "aegis/config.hpp"
13 #include "aegis/snowflake.hpp"
14 #include <nlohmann/json.hpp>
15 
16 namespace aegis
17 {
18 
19 class user;
20 class channel;
21 
22 }
23 
24 namespace aegis
25 {
26 
27 namespace gateway
28 {
29 
30 namespace objects
31 {
32 
35 struct user
36 {
38 
41  bool is_bot() const noexcept
42  {
43  return _is_bot;
44  }
45 
47 
50  void is_bot(bool isbot) noexcept
51  {
52  _is_bot = isbot;
53  }
54 
56 
59  bool is_webhook() const noexcept
60  {
61  return (_is_bot) && (discriminator == "0000" || discriminator == "0");
62  }
63 
66  std::string username;
67  //todo: make discriminator an integer
68  std::string discriminator;
69  std::string avatar;
70 private:
71  friend void from_json(const nlohmann::json& j, user& m);
72  friend void to_json(nlohmann::json& j, const user& m);
73  bool _is_bot = false;
74  bool mfa_enabled = false;
75  bool verified = false;
76 };
77 
79 inline void from_json(const nlohmann::json& j, user& m)
80 {
81  m.id = std::stoull(j["id"].get<std::string>());
82  if (j.count("guild_id") && !j["guild_id"].is_null())
83  m.guild_id = std::stoull(j["guild_id"].get<std::string>());
84  if (j.count("username") && !j["username"].is_null())
85  m.username = j["username"].get<std::string>();
86  if (j.count("discriminator") && !j["discriminator"].is_null())
87  m.discriminator = j["discriminator"].get<std::string>();
88  if (j.count("avatar") && !j["avatar"].is_null())
89  m.avatar = j["avatar"].get<std::string>();
90  if (j.count("bot") && !j["bot"].is_null())
91  m._is_bot = j["bot"];
92  if (j.count("mfa_enabled") && !j["mfa_enabled"].is_null())
93  m.mfa_enabled = j["mfa_enabled"];
94  if (j.count("verified") && !j["verified"].is_null())
95  m.verified = j["verified"];
96 }
98 
100 inline void to_json(nlohmann::json& j, const user& m)
101 {
102  j["id"] = std::to_string(m.id);
103  j["guild_id"] = std::to_string(m.guild_id);
104  j["username"] = m.username;
105  j["discriminator"] = m.discriminator;
106  j["bot"] = m._is_bot;
107  j["mfa_enabled"] = m.mfa_enabled;
108  j["verified"] = m.verified;
109 }
111 
112 }
113 
114 }
115 
116 }
Stores creation time and extra data specific to Discord for entities.
Definition: snowflake.hpp:21
Definition: user.hpp:35
std::string username
Definition: user.hpp:66
snowflake id
Definition: user.hpp:64
Stores user-specific and guild-specific attributes of users.
Definition: user.hpp:37
bool is_webhook() const noexcept
Returns whether user is a webhook.
Definition: user.hpp:59
bool is_bot() const noexcept
Returns whether user is a bot.
Definition: user.hpp:41
std::string avatar
Definition: user.hpp:69
void is_bot(bool isbot) noexcept
Set user bot flag.
Definition: user.hpp:50
snowflake guild_id
Definition: user.hpp:65
std::string discriminator
Definition: user.hpp:68