Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking, fix: guild_member state tracking #909

Merged
merged 13 commits into from
Oct 2, 2023
Merged
36 changes: 36 additions & 0 deletions include/dpp/guild.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ enum guild_member_flags : uint16_t {
gm_bypasses_verification = 0b0000000010000000,
/** Member has started onboarding */
gm_started_onboarding = 0b0000000100000000,
gm_roles_action = 0b0000001000000000,
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
gm_nickname_action = 0b0000010000000000,
};

/**
Expand Down Expand Up @@ -378,6 +380,40 @@ class DPP_EXPORT guild_member {
* @return std::string mention
*/
std::string get_mention() const;

/**
* @brief Add a role to this member
* @note This call sets the role change bit, which causes the new role
* list to be sent if this is passed to dpp::clusterguild_edit_member
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
* or dpp::cluster::guild_add_member
*
* @param role_id Role ID to add
* @return guild_member& Reference to self
*/
guild_member& add_role(dpp::snowflake role_id);

/**
* @brief Remove a role from this member
* @note This call sets the role change bit, which causes the new role
* list to be sent if this is passed to dpp::clusterguild_edit_member
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
* or dpp::cluster::guild_add_member
*
* @param role_id Role ID to remove
* @return guild_member& Reference to self
*/
guild_member& remove_role(dpp::snowflake role_id);

/**
* @brief Set a new role list for this member
* @note This call sets the role change bit, which causes the new role
* list to be sent if this is passed to dpp::clusterguild_edit_member
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
* or dpp::cluster::guild_add_member
*
* @param role_ids Roles to set
* @return guild_member& Reference to self
*/
guild_member& set_roles(const std::vector<dpp::snowflake> &role_ids);

braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
45 changes: 35 additions & 10 deletions src/dpp/guild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ std::string guild_member::get_mention() const {

guild_member& guild_member::set_nickname(const std::string& nick) {
this->nickname = nick;
this->flags |= gm_nickname_action;
return *this;
}

guild_member& guild_member::add_role(dpp::snowflake role_id) {
roles.emplace_back(role_id);
flags |= gm_roles_action;
return *this;
}

guild_member& guild_member::remove_role(dpp::snowflake role_id) {
roles.erase(std::remove_if(roles.begin(), roles.end(), [&role_id](dpp::snowflake role) {
Mishura4 marked this conversation as resolved.
Show resolved Hide resolved
return role == role_id;
}), roles.end());
flags |= gm_roles_action;
return *this;
}

guild_member& guild_member::set_roles(const std::vector<dpp::snowflake> &role_ids) {
roles = role_ids;
flags |= gm_roles_action;
return *this;
}

Expand Down Expand Up @@ -211,19 +232,23 @@ std::string guild_member::build_json(bool with_id) const {
}
}

if (!this->nickname.empty()) {
j["nick"] = this->nickname;
} else {
j["nick"] = json::value_t::null;
if (this->flags & gm_nickname_action) {
if (!this->nickname.empty()) {
j["nick"] = this->nickname;
} else {
j["nick"] = json::value_t::null;
}
}

if (!this->roles.empty()) {
j["roles"] = {};
for (auto & role : this->roles) {
j["roles"].push_back(std::to_string(role));
if (this->flags & gm_roles_action) {
if (!this->roles.empty()) {
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
j["roles"] = {};
for (auto & role : this->roles) {
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
j["roles"].push_back(std::to_string(role));
}
} else {
j["roles"] = {};
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
j["roles"] = {};
}

if (this->flags & gm_voice_action) {
Expand Down
3 changes: 2 additions & 1 deletion src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ DPP_TEST(OPTCHOICE_STRING, "command_option_choice::fill_from_json: string", tf_o
DPP_TEST(HOSTINFO, "https_client::get_host_info()", tf_offline);
DPP_TEST(HTTPS, "https_client HTTPS request", tf_online);
DPP_TEST(HTTP, "https_client HTTP request", tf_offline);
DPP_TEST(MULTIHEADER, "multiheader cookie test", tf_offline);
DPP_TEST(RUNONCE, "run_once<T>", tf_offline);
DPP_TEST(WEBHOOK, "webhook construct from URL", tf_offline);
DPP_TEST(MD_ESC_1, "Markdown escaping (ignore code block contents)", tf_offline);
Expand Down Expand Up @@ -221,6 +220,8 @@ DPP_TEST(INVITE_DELETE, "cluster::invite_delete", tf_online);

/* Extended set -- Less important, skipped on the master branch due to rate limits and GitHub actions limitations*/
/* To execute, run unittests with "full" command line argument */
DPP_TEST(MULTIHEADER, "multiheader cookie test", tf_offline | tf_extended); // Fails in the EU as cookies are not sent without acceptance

DPP_TEST(VOICECONN, "Connect to voice channel", tf_online | tf_extended);
DPP_TEST(VOICESEND, "Send audio to voice channel", tf_online | tf_extended); // udp unreliable on gitbub
DPP_TEST(MESSAGEPIN, "Pinning a channel message", tf_online | tf_extended);
Expand Down