diff --git a/include/dpp/permissions.h b/include/dpp/permissions.h index b57af3d9f3..fccb333712 100644 --- a/include/dpp/permissions.h +++ b/include/dpp/permissions.h @@ -127,6 +127,44 @@ class DPP_EXPORT permission { */ operator nlohmann::json() const; + /** + * @brief Check for certain permissions, taking into account administrator privileges. It uses the Bitwise AND operator + * @tparam T one or more uint64_t permission bits + * @param values The permissions (from dpp::permissions) to check for + * + * **Example:** + * + * ```cpp + * bool is_mod = permission.can(dpp::p_kick_members, dpp::p_ban_members); + * // Returns true if it has permission to p_kick_members and p_ban_members + * ``` + * + * @return bool True if it has **all** the given permissions or dpp::p_administrator + */ + template + constexpr bool can(T... values) const noexcept { + return has(values...) || (value & p_administrator); + } + + /** + * @brief Check for certain permissions, taking into account administrator privileges. It uses the Bitwise AND operator + * @tparam T one or more uint64_t permission bits + * @param values The permissions (from dpp::permissions) to check for + * + * **Example:** + * + * ```cpp + * bool is_mod = permission.can_any(dpp::p_kick_members, dpp::p_ban_members); + * // Returns true if it has permission to p_kick_members or p_ban_members + * ``` + * + * @return bool True if it has **any** of the given permissions or dpp::p_administrator + */ + template + constexpr bool can_any(T... values) const noexcept { + return has_any(values...) || (value & p_administrator); + } + /** * @brief Check for permission flags set. It uses the Bitwise AND operator * @tparam T one or more uint64_t permission bits @@ -139,7 +177,7 @@ class DPP_EXPORT permission { * // Returns true if the permission bitmask contains p_kick_members and p_ban_members * ``` * - * @return bool True if it has all the given permissions + * @return bool True if it has **all** the given permissions */ template constexpr bool has(T... values) const noexcept { @@ -158,7 +196,7 @@ class DPP_EXPORT permission { * // Returns true if the permission bitmask contains p_administrator or p_ban_members * ``` * - * @return bool True if it has any the given permissions + * @return bool True if it has **any** of the given permissions */ template constexpr bool has_any(T... values) const noexcept { @@ -187,7 +225,7 @@ class DPP_EXPORT permission { } /** - * @brief Assign a permission. This will reset the bitmask to the new value. + * @brief Assign permissions. This will reset the bitmask to the new value. * @tparam T one or more uint64_t permission bits * @param values The permissions (from dpp::permissions) to set * diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 7b0df49fea..938d4f188c 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -316,6 +316,8 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b p.set(0).add(~uint64_t{0}).remove(dpp::p_speak).set(dpp::p_administrator); success = !p.has(dpp::p_administrator, dpp::p_ban_members) && success; // must return false because they're not both set success = !p.has(dpp::p_administrator | dpp::p_ban_members) && success; + success = p.can(dpp::p_ban_members) && success; + success = p.can(dpp::p_speak) && success; constexpr auto permission_test = [](dpp::permission p) constexpr noexcept { bool success{true}; @@ -328,6 +330,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b success = p.has(dpp::p_administrator | dpp::p_ban_members) && success; success = p.add(dpp::p_speak).has(dpp::p_administrator, dpp::p_speak) && success; success = !p.remove(dpp::p_speak).has(dpp::p_administrator, dpp::p_speak) && success; + p.remove(dpp::p_administrator); + success = p.can(dpp::p_ban_members) && success; + success = !p.can(dpp::p_speak, dpp::p_ban_members) && success; + success = p.can_any(dpp::p_speak, dpp::p_ban_members) && success; return success; }; constexpr auto constexpr_success = permission_test({~uint64_t{0}}); // test in constant evaluated