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

feat: added guild member flags #679

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions include/dpp/guild.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,27 @@ enum guild_flags_extra : uint16_t {

/**
* @brief Various flags that can be used to indicate the status of a guild member.
* @note Use set_mute and set_deaf member functions and do not toggle the bits yourself.
* @note Use the setter functions in dpp::guild_member and do not toggle the bits yourself.
*/
enum guild_member_flags : uint8_t {
enum guild_member_flags : uint16_t {
/** Member deafened in voice channels */
gm_deaf = 0b00000001,
gm_deaf = 0b0000000000000001,
/** Member muted in voice channels */
gm_mute = 0b00000010,
gm_mute = 0b0000000000000010,
/** Member pending verification by membership screening */
gm_pending = 0b00000100,
gm_pending = 0b0000000000000100,
/** Member has animated guild-specific avatar */
gm_animated_avatar = 0b00001000,
gm_animated_avatar = 0b0000000000001000,
/** gm_deaf or gm_mute has been toggled */
gm_voice_action = 0b00010000,
gm_voice_action = 0b0000000000010000,
/** Member has left and rejoined the guild */
gm_did_rejoin = 0b0000000000100000,
/** Member has completed onboarding */
gm_completed_onboarding = 0b0000000001000000,
/** Member is exempt from guild verification requirements */
gm_bypasses_verification = 0b0000000010000000,
/** Member has started onboarding */
gm_started_onboarding = 0b0000000100000000,
};

/**
Expand All @@ -196,7 +204,7 @@ class DPP_EXPORT guild_member {
/** Boosting since */
time_t premium_since;
/** A set of flags built from the bitmask defined by dpp::guild_member_flags */
uint8_t flags;
uint16_t flags;

/** Default constructor */
guild_member();
Expand Down Expand Up @@ -249,6 +257,38 @@ class DPP_EXPORT guild_member {
*/
bool is_pending() const;

/**
* @brief Returns true if the user has left and rejoined the guild
*
* @return true user has left and rejoined the guild
* @return false user has not rejoined
*/
bool has_rejoined() const;

/**
* @brief Returns true if the user has completed onboarding
*
* @return true user has completed onboarding
* @return false user has not completed onboarding
*/
bool has_completed_onboarding() const;

/**
* @brief Returns true if the user has started onboarding
*
* @return true user has started onboarding
* @return false user has not started onboarding yet
*/
bool has_started_onboarding() const;

/**
* @brief Returns true if the user is exempt from guild verification requirements
*
* @return true user bypasses verification
* @return false user doesn't bypass verification
*/
bool has_bypasses_verification() const;

/**
* @brief Returns true if the user's per-guild custom avatar is animated
*
Expand Down Expand Up @@ -295,6 +335,15 @@ class DPP_EXPORT guild_member {

bool operator == (guild_member const& other_member) const;

/**
* @brief Set whether the user is exempt from guild verification requirements
*
* @param is_bypassing_verification value to set
*
* @return guild_member& reference to self
*/
guild_member& set_bypasses_verification(const bool is_bypassing_verification);

/**
* @brief Set whether the user is muted in voice channels
*
Expand Down
48 changes: 46 additions & 2 deletions src/dpp/guild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ namespace dpp {

using json = nlohmann::json;

/* A mapping of discord's flag values to our bitmap (they're different bit positions to fit other stuff in) */
std::map<uint16_t , dpp::guild_member_flags> membermap = {
{ 1 << 0, dpp::gm_did_rejoin },
{ 1 << 1, dpp::gm_completed_onboarding },
{ 1 << 2, dpp::gm_bypasses_verification },
{ 1 << 3, dpp::gm_started_onboarding },
};

const std::map<std::string, std::variant<dpp::guild_flags, dpp::guild_flags_extra>> featuremap = {
{"ANIMATED_BANNER", dpp::g_animated_banner },
{"ANIMATED_ICON", dpp::g_animated_icon },
Expand Down Expand Up @@ -107,6 +115,11 @@ guild_member& guild_member::set_nickname(const std::string& nick) {
return *this;
}

guild_member& guild_member::set_bypasses_verification(const bool is_bypassing_verification) {
this->flags = (is_bypassing_verification) ? flags | gm_bypasses_verification : flags & ~gm_bypasses_verification;
return *this;
}

guild_member& guild_member::set_mute(const bool is_muted) {
this->flags = (is_muted) ? flags | gm_mute : flags & ~gm_mute;
this->flags |= gm_voice_action;
Expand Down Expand Up @@ -148,6 +161,13 @@ void from_json(const nlohmann::json& j, guild_member& gm) {
set_ts_not_null(&j, "premium_since", gm.premium_since);
set_ts_not_null(&j, "communication_disabled_until", gm.communication_disabled_until);

uint16_t flags = int16_not_null(&j, "flags");
for (auto & flag : membermap) {
if (flags & flag.first) {
gm.flags |= flag.second;
}
}

gm.roles.clear();
if (j.contains("roles") && !j.at("roles").is_null()) {
gm.roles.reserve(j.at("roles").size());
Expand Down Expand Up @@ -213,16 +233,24 @@ std::string guild_member::build_json(bool with_id) const {
j["nick"] = this->nickname;
if (!this->roles.empty()) {
j["roles"] = {};
for (auto & role : roles) {
for (auto & role : this->roles) {
j["roles"].push_back(std::to_string(role));
}
}

if (flags & gm_voice_action) {
if (this->flags & gm_voice_action) {
j["mute"] = is_muted();
j["deaf"] = is_deaf();
}

uint32_t out_flags = 0;
for (auto & flag : membermap) {
if (flags & flag.second) {
out_flags |= flag.first;
}
}
j["flags"] = out_flags;

return j.dump();
}

Expand All @@ -247,6 +275,22 @@ bool guild_member::is_pending() const {
return flags & dpp::gm_pending;
}

bool guild_member::has_rejoined() const {
return flags & dpp::gm_did_rejoin;
}

bool guild_member::has_completed_onboarding() const {
return flags & dpp::gm_completed_onboarding;
}

bool guild_member::has_started_onboarding() const {
return flags & dpp::gm_started_onboarding;
}

bool guild_member::has_bypasses_verification() const {
return flags & dpp::gm_bypasses_verification;
}

bool guild::is_large() const {
return this->flags & g_large;
}
Expand Down