Skip to content

Commit

Permalink
feat: namespace for default cache policies
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Sep 27, 2023
1 parent c973b5f commit 741480b
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 25 deletions.
2 changes: 1 addition & 1 deletion include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ class DPP_EXPORT interaction : public managed, public json_interface<interaction
const dpp::message& get_resolved_message(snowflake id) const;

/**
* @brief Get an uploaded attachment associated with the slash command from the resolved list.
* @brief Get an uploaded axttachment associated with the slash command from the resolved list.
* The resolved list contains associated structures for this command and does not
* use the cache or require any extra API calls.
*
Expand Down
78 changes: 75 additions & 3 deletions include/dpp/cluster.h

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,28 @@ struct DPP_EXPORT cache_policy_t {
cache_policy_setting_t guild_policy = cp_aggressive;
};

/**
* @brief Contains a set of predefined cache policies for use when constructing a dpp::cluster
*/
namespace cache_policy {

/**
* @brief A shortcut constant for all caching enabled for use in dpp::cluster constructor
*/
inline constexpr cache_policy_t cpol_default = { cp_aggressive, cp_aggressive, cp_aggressive, cp_aggressive, cp_aggressive };

/**
* @brief A shortcut constant for a more balanced caching policy for use in dpp::cluster constructor
*/
inline constexpr cache_policy_t cpol_balanced = { cp_lazy, cp_lazy, cp_lazy, cp_aggressive, cp_aggressive };

/**
* @brief A shortcut constant for all caching disabled for use in dpp::cluster constructor
*/
inline constexpr cache_policy_t cpol_none = { cp_none, cp_none, cp_none, cp_none, cp_none };

};

/**
* @brief Represents messages sent and received on Discord
*/
Expand Down
5 changes: 3 additions & 2 deletions src/dpp/events/channel_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ using namespace dpp;
*/
void channel_create::handle(discord_client* client, json &j, const std::string &raw) {
json& d = j["d"];
dpp::channel newchannel;
dpp::channel* c = nullptr;
dpp::guild* g = nullptr;

if (client->creator->cache_policy.channel_policy == cp_none) {
newchannel.fill_from_json(client, &d);
c = *newchannel;
newchannel.fill_from_json(&d);
c = &newchannel;
g = dpp::find_guild(c->guild_id);
} else {
c = dpp::find_channel(snowflake_not_null(&d, "id"));
Expand Down
4 changes: 2 additions & 2 deletions src/dpp/events/channel_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ void channel_update::handle(discord_client* client, json &j, const std::string &
channel newchannel;
channel* c = nullptr;
if (client->creator->cache_policy.channel_policy == cp_none) {
newchannel.fill_from_json(client, &d);
c = *newchannel;
newchannel.fill_from_json(&d);
c = &newchannel;
} else {
c = dpp::find_channel(from_string<uint64_t>(d["id"].get<std::string>()));
if (c) {
Expand Down
2 changes: 1 addition & 1 deletion src/dpp/events/guild_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void guild_create::handle(discord_client* client, json &j, const std::string &ra

if (client->creator->cache_policy.guild_policy == cp_none) {
newguild.fill_from_json(client, &d);
g = *newguild;
g = &newguild;
} else {
bool newguild = false;
if (snowflake_not_null(&d, "id") == 0)
Expand Down
15 changes: 8 additions & 7 deletions src/dpp/events/guild_members_chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ using namespace dpp;
void guild_members_chunk::handle(discord_client* client, json &j, const std::string &raw) {
json &d = j["d"];
dpp::guild_member_map um;
dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id"));
dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id");
dpp::guild* g = dpp::find_guild(guild_id);
if (g) {
/* Store guild members */
if (client->creator->cache_policy.user_policy == cp_aggressive) {
Expand All @@ -64,12 +65,12 @@ void guild_members_chunk::handle(discord_client* client, json &j, const std::str
}
}
}
if (!client->creator->on_guild_members_chunk.empty()) {
dpp::guild_members_chunk_t gmc(client, raw);
gmc.adding = g;
gmc.members = &um;
client->creator->on_guild_members_chunk.call(gmc);
}
}
if (!client->creator->on_guild_members_chunk.empty()) {
dpp::guild_members_chunk_t gmc(client, raw);
gmc.adding = g;
gmc.members = &um;
client->creator->on_guild_members_chunk.call(gmc);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/dpp/events/guild_role_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ void guild_role_create::handle(discord_client* client, json &j, const std::strin
client->creator->on_guild_role_create.call(grc);
}
}

}

}};
2 changes: 1 addition & 1 deletion src/dpp/events/guild_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void guild_update::handle(discord_client* client, json &j, const std::string &ra
dpp::guild* g = nullptr;
if (client->creator->cache_policy.guild_policy == cp_none) {
newguild.fill_from_json(client, &d);
g = *newguild;
g = &newguild;
} else {
g = dpp::find_guild(from_string<uint64_t>(d["id"].get<std::string>()));
if (g) {
Expand Down
12 changes: 6 additions & 6 deletions src/dpp/events/thread_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ void thread_delete::handle(discord_client* client, json& j, const std::string& r
if (gt != g->threads.end()) {
g->threads.erase(gt);
}
if (!client->creator->on_thread_delete.empty()) {
dpp::thread_delete_t td(client, raw);
td.deleted = t;
td.deleting_guild = g;
client->creator->on_thread_delete.call(td);
}
}
if (!client->creator->on_thread_delete.empty()) {
dpp::thread_delete_t td(client, raw);
td.deleted = t;
td.deleting_guild = g;
client->creator->on_thread_delete.call(td);
}
}
}};
2 changes: 1 addition & 1 deletion src/dpp/slashcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ slashcommand& slashcommand::add_option(const command_option &o)
return *this;
}

interaction::interaction() : application_id(0), type(0), guild_id(0), channel_id(0), message_id(0), version(0), cache_policy({cp_aggressive, cp_aggressive, cp_aggressive, cp_aggressive, cp_aggressive}) {
interaction::interaction() : application_id(0), type(0), guild_id(0), channel_id(0), message_id(0), version(0), cache_policy(cache_policy::cpol_default) {
}

command_interaction interaction::get_command_interaction() const {
Expand Down

0 comments on commit 741480b

Please sign in to comment.