Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 73af636

Browse files
authored
Merge pull request #71 from hochladen/master
Finish implementation of invites and emojis
2 parents e8edae8 + d7e65da commit 73af636

File tree

6 files changed

+317
-8
lines changed

6 files changed

+317
-8
lines changed

include/aegis/gateway/objects/bans.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ban.hpp
2+
// bans.hpp
33
// ********
44
//
55
// Copyright (c) 2020 Sharon Fox (sharon at xandium dot io)

include/aegis/gateway/objects/emoji.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,30 @@ namespace gateway
2222
namespace objects
2323
{
2424

25+
struct emoji;
26+
27+
/// \cond TEMPLATES
28+
void from_json(const nlohmann::json& j, emoji& m);
29+
void to_json(nlohmann::json& j, const emoji& m);
30+
/// \endcond
31+
2532
/// Discord Emoji Object
2633
struct emoji
2734
{
35+
emoji(const std::string& _json, aegis::core* bot) noexcept
36+
{
37+
from_json(nlohmann::json::parse(_json), *this);
38+
}
39+
40+
emoji(const nlohmann::json& _json, aegis::core* bot) noexcept
41+
{
42+
from_json(_json, *this);
43+
}
44+
45+
emoji(aegis::core* bot) noexcept {}
46+
47+
emoji() noexcept {}
48+
2849
/// Formats the emoji for adding to a message
2950
/**
3051
* @returns string of formatted emoji
@@ -34,6 +55,15 @@ struct emoji
3455
return id ? fmt::format("<{}:{}:{}>", animated ? "a" : "", name, id) : name;
3556
}
3657

58+
/// Formats the emoji for use as a reaction
59+
/**
60+
* @returns string of reaction-formatted emoji
61+
*/
62+
std::string reaction() const noexcept
63+
{
64+
return id ? fmt::format("{}:{}", name, id) : name;
65+
}
66+
3767
snowflake id; /**< Emoji ID */
3868
std::string name; /**< Emoji Name */
3969
std::vector<snowflake> roles; /**< Roles this emoji is whitelisted to */
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//
2+
// invite.hpp
3+
// **********
4+
//
5+
// Copyright (c) 2020 Sharon Fox (sharon at xandium dot io)
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+
namespace gateway
20+
{
21+
22+
namespace objects
23+
{
24+
25+
enum target_user_type {
26+
STREAM = 1
27+
};
28+
29+
struct invite_metadata_t {
30+
int32_t uses; /**< # of times this invite has been used */
31+
int32_t max_uses; /**< Max # of times this invite can be used */
32+
int32_t max_age; /**< Duration (in seconds) after which the invite expires */
33+
bool temporary = false; /**< Whether this invite only grants temporary membership */
34+
std::string created_at; /**< ISO8601 timestamp of when this invite was created */
35+
};
36+
37+
struct invite;
38+
39+
/// \cond TEMPLATES
40+
void from_json(const nlohmann::json& j, invite& m);
41+
void to_json(nlohmann::json& j, const invite& m);
42+
/// \endcond
43+
44+
/// Discord Invite Object
45+
struct invite
46+
{
47+
invite(const std::string& _json, aegis::core* bot) noexcept
48+
{
49+
from_json(nlohmann::json::parse(_json), *this);
50+
}
51+
52+
invite(const nlohmann::json& _json, aegis::core* bot) noexcept
53+
{
54+
from_json(_json, *this);
55+
}
56+
57+
invite(aegis::core* bot) noexcept {}
58+
59+
invite() noexcept {}
60+
61+
std::string code; /**< Invite code (unique ID) */
62+
snowflake _guild; /**< Guild this invite is for */
63+
snowflake _channel; /**< Channel this invite is for */
64+
snowflake inviter; /**< User that created the invite */
65+
snowflake target_user; /**< Target user for this invite */
66+
target_user_type target_type = STREAM; /**< Type of user target for this invite */
67+
int32_t approximate_presence_count = 0; /**< Approximate count of online members of guild, requires target_user be set */
68+
int32_t approximate_member_count = 0; /**< Approximate count of total members of guild */
69+
invite_metadata_t metadata; /**< Extra information about invite */
70+
};
71+
72+
/// \cond TEMPLATES
73+
inline void from_json(const nlohmann::json& j, invite& m)
74+
{
75+
m.code = j["code"];
76+
if (j.count("guild") && !j["guild"].is_null())
77+
m._guild = j["guild"]["id"];
78+
if (j.count("channel") && !j["channel"].is_null())
79+
m._channel = j["channel"]["id"];
80+
if (j.count("inviter") && !j["inviter"].is_null())
81+
m.inviter = j["inviter"]["id"];
82+
if (j.count("target_user") && !j["target_user"].is_null())
83+
m.target_user = j["target_user"]["id"];
84+
if (j.count("target_user_type") && !j["target_user_type"].is_null())
85+
m.target_type = j["target_user_type"];
86+
if (j.count("approximate_presence_count") && !j["approximate_presence_count"].is_null())
87+
m.approximate_presence_count = j["approximate_presence_count"];
88+
if (j.count("approximate_member_count") && !j["approximate_member_count"].is_null())
89+
m.approximate_member_count = j["approximate_member_count"];
90+
91+
// Metadata
92+
if (j.count("uses") && !j["uses"].is_null())
93+
m.metadata.uses = j["uses"];
94+
if (j.count("max_uses") && !j["max_uses"].is_null())
95+
m.metadata.max_uses = j["max_uses"];
96+
if (j.count("max_age") && !j["max_age"].is_null())
97+
m.metadata.max_age = j["max_age"];
98+
if (j.count("temporary") && !j["temporary"].is_null())
99+
m.metadata.temporary = j["temporary"];
100+
if (j.count("created_at") && !j["created_at"].is_null())
101+
m.metadata.created_at = j["created_at"];
102+
}
103+
104+
inline void to_json(nlohmann::json& j, const invite& m)
105+
{
106+
j["code"] = m.code;
107+
j["guild"] = m._guild;
108+
j["channel"] = m._channel;
109+
j["inviter"] = m.inviter;
110+
j["target_user"] = m.target_user;
111+
j["target_user_type"] = m.target_type;
112+
j["approximate_presence_count"] = m.approximate_presence_count;
113+
j["approximate_member_count"] = m.approximate_member_count;
114+
115+
// Metadata
116+
j["uses"] = m.metadata.uses;
117+
j["max_uses"] = m.metadata.max_uses;
118+
j["max_age"] = m.metadata.max_age;
119+
j["temporary"] = m.metadata.temporary;
120+
j["created_at"] = m.metadata.created_at;
121+
}
122+
/// \endcond
123+
124+
}
125+
126+
}
127+
128+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//
2+
// invites.hpp
3+
// ***********
4+
//
5+
// Copyright (c) 2020 Sharon Fox (sharon at xandium dot io)
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/permission.hpp"
15+
#include "aegis/user.hpp"
16+
#include "invite.hpp"
17+
#include <nlohmann/json.hpp>
18+
#include <vector>
19+
20+
namespace aegis
21+
{
22+
23+
namespace gateway
24+
{
25+
26+
namespace objects
27+
{
28+
29+
struct invites;
30+
31+
/// \cond TEMPLATES
32+
void from_json(const nlohmann::json& j, invites& m);
33+
void to_json(nlohmann::json& j, const invites& m);
34+
/// \endcond
35+
36+
/// Stores info pertaining to guild invites
37+
struct invites
38+
{
39+
invites(const std::string& _json, aegis::core* bot) noexcept
40+
{
41+
from_json(nlohmann::json::parse(_json), *this);
42+
}
43+
44+
invites(const nlohmann::json& _json, aegis::core* bot) noexcept
45+
{
46+
from_json(_json, *this);
47+
}
48+
49+
invites(aegis::core* bot) noexcept {}
50+
51+
invites() noexcept {}
52+
53+
std::vector<aegis::gateway::objects::invite>::iterator begin()
54+
{
55+
return _invites.begin();
56+
}
57+
58+
std::vector<aegis::gateway::objects::invite>::iterator end()
59+
{
60+
return _invites.end();
61+
}
62+
63+
std::vector<aegis::gateway::objects::invite>::reverse_iterator rbegin()
64+
{
65+
return _invites.rbegin();
66+
}
67+
68+
std::vector<aegis::gateway::objects::invite>::reverse_iterator rend()
69+
{
70+
return _invites.rend();
71+
}
72+
73+
std::vector<aegis::gateway::objects::invite> _invites; /**< array of invites */
74+
75+
};
76+
77+
/// \cond TEMPLATES
78+
inline void from_json(const nlohmann::json& j, invites& m)
79+
{
80+
if (j.size())
81+
for (const auto& _invite : j)
82+
m._invites.push_back(_invite);
83+
}
84+
85+
inline void to_json(nlohmann::json& j, const invites& m)
86+
{
87+
if (!m._invites.empty())
88+
for (const auto& _invite : m._invites)
89+
j.push_back(_invite);
90+
}
91+
/// \endcond
92+
93+
}
94+
95+
}
96+
97+
}

include/aegis/guild.hpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "aegis/gateway/objects/member.hpp"
2020
#include "aegis/gateway/objects/ban.hpp"
2121
#include "aegis/gateway/objects/bans.hpp"
22+
#include "aegis/gateway/objects/invite.hpp"
23+
#include "aegis/gateway/objects/invites.hpp"
2224
#include <future>
2325
#include <asio.hpp>
2426
#include <shared_mutex>
@@ -633,11 +635,25 @@ class guild
633635
*/
634636
AEGIS_DECL aegis::future<rest::rest_reply> begin_guild_prune(int16_t days);
635637

638+
/// Get active guild invite
639+
/**
640+
* @param invite_code The invite code of the invite to retrieve
641+
* @returns aegis::future<gateway::objects::invite>
642+
*/
643+
AEGIS_DECL aegis::future<gateway::objects::invite> get_guild_invite(std::string invite_code);
644+
636645
/// Get active guild invites
637646
/**
638-
* @returns aegis::future<rest::rest_reply>
639-
*/
640-
AEGIS_DECL aegis::future<rest::rest_reply> get_guild_invites();
647+
* @returns aegis::future<gateway::objects::invites>
648+
*/
649+
AEGIS_DECL aegis::future<gateway::objects::invites> get_guild_invites();
650+
651+
/// Delete active guild invite
652+
/**
653+
* @param invite_code The invite code of the invite to delete
654+
* @returns aegis::future<rest::rest_reply>
655+
*/
656+
AEGIS_DECL aegis::future<rest::rest_reply> delete_guild_invite(std::string invite_code);
641657

642658
/// Get guild integrations
643659
/**
@@ -775,6 +791,17 @@ class guild
775791
return std::move(_list);
776792
}
777793

794+
/// Obtain map of emojis
795+
/**
796+
* @returns std::unordered_map<snowflake, gateway::objects::emoji> COPY of emojis
797+
*/
798+
std::unordered_map<snowflake, gateway::objects::emoji> get_emojis() const noexcept
799+
{
800+
std::shared_lock<shared_mutex> l(_m);
801+
std::unordered_map<snowflake, gateway::objects::emoji> _list = emojis;
802+
return std::move(_list);
803+
}
804+
778805
/// Obtain map of members - caller must lock guild._m to ensure no race conditions
779806
/**
780807
* @returns unordered_map<snowflake, user*> of members
@@ -792,6 +819,15 @@ class guild
792819
{
793820
return roles;
794821
}
822+
823+
/// Obtain map of emojis - caller must lock guild._m to ensure no race conditions
824+
/**
825+
* @returns unordered_map<snowflake, gateway::objects::emoji> of emojis
826+
*/
827+
const std::unordered_map<snowflake, gateway::objects::emoji>& get_emojis_nocopy() const noexcept
828+
{
829+
return emojis;
830+
}
795831
#endif
796832

797833
/// Obtain map of channels - caller must lock guild._m to ensure no race conditions

include/aegis/impl/guild.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -999,16 +999,34 @@ AEGIS_DECL aegis::future<rest::rest_reply> guild::begin_guild_prune(int16_t days
999999
return aegis::make_exception_future(error::not_implemented);
10001000
}
10011001

1002-
/**\todo Incomplete. Signature may change
1003-
*/
1004-
AEGIS_DECL aegis::future<rest::rest_reply> guild::get_guild_invites()
1002+
AEGIS_DECL aegis::future<gateway::objects::invite> guild::get_guild_invite(std::string invite_code)
1003+
{
1004+
#if !defined(AEGIS_DISABLE_ALL_CACHE)
1005+
if (!perms().can_manage_guild())
1006+
return aegis::make_exception_future<gateway::objects::invite>(error::no_permission);
1007+
#endif
1008+
1009+
return _bot->get_ratelimit().post_task<gateway::objects::invite>({ fmt::format("/invites/{}", invite_code), rest::Get });
1010+
}
1011+
1012+
AEGIS_DECL aegis::future<gateway::objects::invites> guild::get_guild_invites()
1013+
{
1014+
#if !defined(AEGIS_DISABLE_ALL_CACHE)
1015+
if (!perms().can_manage_guild())
1016+
return aegis::make_exception_future<gateway::objects::invites>(error::no_permission);
1017+
#endif
1018+
1019+
return _bot->get_ratelimit().post_task<gateway::objects::invites>({ fmt::format("/guilds/{}/invites", guild_id), rest::Get });
1020+
}
1021+
1022+
AEGIS_DECL aegis::future<rest::rest_reply> guild::delete_guild_invite(std::string invite_code)
10051023
{
10061024
#if !defined(AEGIS_DISABLE_ALL_CACHE)
10071025
if (!perms().can_manage_guild())
10081026
return aegis::make_exception_future(error::no_permission);
10091027
#endif
10101028

1011-
return aegis::make_exception_future(error::not_implemented);
1029+
return _bot->get_ratelimit().post_task({ fmt::format("/invites/{}", invite_code), rest::Delete });
10121030
}
10131031

10141032
/**\todo Incomplete. Signature may change

0 commit comments

Comments
 (0)