Skip to content

Commit 8a952bf

Browse files
Merge pull request #86 from brainboxdotcc/dev
Dev
2 parents 5f76c80 + 6564867 commit 8a952bf

File tree

6 files changed

+104
-24
lines changed

6 files changed

+104
-24
lines changed

include/dpp/channel.h

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,39 @@ class DPP_EXPORT channel : public managed, public json_interface<channel> {
504504
channel& set_rate_limit_per_user(const uint16_t rate_limit_per_user);
505505

506506
/**
507-
* @brief Add a permission_overwrite to this channel object
508-
*
509-
* @param id ID of the role or the member you want to add overwrite for
507+
* @brief Add permission overwrites for a user or role.
508+
* If the channel already has permission overwrites for the passed target, the existing ones will be adjusted by the passed permissions
509+
*
510+
* @param target ID of the role or the member you want to adjust overwrites for
510511
* @param type type of overwrite
511-
* @param allowed_permissions bitmask of allowed permissions (refer to enum dpp::permissions) for this user/role in this channel
512-
* @param denied_permissions bitmask of denied permissions (refer to enum dpp::permissions) for this user/role in this channel
512+
* @param allowed_permissions bitmask of dpp::permissions you want to allow for this user/role in this channel. Note: You can use the dpp::permission class
513+
* @param denied_permissions bitmask of dpp::permissions you want to deny for this user/role in this channel. Note: You can use the dpp::permission class
513514
*
514-
* @return Reference to self, so these method calls may be chained
515+
* @return Reference to self, so these method calls may be chained
516+
*/
517+
channel& add_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions);
518+
/**
519+
* @brief Set permission overwrites for a user or role on this channel object. Old permission overwrites for the target will be overwritten
520+
*
521+
* @param target ID of the role or the member you want to set overwrites for
522+
* @param type type of overwrite
523+
* @param allowed_permissions bitmask of allowed dpp::permissions for this user/role in this channel. Note: You can use the dpp::permission class
524+
* @param denied_permissions bitmask of denied dpp::permissions for this user/role in this channel. Note: You can use the dpp::permission class
525+
*
526+
* @return Reference to self, so these method calls may be chained
527+
*
528+
* @note If both `allowed_permissions` and `denied_permissions` parameters are 0, the permission overwrite for the target will be removed
529+
*/
530+
channel& set_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions);
531+
/**
532+
* @brief Remove channel specific permission overwrites of a user or role
533+
*
534+
* @param target ID of the role or the member you want to remove permission overwrites of
535+
* @param type type of overwrite
536+
*
537+
* @return Reference to self, so these method calls may be chained
515538
*/
516-
channel& add_permission_overwrite(const snowflake id, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions);
539+
channel& remove_permission_overwrite(const snowflake target, const overwrite_type type);
517540

