From c5f465bd1dca0e60d19777d8d6212d7d872bc5b8 Mon Sep 17 00:00:00 2001 From: Axyte <85938894+Axyte@users.noreply.github.com> Date: Wed, 24 Nov 2021 02:12:54 +0530 Subject: [PATCH] [minor:] add find_guild_member() fetch guild_member from cache --- include/dpp/cache.h | 14 ++++++++++++++ include/dpp/exception.h | 6 ++++++ src/dpp/cache.cpp | 15 +++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/include/dpp/cache.h b/include/dpp/cache.h index caa81644db..553350c45b 100644 --- a/include/dpp/cache.h +++ b/include/dpp/cache.h @@ -27,6 +27,9 @@ namespace dpp { + /** forward declaratiin */ + class guild_member; + /** * @brief A set of cached managed objects */ @@ -123,5 +126,16 @@ namespace dpp { cache_decl(role, find_role, get_role_cache, get_role_count); cache_decl(channel, find_channel, get_channel_cache, get_channel_count); cache_decl(emoji, find_emoji, get_emoji_cache, get_emoji_count); + + /** + * @brief Get the guild_member from cache of given IDs + * + * @param guild_id ID of the guild to find guild_member for + * @param user_id ID of the user to find guild_member for + * + * @throw dpp::cache_exception if the guild or guild_member is not found in the cache + * @return guild_member the cached object, if found + */ + guild_member find_guild_member(const snowflake guild_id, const snowflake user_id); }; diff --git a/include/dpp/exception.h b/include/dpp/exception.h index f57046f8dc..8401835cea 100644 --- a/include/dpp/exception.h +++ b/include/dpp/exception.h @@ -175,6 +175,11 @@ class exception : public std::exception * @note This is a stub for documentation purposes. For full information on supported methods please see dpp::exception. */ class parse_exception : public dpp::exception { }; + /** + * @brief Represents invalid access to dpp's cache or its members, which may or may not exist. + * @note This is a stub for documentation purposes. For full information on supported methods please see dpp::exception. + */ + class cache_exception : public dpp::exception { }; #else derived_exception(logic_exception, dpp::exception); derived_exception(file_exception, dpp::exception); @@ -183,6 +188,7 @@ class exception : public std::exception derived_exception(rest_exception, dpp::exception); derived_exception(length_exception, dpp::exception); derived_exception(parse_exception, dpp::exception); + derived_exception(cache_exception, dpp::exception); #endif }; diff --git a/src/dpp/cache.cpp b/src/dpp/cache.cpp index 1afc6a92af..83537fb3eb 100644 --- a/src/dpp/cache.cpp +++ b/src/dpp/cache.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace dpp { @@ -158,4 +159,18 @@ cache_helper(role, role_cache, find_role, get_role_cache, get_role_count); cache_helper(guild, guild_cache, find_guild, get_guild_cache, get_guild_count); cache_helper(emoji, emoji_cache, find_emoji, get_emoji_cache, get_emoji_count); +guild_member find_guild_member(const snowflake guild_id, const snowflake user_id) { + guild* g = find_guild(guild_id); + if (g) { + auto gm = g->members.find(user_id); + if (gm != g->members.end()) { + return gm->second; + } + + throw dpp::cache_exception("Requested member not found in the guild cache!"); + } + + throw dpp::cache_exception("Requested guild cache not found!"); +} + };