Skip to content

Commit

Permalink
Merge pull request #86 from brainboxdotcc/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Commandserver authored Jun 2, 2023
2 parents 5f76c80 + 6564867 commit 8a952bf
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 24 deletions.
37 changes: 30 additions & 7 deletions include/dpp/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,16 +504,39 @@ class DPP_EXPORT channel : public managed, public json_interface<channel> {
channel& set_rate_limit_per_user(const uint16_t rate_limit_per_user);

/**
* @brief Add a permission_overwrite to this channel object
*
* @param id ID of the role or the member you want to add overwrite for
* @brief Add permission overwrites for a user or role.
* If the channel already has permission overwrites for the passed target, the existing ones will be adjusted by the passed permissions
*
* @param target ID of the role or the member you want to adjust overwrites for
* @param type type of overwrite
* @param allowed_permissions bitmask of allowed permissions (refer to enum dpp::permissions) for this user/role in this channel
* @param denied_permissions bitmask of denied permissions (refer to enum dpp::permissions) for this user/role in this channel
* @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
* @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
*
* @return Reference to self, so these method calls may be chained
* @return Reference to self, so these method calls may be chained
*/
channel& add_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions);
/**
* @brief Set permission overwrites for a user or role on this channel object. Old permission overwrites for the target will be overwritten
*
* @param target ID of the role or the member you want to set overwrites for
* @param type type of overwrite
* @param allowed_permissions bitmask of allowed dpp::permissions for this user/role in this channel. Note: You can use the dpp::permission class
* @param denied_permissions bitmask of denied dpp::permissions for this user/role in this channel. Note: You can use the dpp::permission class
*
* @return Reference to self, so these method calls may be chained
*
* @note If both `allowed_permissions` and `denied_permissions` parameters are 0, the permission overwrite for the target will be removed
*/
channel& set_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions);
/**
* @brief Remove channel specific permission overwrites of a user or role
*
* @param target ID of the role or the member you want to remove permission overwrites of
* @param type type of overwrite
*
* @return Reference to self, so these method calls may be chained
*/
channel& add_permission_overwrite(const snowflake id, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions);
channel& remove_permission_overwrite(const snowflake target, const overwrite_type type);

/**
* @brief Get the channel type
Expand Down
28 changes: 25 additions & 3 deletions include/dpp/invite.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@
#include <dpp/stage_instance.h>
#include <unordered_map>
#include <dpp/json_interface.h>
#include <dpp/channel.h>
#include <dpp/user.h>
#include <dpp/guild.h>

namespace dpp {

/**
* @brief Invite target types for dpp::invite
*/
enum invite_target_t : uint8_t {
itt_none = 0, //!< Undefined invite target type
itt_stream = 1, //!< Stream target type
itt_embedded_application = 2, //!< Embedded Application target type
};