518541
/**
519542
* @brief Get the channel type

include/dpp/invite.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,21 @@
2525
#include <dpp/stage_instance.h>
2626
#include <unordered_map>
2727
#include <dpp/json_interface.h>
28+
#include <dpp/channel.h>
29+
#include <dpp/user.h>
30+
#include <dpp/guild.h>
2831

2932
namespace dpp {
3033

34+
/**
35+
* @brief Invite target types for dpp::invite
36+
*/
37+
enum invite_target_t : uint8_t {
38+
itt_none = 0, //!< Undefined invite target type
39+
itt_stream = 1, //!< Stream target type
40+
itt_embedded_application = 2, //!< Embedded Application target type
41+
};
42+
3143
/**
3244
* @brief Represents an invite to a discord guild or channel
3345
*/
@@ -43,18 +55,28 @@ class DPP_EXPORT invite : public json_interface<invite> {
4355
/** Guild ID this invite is for
4456
*/
4557
snowflake guild_id;
58+
/** The partial guild this invite is for. Only filled in retrieved invites
59+
*/
60+
guild destination_guild;
4661
/** Channel ID this invite is for
4762
*/
4863
snowflake channel_id;
64+
/** The partial channel this invite is for. Only filled in retrieved invites
65+
*/
66+
channel destination_channel;
4967
/** User ID who created this invite
68+
* @deprecated Use the `inviter` field instead
5069
*/
5170
snowflake inviter_id;
71+
/** User who created this invite
72+
*/
73+
user inviter;
5274
/** The user ID whose stream to display for this voice channel stream invite
5375
*/
5476
snowflake target_user_id;
55-
/** Target type
77+
/** Target type for this voice channel invite
5678
*/
57-
uint8_t target_type;
79+
invite_target_t target_type;
5880
/** Approximate number of online users
5981
* @note Only returned from cluster::invite_get
6082
*/
@@ -68,7 +90,7 @@ class DPP_EXPORT invite : public json_interface<invite> {
6890
uint32_t max_age;
6991
/** Maximum number of uses, or 0 for unlimited. Must be between 0 and 100. Defaults to 0
7092
*/
71-
uint32_t max_uses;
93+
uint8_t max_uses;
7294
/** Whether this invite only grants temporary membership
7395
*/
7496
bool temporary;

src/dpp/channel.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,42 @@ channel& channel::set_user_limit(const uint8_t user_limit) {
219219
return *this;
220220
}
221221

222-
channel& channel::add_permission_overwrite(const snowflake id, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions) {
223-
permission_overwrite po {id, allowed_permissions, denied_permissions, type};
222+
channel& channel::add_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions) {
223+
for (auto &o : this->permission_overwrites) {
224+
if (o.id == target && o.type == type) {
225+
o.allow.remove(denied_permissions);
226+
o.allow.add(allowed_permissions);
227+
o.deny.remove(allowed_permissions);
228+
o.deny.add(denied_permissions);
229+
return *this;
230+
}
231+
}
232+
permission_overwrite po {target, allowed_permissions, denied_permissions, type};
224233
this->permission_overwrites.push_back(po);
225234
return *this;
226235
}
227236

237+
channel& channel::set_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions) {
238+
this->remove_permission_overwrite(target, type);
239+
if (allowed_permissions != 0 || denied_permissions != 0) {
240+
permission_overwrite po{target, allowed_permissions, denied_permissions, type};
241+
this->permission_overwrites.push_back(po);
242+
}
243+
return *this;
244+
}
245+
246+
channel& channel::remove_permission_overwrite(const dpp::snowflake target, const dpp::overwrite_type type) {
247+
auto it = this->permission_overwrites.begin();
248+
while (it != this->permission_overwrites.end()) {
249+
if (it->id == target && it->type == type) {
250+
it = this->permission_overwrites.erase(it);
251+
} else {
252+
it++;
253+
}
254+
}
255+
return *this;
256+
}
257+
228258
bool channel::is_nsfw() const {
229259
return flags & dpp::c_nsfw;
230260
}

src/dpp/invite.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace dpp {
2828

2929
using json = nlohmann::json;
3030

31-
invite::invite() : expires_at(0), guild_id(0), channel_id(0), inviter_id(0), target_user_id(0), target_type(0), approximate_presence_count(0), approximate_member_count(0), max_age(86400), max_uses(0), temporary(false), unique(false), uses(0), created_at(0)
31+
invite::invite() : expires_at(0), guild_id(0), channel_id(0), inviter_id(0), target_user_id(0), target_type(itt_none), approximate_presence_count(0), approximate_member_count(0), max_age(86400), max_uses(0), temporary(false), unique(false), uses(0), created_at(0)
3232
{
3333
}
3434

@@ -37,22 +37,27 @@ invite& invite::fill_from_json(nlohmann::json* j) {
3737
expires_at = (j->contains("expires_at")) ? ts_not_null(j, "expires_at") : 0;
3838
created_at = (j->contains("created_at")) ? ts_not_null(j, "created_at") : 0;
3939
if (j->contains("guild") && !j->at("guild").is_null()) {
40-
guild_id = snowflake_not_null(&((*j)["guild"]), "id");
41-
} else if (j->contains("guild_id")) { // check ID for the invite create event
40+
destination_guild = dpp::guild().fill_from_json(&((*j)["guild"]));
41+
guild_id = destination_guild.id;
42+
} else if (j->contains("guild_id")) { // check ID for invite gateway events
4243
guild_id = snowflake_not_null(j, "guild_id");
4344
}
4445
if (j->contains("channel") && !j->at("channel").is_null()) {
45-
channel_id = snowflake_not_null(&((*j)["channel"]), "id");
46-
} else if (j->contains("channel_id")) { // check ID for the invite create event
46+
destination_channel = dpp::channel().fill_from_json(&((*j)["channel"]));
47+
channel_id = destination_channel.id;
48+
} else if (j->contains("channel_id")) { // check ID for invite gateway events
4749
channel_id = snowflake_not_null(j, "channel_id");
4850
}
49-
inviter_id = (j->contains("inviter")) ? snowflake_not_null(&((*j)["inviter"]), "id") : 0;
51+
if (j->contains("inviter") && !j->at("inviter").is_null()) {
52+
inviter = dpp::user().fill_from_json(&((*j)["inviter"]));
53+
inviter_id = inviter.id;
54+
}
5055
target_user_id = (j->contains("target_user")) ? snowflake_not_null(&((*j)["target_user"]), "id") : 0;
51-
target_type = int8_not_null(j, "target_type");
56+
target_type = static_cast<invite_target_t>(int8_not_null(j, "target_type"));
5257
approximate_presence_count = int32_not_null(j, "approximate_presence_count");
5358
approximate_member_count = int32_not_null(j, "approximate_member_count");
5459
max_age = int32_not_null(j, "max_age");
55-
max_uses = int32_not_null(j, "max_uses");
60+
max_uses = int8_not_null(j, "max_uses");
5661
temporary = bool_not_null(j, "temporary");
5762
unique = bool_not_null(j, "unique");
5863
uses = (j->contains("uses")) ? int32_not_null(j, "uses") : 0;
@@ -68,7 +73,7 @@ std::string invite::build_json(bool with_id) const {
6873
j["max_uses"] = max_uses;
6974
if (target_user_id > 0)
7075
j["target_user"] = target_user_id;
71-
if (target_type > 0)
76+
if (target_type != itt_none)
7277
j["target_type"] = target_type;
7378
if (temporary)
7479
j["temporary"] = temporary;

src/unittest/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,15 +991,15 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
991991
if (event.is_error()) return;
992992

993993
auto created = event.get<dpp::invite>();
994-
if (!created.code.empty() && created.channel_id == TEST_TEXT_CHANNEL_ID && created.guild_id == TEST_GUILD_ID && created.inviter_id != 0) {
994+
if (!created.code.empty() && created.channel_id == TEST_TEXT_CHANNEL_ID && created.guild_id == TEST_GUILD_ID && created.inviter.id == bot.me.id) {
995995
set_test("INVITE_CREATE", true);
996996
}
997997

998998
bot.invite_get(created.code, [&bot, created](const dpp::confirmation_callback_t &event) {
999999
if (event.is_error()) return;
10001000

10011001
auto retrieved = event.get<dpp::invite>();
1002-
if (retrieved.code == created.code && retrieved.expires_at == 0 && retrieved.guild_id == created.guild_id && retrieved.channel_id == created.channel_id && retrieved.inviter_id == created.inviter_id) {
1002+
if (retrieved.code == created.code && retrieved.expires_at == 0 && retrieved.guild_id == created.guild_id && retrieved.channel_id == created.channel_id && retrieved.inviter.id == created.inviter.id) {
10031003
set_test("INVITE_GET", true);
10041004
}
10051005

0 commit comments

Comments
 (0)