fix calculating hoisted color role

also slight optimization + make Snowflake::Invalid a real Snowflake
This commit is contained in:
ouwou
2021-07-21 01:29:17 -04:00
parent ccf7c414be
commit a5e5954ae7
3 changed files with 8 additions and 11 deletions

View File

@@ -205,22 +205,17 @@ Snowflake DiscordClient::GetMemberHoistedRole(Snowflake guild_id, Snowflake user
const auto data = GetMember(user_id, guild_id);
if (!data.has_value()) return Snowflake::Invalid;
std::vector<RoleData> roles;
std::optional<RoleData> top_role;
for (const auto &id : data->Roles) {
const auto role = GetRole(id);
if (role.has_value()) {
if (role->IsHoisted || (with_color && role->Color != 0))
roles.push_back(*role);
if ((with_color && role->Color != 0x000000) || (!with_color && role->IsHoisted))
if (!top_role.has_value() || top_role->Position < role->Position)
top_role = role;
}
}
if (roles.size() == 0) return Snowflake::Invalid;
std::sort(roles.begin(), roles.end(), [this](const RoleData &a, const RoleData &b) -> bool {
return a.Position > b.Position;
});
return roles[0].ID;
return top_role.has_value() ? top_role->ID : Snowflake::Invalid;
}
std::optional<RoleData> DiscordClient::GetMemberHighestRole(Snowflake guild_id, Snowflake user_id) const {

View File

@@ -5,6 +5,8 @@
constexpr static uint64_t DiscordEpochSeconds = 1420070400;
const Snowflake Snowflake::Invalid = -1ULL;
Snowflake::Snowflake()
: m_num(Invalid) {}

View File

@@ -26,7 +26,7 @@ struct Snowflake {
return m_num;
}
const static uint64_t Invalid = -1ULL; // makes sense to me
const static Snowflake Invalid; // makes sense to me
const static uint64_t SecondsInterval = 4194304000ULL; // the "difference" between two snowflakes one second apart
friend void from_json(const nlohmann::json &j, Snowflake &s);