/**
* @brief Represents an invite to a discord guild or channel
*/
Expand All @@ -43,18 +55,28 @@ class DPP_EXPORT invite : public json_interface<invite> {
/** Guild ID this invite is for
*/
snowflake guild_id;
/** The partial guild this invite is for. Only filled in retrieved invites
*/
guild destination_guild;
/** Channel ID this invite is for
*/
snowflake channel_id;
/** The partial channel this invite is for. Only filled in retrieved invites
*/
channel destination_channel;
/** User ID who created this invite
* @deprecated Use the `inviter` field instead
*/
snowflake inviter_id;
/** User who created this invite
*/
user inviter;
/** The user ID whose stream to display for this voice channel stream invite
*/
snowflake target_user_id;
/** Target type
/** Target type for this voice channel invite
*/
uint8_t target_type;
invite_target_t target_type;
/** Approximate number of online users
* @note Only returned from cluster::invite_get
*/
Expand All @@ -68,7 +90,7 @@ class DPP_EXPORT invite : public json_interface<invite> {
uint32_t max_age;
/** Maximum number of uses, or 0 for unlimited. Must be between 0 and 100. Defaults to 0
*/
uint32_t max_uses;
uint8_t max_uses;
/** Whether this invite only grants temporary membership
*/
bool temporary;
Expand Down
34 changes: 32 additions & 2 deletions src/dpp/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,42 @@ channel& channel::set_user_limit(const uint8_t user_limit) {
return *this;
}

channel& channel::add_permission_overwrite(const snowflake id, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions) {
permission_overwrite po {id, allowed_permissions, denied_permissions, type};
channel& channel::add_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions) {
for (auto &o : this->permission_overwrites) {
if (o.id == target && o.type == type) {
o.allow.remove(denied_permissions);
o.allow.add(allowed_permissions);
o.deny.remove(allowed_permissions);
o.deny.add(denied_permissions);
return *this;
}
}
permission_overwrite po {target, allowed_permissions, denied_permissions, type};
this->permission_overwrites.push_back(po);
return *this;
}

channel& channel::set_permission_overwrite(const snowflake target, const overwrite_type type, const uint64_t allowed_permissions, const uint64_t denied_permissions) {
this->remove_permission_overwrite(target, type);
if (allowed_permissions != 0 || denied_permissions != 0) {
permission_overwrite po{target, allowed_permissions, denied_permissions, type};
this->permission_overwrites.push_back(po);
}
return *this;
}

channel& channel::remove_permission_overwrite(const dpp::snowflake target, const dpp::overwrite_type type) {
auto it = this->permission_overwrites.begin();
while (it != this->permission_overwrites.end()) {
if (it->id == target && it->type == type) {
it = this->permission_overwrites.erase(it);
} else {
it++;
}
}
return *this;
}

bool channel::is_nsfw() const {
return flags & dpp::c_nsfw;
}
Expand Down
23 changes: 14 additions & 9 deletions src/dpp/invite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace dpp {

using json = nlohmann::json;

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)
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)
{
}

Expand All @@ -37,22 +37,27 @@ invite& invite::fill_from_json(nlohmann::json* j) {
expires_at = (j->contains("expires_at")) ? ts_not_null(j, "expires_at") : 0;
created_at = (j->contains("created_at")) ? ts_not_null(j, "created_at") : 0;
if (j->contains("guild") && !j->at("guild").is_null()) {
guild_id = snowflake_not_null(&((*j)["guild"]), "id");
} else if (j->contains("guild_id")) { // check ID for the invite create event
destination_guild = dpp::guild().fill_from_json(&((*j)["guild"]));
guild_id = destination_guild.id;
} else if (j->contains("guild_id")) { // check ID for invite gateway events
guild_id = snowflake_not_null(j, "guild_id");
}
if (j->contains("channel") && !j->at("channel").is_null()) {
channel_id = snowflake_not_null(&((*j)["channel"]), "id");
} else if (j->contains("channel_id")) { // check ID for the invite create event
destination_channel = dpp::channel().fill_from_json(&((*j)["channel"]));
channel_id = destination_channel.id;
} else if (j->contains("channel_id")) { // check ID for invite gateway events
channel_id = snowflake_not_null(j, "channel_id");
}
inviter_id = (j->contains("inviter")) ? snowflake_not_null(&((*j)["inviter"]), "id") : 0;
if (j->contains("inviter") && !j->at("inviter").is_null()) {
inviter = dpp::user().fill_from_json(&((*j)["inviter"]));
inviter_id = inviter.id;
}
target_user_id = (j->contains("target_user")) ? snowflake_not_null(&((*j)["target_user"]), "id") : 0;
target_type = int8_not_null(j, "target_type");
target_type = static_cast<invite_target_t>(int8_not_null(j, "target_type"));
approximate_presence_count = int32_not_null(j, "approximate_presence_count");
approximate_member_count = int32_not_null(j, "approximate_member_count");
max_age = int32_not_null(j, "max_age");
max_uses = int32_not_null(j, "max_uses");
max_uses = int8_not_null(j, "max_uses");
temporary = bool_not_null(j, "temporary");
unique = bool_not_null(j, "unique");
uses = (j->contains("uses")) ? int32_not_null(j, "uses") : 0;
Expand All @@ -68,7 +73,7 @@ std::string invite::build_json(bool with_id) const {
j["max_uses"] = max_uses;
if (target_user_id > 0)
j["target_user"] = target_user_id;
if (target_type > 0)
if (target_type != itt_none)
j["target_type"] = target_type;
if (temporary)
j["temporary"] = temporary;
Expand Down
4 changes: 2 additions & 2 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,15 +991,15 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
if (event.is_error()) return;

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

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

auto retrieved = event.get<dpp::invite>();
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) {
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) {
set_test("INVITE_GET", true);
}

Expand Down

0 comments on commit 8a952bf

Please sign in to comment.