aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
message.hpp
1 //
2 // message.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 "aegis/shards/shard.hpp"
15 #include "aegis/rest/rest_reply.hpp"
16 #include "aegis/gateway/objects/attachment.hpp"
17 #include "aegis/gateway/objects/embed.hpp"
18 #include "aegis/gateway/objects/reaction.hpp"
19 #include "aegis/gateway/objects/user.hpp"
20 #include <nlohmann/json.hpp>
21 #include "aegis/futures.hpp"
22 
23 namespace aegis
24 {
25 
26 struct edit_message_t;
27 
28 namespace gateway
29 {
30 
31 namespace objects
32 {
33 
34 class message;
35 
37 AEGIS_DECL void from_json(const nlohmann::json& j, objects::message& m);
38 
39 AEGIS_DECL void to_json(nlohmann::json& j, const objects::message& m);
41 
43 enum message_type
44 {
45  Default = 0,
46  RecipientAdd = 1,
47  RecipientRemove = 2,
48  Call = 3,
49  ChannelNameChange = 4,
50  ChannelIconChange = 5,
51  ChannelPinnedMessage = 6,
52  GuildMemberJoin = 7
53 };
54 
57 class message
58 {
59 public:
61 
66  explicit message(const std::string & content, aegis::channel * _channel, aegis::guild * _guild) noexcept
67  : _content(content)
68  , _channel(_channel)
69  , _guild(_guild)
70  {
71  }
72 
74 
78  message(const std::string & _json, aegis::core * _core) noexcept
79  : _core(_core)
80  {
81  from_json(nlohmann::json::parse(_json), *this);
82  populate_self();
83  }
84 
86 
90  message(const nlohmann::json & _json, aegis::core * _core) noexcept
91  : _core(_core)
92  {
93  from_json(_json, *this);
94  populate_self();
95  }
96 
98 
101  message(aegis::core * _core) noexcept
102  : _core(_core)
103  {
104  populate_self();
105  }
106 
109 
112  AEGIS_DECL void set_channel(aegis::channel * _channel)
113  {
114  this->_channel = _channel;
115  }
116 
119 
122  AEGIS_DECL void set_guild(aegis::guild * _guild)
123  {
124  this->_guild = _guild;
125  }
126 
127  message() = default;
128  message & operator=(const message &) = default;
129  message(const message&) = default;
130  message(message && msg) = default;
131 
133  inline bool operator==(const std::string& rhs)
134  {
135  return _content == rhs;
136  }
137 
139  inline bool operator!=(const std::string& rhs)
140  {
141  return !(*this == rhs);
142  }
143 
145  inline bool operator==(const char * rhs)
146  {
147  return _content == rhs;
148  }
149 
151  inline bool operator!=(const char * rhs)
152  {
153  return !(*this == rhs);
154  }
155 
156  std::string timestamp;
157  std::string edited_timestamp;
158  bool tts = false;
159  bool mention_everyone = false;
160  std::vector<snowflake> mentions;
161  std::vector<snowflake> mention_roles;
162  std::vector<objects::attachment> attachments;
163  std::vector<objects::embed> embeds;
164  bool pinned = false;
165  std::vector<objects::reaction> reactions;
167  std::string webhook_id;
168  objects::message_type type = Default;
171 
175  bool is_dm() const noexcept
176  {
177  return !_guild_id;
178  }
179 
181 
184  bool is_bot() const noexcept
185  {
186  return author.is_bot();
187  }
188 
190 
193  bool is_webhook() const noexcept
194  {
195  return !webhook_id.empty();
196  }
197 
199 
202  const std::string & get_content() const noexcept
203  {
204  return _content;
205  }
206 
208 
211  void set_content(const std::string & content) noexcept
212  {
213  _content = content;
214  }
215 
217 
220  snowflake get_id() const noexcept
221  {
222  return _message_id;
223  }
224 
226 
229  snowflake get_channel_id() const noexcept
230  {
231  return _channel_id;
232  }
233 
235 
238  snowflake get_guild_id() const noexcept
239  {
240  return _guild_id;
241  }
242 
244 
247  snowflake get_author_id() const noexcept
248  {
249  return _author_id;
250  }
251 
255 
258  bool has_guild() const noexcept
259  {
260  return _guild != nullptr || _guild_id != 0;
261  }
262 
264 
267  bool has_channel() const noexcept
268  {
269  return _channel != nullptr || _channel_id != 0;
270  }
271 
272 #if !defined(AEGIS_DISABLE_ALL_CACHE)
273 
277  bool has_member() const noexcept
278  {
279  return _user != nullptr || _author_id != 0;
280  }
281 #endif
282 
284 
287  AEGIS_DECL aegis::guild & get_guild();
288 
290 
293  AEGIS_DECL aegis::channel & get_channel();
294 
295 #if !defined(AEGIS_DISABLE_ALL_CACHE)
296 
300  AEGIS_DECL aegis::user & get_user();
301 #endif
302 
304 
308 
310 
314  AEGIS_DECL aegis::future<message> edit(const std::string & content);
315 
317 
322  AEGIS_DECL aegis::future<message> edit(edit_message_t & obj);
323 
325 
329  AEGIS_DECL aegis::future<rest::rest_reply> create_reaction(const std::string & content);
330 
332 
336  AEGIS_DECL aegis::future<rest::rest_reply> delete_own_reaction(const std::string & content);
337 
339 
344  AEGIS_DECL aegis::future<rest::rest_reply> delete_user_reaction(const std::string & content, const snowflake member_id);
345 
347 
351 
353 
358  std::tuple<snowflake, snowflake, snowflake, snowflake> get_related_ids() const noexcept
359  {
360  return std::tuple<snowflake, snowflake, snowflake, snowflake>{ _channel_id, _guild_id, _message_id, _author_id };
361  };
362 
363 private:
364  friend AEGIS_DECL void from_json(const nlohmann::json& j, objects::message& m);
365  friend AEGIS_DECL void to_json(nlohmann::json& j, const objects::message& m);
366  friend class aegis::core;
367 
368  AEGIS_DECL void populate_self();
369 
370  std::string _content;
371  aegis::channel * _channel = nullptr;
372  aegis::guild * _guild = nullptr;
373 #if !defined(AEGIS_DISABLE_ALL_CACHE)
374  aegis::user * _user = nullptr;
375 #endif
376  aegis::core * _core = nullptr;
377  snowflake _message_id = 0;
378  snowflake _channel_id = 0;
379  snowflake _guild_id = 0;
380  snowflake _author_id = 0;
381 };
382 
383 }
384 
385 }
386 
387 }
388 
389 #if defined(AEGIS_HEADER_ONLY)
390 #include "aegis/gateway/objects/impl/message.cpp"
391 #endif
void set_content(const std::string &content) noexcept
Set the message's content.
Definition: message.hpp:211
snowflake get_channel_id() const noexcept
Get the Channel ID of the message.
Definition: message.hpp:229
bool operator!=(const char *rhs)
Comparison of message content.
Definition: message.hpp:151
std::string webhook_id
Definition: message.hpp:167
Stores creation time and extra data specific to Discord for entities.
Definition: snowflake.hpp:21
Definition: user.hpp:35
snowflake get_guild_id() const noexcept
Get the Guild ID of the message.
Definition: message.hpp:238
message(const std::string &content, aegis::channel *_channel, aegis::guild *_guild) noexcept
Constructor for the message object.
Definition: message.hpp:66
void set_channel(aegis::channel *_channel)
Definition: message.hpp:112
aegis::guild & get_guild()
Get a reference to the guild object this message was sent in.
Definition: message.cpp:28
bool is_dm() const noexcept
Whether message is a Direct Message.
Definition: message.hpp:175
Class for performing actions pertaining to a specified guild.
Definition: guild.hpp:159
aegis::channel & get_channel()
Get a reference to the channel object this message was sent in.
Definition: message.cpp:38
aegis::future< rest::rest_reply > create_reaction(const std::string &content)
Add a reaction to this message.
Definition: message.cpp:95
Primary class for managing a bot interface.
Definition: core.hpp:157
const std::string & get_content() const noexcept
Get a reference to the content of the message.
Definition: message.hpp:202
message(const std::string &_json, aegis::core *_core) noexcept
Constructor for the message object.
Definition: message.hpp:78
aegis::future< message > edit(const std::string &content)
Edit this message.
Definition: message.cpp:75
std::vector< objects::reaction > reactions
Definition: message.hpp:165
Definition: message.hpp:57
aegis::future< rest::rest_reply > delete_all_reactions()
Delete all reactions on this message (not available for DMs)
Definition: message.cpp:125
bool pinned
Definition: message.hpp:164
Class for performing actions pertaining to a specified channel.
Definition: channel.hpp:170
std::vector< objects::embed > embeds
Definition: message.hpp:163
objects::message_type type
Definition: message.hpp:168
Stores user-specific and guild-specific attributes of users.
Definition: user.hpp:37
snowflake nonce
Definition: message.hpp:166
message(aegis::core *_core) noexcept
Constructor for the message object.
Definition: message.hpp:101
bool is_webhook() const noexcept
Whether message is from a webhook.
Definition: message.hpp:193
aegis::future< rest::rest_reply > delete_own_reaction(const std::string &content)
Delete your reaction to this message.
Definition: message.cpp:105
bool has_guild() const noexcept
Definition: message.hpp:258
bool operator!=(const std::string &rhs)
Comparison of message content.
Definition: message.hpp:139
void set_guild(aegis::guild *_guild)
Definition: message.hpp:122
bool is_bot() const noexcept
Returns whether user is a bot.
Definition: user.hpp:41
bool mention_everyone
Definition: message.hpp:159
snowflake get_author_id() const noexcept
Get the Member ID of the author of the message.
Definition: message.hpp:247
std::string timestamp
Definition: message.hpp:156
future
Definition: futures.hpp:41
bool tts
Definition: message.hpp:158
std::string edited_timestamp
Definition: message.hpp:157
aegis::future< rest::rest_reply > delete_message()
Delete this message.
Definition: message.cpp:65
std::vector< objects::attachment > attachments
Definition: message.hpp:162
user author
Definition: message.hpp:169
bool is_bot() const noexcept
Whether message is from a bot.
Definition: message.hpp:184
bool has_channel() const noexcept
Whether the message has a valid channel set.
Definition: message.hpp:267
std::vector< snowflake > mentions
Definition: message.hpp:160
message(const nlohmann::json &_json, aegis::core *_core) noexcept
Constructor for the message object.
Definition: message.hpp:90
bool has_member() const noexcept
Whether the message has a valid member set.
Definition: message.hpp:277
snowflake get_id() const noexcept
Get the ID of the message.
Definition: message.hpp:220
aegis::user & get_user()
Get a reference to the user object this message was sent by.
Definition: message.cpp:49
bool operator==(const char *rhs)
Comparison of message content.
Definition: message.hpp:145
std::vector< snowflake > mention_roles
Definition: message.hpp:161
bool operator==(const std::string &rhs)
Comparison of message content.
Definition: message.hpp:133
aegis::future< rest::rest_reply > delete_user_reaction(const std::string &content, const snowflake member_id)
Delete other user reaction to this message (not available for DMs)
Definition: message.cpp:115
std::tuple< snowflake, snowflake, snowflake, snowflake > get_related_ids() const noexcept
Obtain the relevant snowflakes related to this message.
Definition: message.hpp:358
Definition: channel.hpp:57