Skip to content

Commit

Permalink
Merge pull request #159 from Axyte/dev
Browse files Browse the repository at this point in the history
add find_guild_member()
  • Loading branch information
braindigitalis authored Nov 23, 2021
2 parents db5556f + c5f465b commit 8ab0ca5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/dpp/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

namespace dpp {

/** forward declaratiin */
class guild_member;

/**
* @brief A set of cached managed objects
*/
Expand Down Expand Up @@ -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);
};

6 changes: 6 additions & 0 deletions include/dpp/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

};
Expand Down
15 changes: 15 additions & 0 deletions src/dpp/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <variant>
#include <dpp/cache.h>
#include <dpp/guild.h>
#include <dpp/exception.h>

namespace dpp {

Expand Down Expand Up @@ -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!");
}

};

0 comments on commit 8ab0ca5

Please sign in to comment.