aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
channel.hpp
1 //
2 // channel.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/fwd.hpp"
14 #include "aegis/utility.hpp"
15 #include "aegis/ratelimit/ratelimit.hpp"
16 #include "aegis/permission.hpp"
17 #include "aegis/snowflake.hpp"
18 #include "aegis/gateway/objects/permission_overwrite.hpp"
19 #include "aegis/gateway/objects/channel.hpp"
20 #include <shared_mutex>
21 #include "aegis/futures.hpp"
22 
23 namespace aegis
24 {
25 
26 #if (AEGIS_HAS_STD_SHARED_MUTEX == 1)
27 using shared_mutex = std::shared_mutex;
28 #else
29 using shared_mutex = std::shared_timed_mutex;
30 #endif
31 
32 using json = nlohmann::json;
33 
34 using channel_type = gateway::objects::channel::channel_type;
35 
36 #pragma region rest params
38 {
40  create_message_t & user_id(snowflake param) { _user_id = param; return *this; }
41 
42  create_message_t & content(const std::string & param) { _content = param; return *this; }
43  create_message_t & embed(const json & param) { _embed = param; return *this; }
44  create_message_t & nonce(int64_t param) { _nonce = param; return *this; }
45  create_message_t & file(rest::aegis_file file)
46  {
47  this->_file.emplace(std::move(file));
48  return *this;
49  }
50  snowflake _user_id;
51  std::string _content;
52  json _embed;
53  int64_t _nonce = 0;
54  lib::optional<rest::aegis_file> _file;
55 };
56 
58 {
59  edit_message_t & message_id(snowflake param) { _message_id = param; return *this; }
60  edit_message_t & content(const std::string & param) { _content = param; return *this; }
61  edit_message_t & embed(const json & param) { _embed = param; return *this; }
62  snowflake _message_id;
63  std::string _content;
64  json _embed;
65 };
66 
68 {
69  enum class get_messages_type {
70  AROUND,
71  BEFORE,
72  AFTER,
73  LAST
74  };
75  get_messages_t & message_id(snowflake param) { _message_id = param; return *this; }
76  get_messages_t & around() { _type = get_messages_type::AROUND; return *this; }
77  get_messages_t & before() { _type = get_messages_type::BEFORE; return *this; }
78  get_messages_t & after() { _type = get_messages_type::AFTER; return *this; }
79  get_messages_t & limit(int16_t param) { _limit = param; return *this; }
80  get_messages_type _type = get_messages_type::LAST;
81  snowflake _message_id;
82  int16_t _limit;
83 };
84 
86 {
87  modify_channel_t & name(const std::string & param) { _name = param; return *this; }
88  modify_channel_t & position(int param) { _position = param; return *this; }
89  modify_channel_t & topic(const std::string & param) { _topic = param; return *this; }
90  modify_channel_t & nsfw(bool param) { _nsfw = param; return *this; }
91  modify_channel_t & bitrate(int param) { _bitrate = param; return *this; }
92  modify_channel_t & user_limit(int param) { _user_limit = param; return *this; }
93  modify_channel_t & permission_overwrites(const std::vector<gateway::objects::permission_overwrite> & param)
94  {
95  _permission_overwrites = param; return *this;
96  }
97  modify_channel_t & parent_id(snowflake param) { _parent_id = param; return *this; }
98  modify_channel_t & rate_limit_per_user(int param) { _rate_limit_per_user = param; return *this; }
99  lib::optional<std::string> _name = {};
100  lib::optional<int> _position = {};
101  lib::optional<std::string> _topic = {};
102  lib::optional<bool> _nsfw = {};
103  lib::optional<int> _bitrate = {};
104  lib::optional<int> _user_limit = {};
105  lib::optional<std::vector<gateway::objects::permission_overwrite>> _permission_overwrites = {};
106  lib::optional<snowflake> _parent_id = {};
107  lib::optional<int> _rate_limit_per_user = {};
108 };
109 
111 {
112  create_reaction_t & message_id(snowflake param) { _message_id = param; return *this; }
113  create_reaction_t & emoji_text(const std::string & param) { _emoji_text = param; return *this; }
114  snowflake _message_id;
115  std::string _emoji_text;
116 };
117 
119 {
120  delete_own_reaction_t & message_id(int64_t param) { _message_id = param; return *this; }
121  delete_own_reaction_t & emoji_text(const std::string & param) { _emoji_text = param; return *this; }
122  snowflake _message_id;
123  std::string _emoji_text;
124 };
125 
127 {
128  delete_user_reaction_t & message_id(snowflake param) { _message_id = param; return *this; }
129  delete_user_reaction_t & emoji_text(const std::string & param) { _emoji_text = param; return *this; }
130  delete_user_reaction_t & member_id(snowflake param) { _member_id = param; return *this; }
131  snowflake _message_id;
132  std::string _emoji_text;
133  snowflake _member_id;
134 };
135 
137 {
138  get_reactions_t & message_id(snowflake param) { _message_id = param; return *this; }
139  get_reactions_t & emoji_text(const std::string & param) { _emoji_text = param; return *this; }
140  snowflake _message_id;
141  std::string _emoji_text;
142 };
143 
145 {
146  edit_channel_permissions_t & overwrite_id(snowflake param) { _overwrite_id = param; return *this; }
147  edit_channel_permissions_t & allow(int64_t param) { _allow = param; return *this; }
148  edit_channel_permissions_t & deny(int64_t param) { _deny = param; return *this; }
149  edit_channel_permissions_t & type(const std::string & param) { _type = param; return *this; }
150  snowflake _overwrite_id;
151  int64_t _allow;
152  int64_t _deny;
153  std::string _type;
154 };
155 
157 {
158  create_channel_invite_t & max_age(int param) { _max_age = param; return *this; }
159  create_channel_invite_t & max_uses(int param) { _max_uses = param; return *this; }
160  create_channel_invite_t & temporary(bool param) { _temporary = param; return *this; }
161  create_channel_invite_t & unique(bool param) { _unique = param; return *this; }
162  lib::optional<int> _max_age;
163  lib::optional<int> _max_uses;
164  lib::optional<bool> _temporary;
165  lib::optional<bool> _unique;
166 };
167 #pragma endregion
168 
170 class channel
171 {
172 public:
174 
181  AEGIS_DECL channel(const snowflake channel_id, const snowflake guild_id, core * _bot, asio::io_context & _io, ratelimit::ratelimit_mgr & _ratelimit);
182 
184 
188  AEGIS_DECL guild & get_guild() const;
189 
191 
194  AEGIS_DECL guild & get_guild(std::error_code & ec) const noexcept;
195 
196 #if !defined(AEGIS_DISABLE_ALL_CACHE)
197 
201  std::string get_name() const noexcept
202  {
203  std::shared_lock<shared_mutex> l(_m);
204  std::string _name = name;
205  return std::move(_name);
206  }
207 
209 
213  {
214  return type;
215  }
216 
218 
221  AEGIS_DECL permission perms() const noexcept;
222 #endif
223 
225 
229  AEGIS_DECL aegis::future<gateway::objects::message> create_message(const std::string & content, int64_t nonce = 0);
230 
232 
238 
240 
245  AEGIS_DECL aegis::future<gateway::objects::message> create_message_embed(const std::string & content, const json & embed, int64_t nonce = 0);
246 
248 
254 
256 
261 
263 
269 
270 
272 
277  AEGIS_DECL aegis::future<gateway::objects::message> edit_message(snowflake message_id, const std::string & content);
278 
280 
286 
288 
294  AEGIS_DECL aegis::future<gateway::objects::message> edit_message_embed(snowflake message_id, const std::string & content, const json & embed);
295 
297 
303 
305 
310 
312 
316  AEGIS_DECL aegis::future<rest::rest_reply> bulk_delete_message(const std::vector<int64_t> & messages);
317 
319 
323  AEGIS_DECL aegis::future<rest::rest_reply> bulk_delete_message(const std::vector<snowflake> & messages);
324 
326 
337  AEGIS_DECL aegis::future<gateway::objects::channel> modify_channel(lib::optional<std::string> _name = {},
338  lib::optional<int> _position = {}, lib::optional<std::string> _topic = {},
339  lib::optional<bool> _nsfw = {}, lib::optional<int> _bitrate = {},
340  lib::optional<int> _user_limit = {},
341  lib::optional<std::vector<gateway::objects::permission_overwrite>> _permission_overwrites = {},
342  lib::optional<snowflake> _parent_id = {}, lib::optional<int> _rate_limit_per_user = {});
343 
345 
351  {
352  return modify_channel(obj._name, obj._position, obj._topic, obj._nsfw,
353  obj._bitrate, obj._user_limit, obj._permission_overwrites,
354  obj._parent_id, obj._rate_limit_per_user);
355  }
356 
358 
362 
364 
369  AEGIS_DECL aegis::future<rest::rest_reply> create_reaction(snowflake message_id, const std::string & emoji_text);
370 
372 
378  {
379  return create_reaction(obj._message_id, obj._emoji_text);
380  }
381 
383 
388  AEGIS_DECL aegis::future<rest::rest_reply> delete_own_reaction(snowflake message_id, const std::string & emoji_text);
389 
391 
397  {
398  return delete_own_reaction(obj._message_id, obj._emoji_text);
399  }
400 
402 
408  AEGIS_DECL aegis::future<rest::rest_reply> delete_user_reaction(snowflake message_id, const std::string & emoji_text, snowflake member_id);
409 
411 
417  {
418  return delete_user_reaction(obj._message_id, obj._emoji_text, obj._member_id);
419  }
420 
422 
427  AEGIS_DECL aegis::future<rest::rest_reply> get_reactions(snowflake message_id, const std::string & emoji_text);
428 
430 
436  {
437  return get_reactions(obj._message_id, obj._emoji_text);
438  }
439 
441 
446 
448 
455  AEGIS_DECL aegis::future<rest::rest_reply> edit_channel_permissions(snowflake _overwrite_id, int64_t _allow, int64_t _deny, const std::string & _type);
456 
458 
464  {
465  return edit_channel_permissions(obj._overwrite_id, obj._allow, obj._deny, obj._type);
466  }
467 
469 
473 
475 
482  AEGIS_DECL aegis::future<rest::rest_reply> create_channel_invite(const lib::optional<int> max_age,
483  const lib::optional<int> max_uses,
484  const lib::optional<bool> temporary,
485  const lib::optional<bool> unique);
486 
488 
494  {
495  return create_channel_invite(obj._max_age, obj._max_uses, obj._temporary, obj._unique);
496  }
497 
499 
504 
506 
510 
512 
516 
518 
522 
524 
528 
530 
534 
536 
540 
542 
546  {
547  return _bot->find_channel(parent_id);
548  }
549 
551 
555  {
556  return parent_id;
557  }
558 
560 
563  snowflake get_id() const noexcept
564  {
565  return channel_id;
566  }
567 
569 
572  snowflake get_guild_id() const noexcept
573  {
574  return guild_id;
575  }
576 
578 
581  bool is_dm() const noexcept
582  {
583  return _guild == nullptr;
584  }
585 
587 
590  shared_mutex & mtx() const noexcept
591  {
592  return _m;
593  }
594 
595 #if !defined(AEGIS_DISABLE_ALL_CACHE)
596 
600  bool nsfw() const noexcept
601  {
602  return _nsfw;
603  }
604 #endif
605 
606 private:
607  friend class guild;
608  friend class core;
609 
611  AEGIS_DECL void _load_with_guild(guild & _guild, const json & obj, shards::shard * _shard);
612 
613  AEGIS_DECL void _load_with_guild_nolock(guild & _guild, const json & obj, shards::shard * _shard);
614 
615  snowflake channel_id;
616  snowflake guild_id;
617  guild * _guild;
618  snowflake parent_id;
619 #if !defined(AEGIS_DISABLE_ALL_CACHE)
620  snowflake last_message_id = 0;
621  std::string name;
622  std::string topic;
623  bool _nsfw = false;
624  uint32_t position = 0;
625  gateway::objects::channel::channel_type type = gateway::objects::channel::channel_type::Text;
626  uint16_t bitrate = 0;
627  uint16_t user_limit = 0;
628  std::unordered_map<int64_t, gateway::objects::permission_overwrite> overrides;
629  uint16_t rate_limit_per_user = 0;
630 #endif
631  asio::io_context & _io_context;
632  mutable shared_mutex _m;
633  core * _bot = nullptr;
634  ratelimit::ratelimit_mgr & _ratelimit;
635 };
636 
637 }
aegis::future< rest::rest_reply > group_dm_remove_recipient(snowflake user_id)
Remove member from a group direct message.
Definition: channel.cpp:633
Definition: channel.hpp:110
Stores creation time and extra data specific to Discord for entities.
Definition: snowflake.hpp:21
Utility class for permission checks.
Definition: permission.hpp:57
Definition: channel.hpp:118
aegis::future< rest::rest_reply > get_pinned_messages()
Get pinned messages in channel.
Definition: channel.cpp:582
aegis::future< rest::rest_reply > create_channel_invite(create_channel_invite_t obj)
Create a new channel invite.
Definition: channel.hpp:493
Class for performing actions pertaining to a specified guild.
Definition: guild.hpp:159
channel(const snowflake channel_id, const snowflake guild_id, core *_bot, asio::io_context &_io, ratelimit::ratelimit_mgr &_ratelimit)
Constructor for the channel object.
Definition: channel.cpp:28
aegis::future< rest::rest_reply > edit_channel_permissions(snowflake _overwrite_id, int64_t _allow, int64_t _deny, const std::string &_type)
Edit channel permission override.
Definition: channel.cpp:501
Primary class for managing a bot interface.
Definition: core.hpp:157
aegis::future< rest::rest_reply > add_pinned_channel_message(snowflake message_id)
Add a pinned message in channel.
Definition: channel.cpp:590
aegis::future< gateway::objects::channel > modify_channel(lib::optional< std::string > _name={}, lib::optional< int > _position={}, lib::optional< std::string > _topic={}, lib::optional< bool > _nsfw={}, lib::optional< int > _bitrate={}, lib::optional< int > _user_limit={}, lib::optional< std::vector< gateway::objects::permission_overwrite >> _permission_overwrites={}, lib::optional< snowflake > _parent_id={}, lib::optional< int > _rate_limit_per_user={})
Modify this channel (all parameters optional)
Definition: channel.cpp:365
aegis::future< gateway::objects::channel > modify_channel(modify_channel_t obj)
Modify this channel (all parameters optional)
Definition: channel.hpp:350
aegis::future< gateway::objects::message > edit_message_embed(snowflake message_id, const std::string &content, const json &embed)
Edit an embed message in this channel.
Definition: channel.cpp:289
aegis::future< rest::rest_reply > delete_channel_permission(snowflake overwrite_id)
Delete channel permission override.
Definition: channel.cpp:559
aegis::future< rest::rest_reply > delete_user_reaction(snowflake message_id, const std::string &emoji_text, snowflake member_id)
Delete specified member reaction on message.
Definition: channel.cpp:459
Class for performing actions pertaining to a specified channel.
Definition: channel.hpp:170
aegis::future< rest::rest_reply > create_reaction(create_reaction_t obj)
Add new reaction on message.
Definition: channel.hpp:377
aegis::future< rest::rest_reply > create_reaction(snowflake message_id, const std::string &emoji_text)
Add new reaction on message.
Definition: channel.cpp:429
snowflake get_id() const noexcept
Get the aegis::snowflake of this channel.
Definition: channel.hpp:563
channel * find_channel(snowflake id) const noexcept
Obtain a pointer to a channel by snowflake.
Definition: core.cpp:342
Definition: channel.hpp:85
aegis::future< rest::rest_reply > bulk_delete_message(const std::vector< int64_t > &messages)
Delete up to 100 messages at once.
Definition: channel.cpp:325
shared_mutex & mtx() const noexcept
Get the mutex for the channel.
Definition: channel.hpp:590
permission perms() const noexcept
Get bot's permission for this channel.
Definition: channel.cpp:55
aegis::snowflake get_parent_id() const noexcept
Get parent channel snowflake.
Definition: channel.hpp:554
aegis::future< rest::rest_reply > edit_channel_permissions(edit_channel_permissions_t obj)
Edit channel permission override.
Definition: channel.hpp:463
std::string get_name() const noexcept
Get channel name.
Definition: channel.hpp:201
Definition: channel.hpp:144
gateway::objects::channel::channel_type get_type() const noexcept
Get the type of this channel.
Definition: channel.hpp:212
create_message_t & user_id(snowflake param)
Only used for sending DMs.
Definition: channel.hpp:40
snowflake get_guild_id() const noexcept
Get the snowflake of this channel's guild - 0 if DM.
Definition: channel.hpp:572
aegis::future< gateway::objects::message > create_message_embed(const std::string &content, const json &embed, int64_t nonce=0)
Send an embed message to this channel.
Definition: channel.cpp:241
bool nsfw() const noexcept
Get if this channel is set to NSFW.
Definition: channel.hpp:600
Definition: channel.hpp:126
Definition: channel.hpp:136
aegis::future< gateway::objects::message > edit_message(snowflake message_id, const std::string &content)
Edit a message in this channel.
Definition: channel.cpp:272
aegis::future< rest::rest_reply > delete_all_reactions(snowflake message_id)
Delete all reactions by message.
Definition: channel.cpp:486
aegis::channel * get_parent() const
Get parent channel.
Definition: channel.hpp:545
channel_type
Enumeration of the channel types.
Definition: channel.hpp:50
aegis::future< gateway::objects::messages > get_messages(get_messages_t obj)
Get multiple messages from this channel.
Definition: channel.cpp:181
future
Definition: futures.hpp:41
aegis::future< rest::rest_reply > trigger_typing_indicator()
Trigger typing indicator in channel (lasts 10 seconds)
Definition: channel.cpp:574
Definition: channel.hpp:37
aegis::future< rest::rest_reply > get_channel_invites()
Get active channel invites.
Definition: channel.cpp:521
Factory class for managing ratelimit bucket factory objects.
Definition: ratelimit.hpp:41
Definition: channel.hpp:67
aegis::future< rest::rest_reply > group_dm_add_recipient(snowflake user_id)
Add member to a group direct message.
Definition: channel.cpp:621
bool is_dm() const noexcept
Is this channel a DM or in a guild.
Definition: channel.hpp:581
aegis::future< rest::rest_reply > delete_user_reaction(delete_user_reaction_t obj)
Delete specified member reaction on message.
Definition: channel.hpp:416
aegis::future< rest::rest_reply > delete_channel()
Delete this channel.
Definition: channel.cpp:416
aegis::future< rest::rest_reply > delete_own_reaction(delete_own_reaction_t obj)
Delete own reaction on message.
Definition: channel.hpp:396
aegis::future< rest::rest_reply > create_channel_invite(const lib::optional< int > max_age, const lib::optional< int > max_uses, const lib::optional< bool > temporary, const lib::optional< bool > unique)
Create a new channel invite.
Definition: channel.cpp:535
aegis::future< rest::rest_reply > delete_own_reaction(snowflake message_id, const std::string &emoji_text)
Delete own reaction on message.
Definition: channel.cpp:444
aegis::future< rest::rest_reply > delete_pinned_channel_message(snowflake message_id)
Delete a pinned message in channel.
Definition: channel.cpp:604
Definition: channel.hpp:156
aegis::future< gateway::objects::message > create_message(const std::string &content, int64_t nonce=0)
Send message to this channel.
Definition: channel.cpp:150
Tracks websocket shards and their connections.
Definition: shard.hpp:42
aegis::future< gateway::objects::message > get_message(snowflake message_id)
Get message from this channel.
Definition: channel.cpp:168
guild & get_guild() const
Get a reference to the guild object this channel belongs to.
Definition: channel.cpp:38
aegis::future< rest::rest_reply > get_reactions(snowflake message_id, const std::string &emoji_text)
Get all reactions for this message.
Definition: channel.cpp:477
aegis::future< rest::rest_reply > get_reactions(get_reactions_t obj)
Get all reactions for this message.
Definition: channel.hpp:435
aegis::future< rest::rest_reply > delete_message(snowflake message_id)
Delete a message.
Definition: channel.cpp:310
Definition: channel.hpp:57