From c973b5f0290c941b698a9461d20c1b6480e040da Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 00:54:59 +0000 Subject: [PATCH 1/8] feat: allow disabling of guild and channel caches --- include/dpp/cluster.h | 2 +- include/dpp/message.h | 10 ++ src/dpp/discordvoiceclient.cpp | 2 +- src/dpp/events/channel_create.cpp | 45 ++++--- src/dpp/events/channel_delete.cpp | 16 +-- src/dpp/events/channel_update.cpp | 23 ++-- src/dpp/events/guild_create.cpp | 164 ++++++++++++----------- src/dpp/events/guild_delete.cpp | 11 +- src/dpp/events/guild_emojis_update.cpp | 29 ++-- src/dpp/events/guild_member_add.cpp | 62 +++++---- src/dpp/events/guild_member_update.cpp | 23 ++-- src/dpp/events/guild_role_create.cpp | 55 ++++---- src/dpp/events/guild_role_delete.cpp | 49 +++---- src/dpp/events/guild_role_update.cpp | 35 +++-- src/dpp/events/guild_stickers_update.cpp | 21 ++- src/dpp/events/guild_update.cpp | 50 ++++--- src/dpp/events/thread_create.cpp | 12 +- src/dpp/events/thread_list_sync.cpp | 22 +-- src/dpp/events/thread_members_update.cpp | 34 +++-- src/dpp/events/thread_update.cpp | 12 +- src/dpp/slashcommand.cpp | 2 +- 21 files changed, 358 insertions(+), 321 deletions(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 926f91d3ec..f795a3f05b 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -218,7 +218,7 @@ class DPP_EXPORT cluster { * @param request_threads_raw The number of threads to allocate for making HTTP requests to sites outside of Discord. This defaults to 1. You can increase this at runtime via the object returned from get_raw_rest(). * @throw dpp::exception Thrown on windows, if WinSock fails to initialise, or on any other system if a dpp::request_queue fails to construct */ - cluster(const std::string& token, uint32_t intents = i_default_intents, uint32_t shards = 0, uint32_t cluster_id = 0, uint32_t maxclusters = 1, bool compressed = true, cache_policy_t policy = { cp_aggressive, cp_aggressive, cp_aggressive }, uint32_t request_threads = 12, uint32_t request_threads_raw = 1); + cluster(const std::string& token, uint32_t intents = i_default_intents, uint32_t shards = 0, uint32_t cluster_id = 0, uint32_t maxclusters = 1, bool compressed = true, cache_policy_t policy = { .user_policy = cp_aggressive, .emoji_policy = cp_aggressive, .role_policy = cp_aggressive, .channel_policy = cp_aggressive, .guild_policy = cp_aggressive }, uint32_t request_threads = 12, uint32_t request_threads_raw = 1); /** * @brief dpp::cluster is non-copyable diff --git a/include/dpp/message.h b/include/dpp/message.h index 422722fb0c..891cef04f2 100644 --- a/include/dpp/message.h +++ b/include/dpp/message.h @@ -1172,6 +1172,16 @@ struct DPP_EXPORT cache_policy_t { * @brief Caching policy for roles */ cache_policy_setting_t role_policy = cp_aggressive; + + /** + * @brief Caching policy for roles + */ + cache_policy_setting_t channel_policy = cp_aggressive; + + /** + * @brief Caching policy for roles + */ + cache_policy_setting_t guild_policy = cp_aggressive; }; /** diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 3f8dc7201c..4f2d3f1281 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -306,8 +306,8 @@ discord_voice_client::discord_voice_client(dpp::cluster* _cluster, snowflake _ch : websocket_client(_host.substr(0, _host.find(":")), _host.substr(_host.find(":") + 1, _host.length()), "/?v=4", OP_TEXT), runner(nullptr), connect_time(0), - port(0), mixer(std::make_unique()), + port(0), ssrc(0), timescale(1000000), paused(false), diff --git a/src/dpp/events/channel_create.cpp b/src/dpp/events/channel_create.cpp index 902e611a89..1bd38d1c6f 100644 --- a/src/dpp/events/channel_create.cpp +++ b/src/dpp/events/channel_create.cpp @@ -38,28 +38,35 @@ using namespace dpp; */ void channel_create::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; + dpp::channel* c = nullptr; + dpp::guild* g = nullptr; - dpp::channel* c = dpp::find_channel(snowflake_not_null(&d, "id")); - if (!c) { - c = new dpp::channel(); - } - c->fill_from_json(&d); - dpp::get_channel_cache()->store(c); - if (c->recipients.size()) { - for (auto & u : c->recipients) { - client->creator->set_dm_channel(u, c->id); + if (client->creator->cache_policy.channel_policy == cp_none) { + newchannel.fill_from_json(client, &d); + c = *newchannel; + g = dpp::find_guild(c->guild_id); + } else { + c = dpp::find_channel(snowflake_not_null(&d, "id")); + if (!c) { + c = new dpp::channel(); } - } - dpp::guild* g = dpp::find_guild(c->guild_id); - if (g) { - g->channels.push_back(c->id); - - if (!client->creator->on_channel_create.empty()) { - dpp::channel_create_t cc(client, raw); - cc.created = c; - cc.creating_guild = g; - client->creator->on_channel_create.call(cc); + c->fill_from_json(&d); + dpp::get_channel_cache()->store(c); + if (c->recipients.size()) { + for (auto & u : c->recipients) { + client->creator->set_dm_channel(u, c->id); + } } + g = dpp::find_guild(c->guild_id); + if (g) { + g->channels.push_back(c->id); + } + } + if (!client->creator->on_channel_create.empty()) { + dpp::channel_create_t cc(client, raw); + cc.created = c; + cc.creating_guild = g; + client->creator->on_channel_create.call(cc); } } diff --git a/src/dpp/events/channel_delete.cpp b/src/dpp/events/channel_delete.cpp index df666028ca..786b3fcc68 100644 --- a/src/dpp/events/channel_delete.cpp +++ b/src/dpp/events/channel_delete.cpp @@ -40,24 +40,24 @@ using namespace dpp; */ void channel_delete::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; + dpp::guild* g = nullptr; dpp::channel* c = dpp::find_channel(snowflake_not_null(&d, "id")); if (c) { - dpp::guild* g = dpp::find_guild(c->guild_id); + g = dpp::find_guild(c->guild_id); if (g) { auto gc = std::find(g->channels.begin(), g->channels.end(), c->id); if (gc != g->channels.end()) { g->channels.erase(gc); } - - if (!client->creator->on_channel_delete.empty()) { - dpp::channel_delete_t cd(client, raw); - cd.deleted = c; - cd.deleting_guild = g; - client->creator->on_channel_delete.call(cd); - } } dpp::get_channel_cache()->remove(c); } + if (!client->creator->on_channel_delete.empty()) { + dpp::channel_delete_t cd(client, raw); + cd.deleted = c; + cd.deleting_guild = g; + client->creator->on_channel_delete.call(cd); + } } }}; \ No newline at end of file diff --git a/src/dpp/events/channel_update.cpp b/src/dpp/events/channel_update.cpp index 91c27a7346..2e726d8c2a 100644 --- a/src/dpp/events/channel_update.cpp +++ b/src/dpp/events/channel_update.cpp @@ -40,16 +40,23 @@ using namespace dpp; */ void channel_update::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; - dpp::channel* c = dpp::find_channel(from_string(d["id"].get())); - if (c) { - c->fill_from_json(&d); - if (!client->creator->on_channel_update.empty()) { - dpp::channel_update_t cu(client, raw); - cu.updated = c; - cu.updating_guild = dpp::find_guild(c->guild_id); - client->creator->on_channel_update.call(cu); + channel newchannel; + channel* c = nullptr; + if (client->creator->cache_policy.channel_policy == cp_none) { + newchannel.fill_from_json(client, &d); + c = *newchannel; + } else { + c = dpp::find_channel(from_string(d["id"].get())); + if (c) { + c->fill_from_json(&d); } } + if (!client->creator->on_channel_update.empty()) { + dpp::channel_update_t cu(client, raw); + cu.updated = c; + cu.updating_guild = dpp::find_guild(c->guild_id); + client->creator->on_channel_update.call(cu); + } } }}; \ No newline at end of file diff --git a/src/dpp/events/guild_create.cpp b/src/dpp/events/guild_create.cpp index 1b79ed11b9..55158a8f55 100644 --- a/src/dpp/events/guild_create.cpp +++ b/src/dpp/events/guild_create.cpp @@ -42,97 +42,105 @@ using namespace dpp; */ void guild_create::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; - bool newguild = false; - if (snowflake_not_null(&d, "id") == 0) - return; - dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "id")); - if (!g) { - g = new dpp::guild(); - newguild = true; - } - g->fill_from_json(client, &d); - g->shard_id = client->shard_id; - if (!g->is_unavailable() && newguild) { - if (client->creator->cache_policy.role_policy != dpp::cp_none) { - /* Store guild roles */ - g->roles.clear(); - g->roles.reserve(d["roles"].size()); - for (auto & role : d["roles"]) { - dpp::role *r = dpp::find_role(snowflake_not_null(&role, "id")); - if (!r) { - r = new dpp::role(); + dpp::guild newguild; + dpp::guild* g = nullptr; + + if (client->creator->cache_policy.guild_policy == cp_none) { + newguild.fill_from_json(client, &d); + g = *newguild; + } else { + bool newguild = false; + if (snowflake_not_null(&d, "id") == 0) + return; + g = dpp::find_guild(snowflake_not_null(&d, "id")); + if (!g) { + g = new dpp::guild(); + newguild = true; + } + g->fill_from_json(client, &d); + g->shard_id = client->shard_id; + if (!g->is_unavailable() && newguild) { + if (client->creator->cache_policy.role_policy != dpp::cp_none) { + /* Store guild roles */ + g->roles.clear(); + g->roles.reserve(d["roles"].size()); + for (auto & role : d["roles"]) { + dpp::role *r = dpp::find_role(snowflake_not_null(&role, "id")); + if (!r) { + r = new dpp::role(); + } + r->fill_from_json(g->id, &role); + dpp::get_role_cache()->store(r); + g->roles.push_back(r->id); } - r->fill_from_json(g->id, &role); - dpp::get_role_cache()->store(r); - g->roles.push_back(r->id); } - } - /* Store guild channels */ - g->channels.clear(); - g->channels.reserve(d["channels"].size()); - for (auto & channel : d["channels"]) { - dpp::channel* c = dpp::find_channel(snowflake_not_null(&channel, "id")); - if (!c) { - c = new dpp::channel(); + /* Store guild channels */ + g->channels.clear(); + g->channels.reserve(d["channels"].size()); + for (auto & channel : d["channels"]) { + dpp::channel* c = dpp::find_channel(snowflake_not_null(&channel, "id")); + if (!c) { + c = new dpp::channel(); + } + c->fill_from_json(&channel); + c->guild_id = g->id; + dpp::get_channel_cache()->store(c); + g->channels.push_back(c->id); } - c->fill_from_json(&channel); - c->guild_id = g->id; - dpp::get_channel_cache()->store(c); - g->channels.push_back(c->id); - } - /* Store guild threads */ - g->threads.clear(); - g->threads.reserve(d["threads"].size()); - for (auto & channel : d["threads"]) { - g->threads.push_back(snowflake_not_null(&channel, "id")); - } + /* Store guild threads */ + g->threads.clear(); + g->threads.reserve(d["threads"].size()); + for (auto & channel : d["threads"]) { + g->threads.push_back(snowflake_not_null(&channel, "id")); + } - /* Store guild members */ - if (client->creator->cache_policy.user_policy == cp_aggressive) { - g->members.reserve(d["members"].size()); - for (auto & user : d["members"]) { - snowflake userid = snowflake_not_null(&(user["user"]), "id"); - /* Only store ones we don't have already otherwise gm will leak */ - if (g->members.find(userid) == g->members.end()) { - dpp::user* u = dpp::find_user(userid); - if (!u) { - u = new dpp::user(); - u->fill_from_json(&(user["user"])); - dpp::get_user_cache()->store(u); - } else { - u->refcount++; + /* Store guild members */ + if (client->creator->cache_policy.user_policy == cp_aggressive) { + g->members.reserve(d["members"].size()); + for (auto & user : d["members"]) { + snowflake userid = snowflake_not_null(&(user["user"]), "id"); + /* Only store ones we don't have already otherwise gm will leak */ + if (g->members.find(userid) == g->members.end()) { + dpp::user* u = dpp::find_user(userid); + if (!u) { + u = new dpp::user(); + u->fill_from_json(&(user["user"])); + dpp::get_user_cache()->store(u); + } else { + u->refcount++; + } + dpp::guild_member gm; + gm.fill_from_json(&user, g->id, userid); + g->members[userid] = gm; } - dpp::guild_member gm; - gm.fill_from_json(&user, g->id, userid); - g->members[userid] = gm; } } - } - if (client->creator->cache_policy.emoji_policy != dpp::cp_none) { - /* Store emojis */ - g->emojis.reserve(d["emojis"].size()); - g->emojis = {}; - for (auto & emoji : d["emojis"]) { - dpp::emoji* e = dpp::find_emoji(snowflake_not_null(&emoji, "id")); - if (!e) { - e = new dpp::emoji(); - e->fill_from_json(&emoji); - dpp::get_emoji_cache()->store(e); + if (client->creator->cache_policy.emoji_policy != dpp::cp_none) { + /* Store emojis */ + g->emojis.reserve(d["emojis"].size()); + g->emojis = {}; + for (auto & emoji : d["emojis"]) { + dpp::emoji* e = dpp::find_emoji(snowflake_not_null(&emoji, "id")); + if (!e) { + e = new dpp::emoji(); + e->fill_from_json(&emoji); + dpp::get_emoji_cache()->store(e); + } + g->emojis.push_back(e->id); } - g->emojis.push_back(e->id); } } - } - dpp::get_guild_cache()->store(g); - if (newguild && g->id && (client->intents & dpp::i_guild_members)) { - if (client->creator->cache_policy.user_policy == cp_aggressive) { - json chunk_req = json({{"op", 8}, {"d", {{"guild_id",std::to_string(g->id)},{"query",""},{"limit",0}}}}); - if (client->intents & dpp::i_guild_presences) { - chunk_req["d"]["presences"] = true; + dpp::get_guild_cache()->store(g); + if (newguild && g->id && (client->intents & dpp::i_guild_members)) { + if (client->creator->cache_policy.user_policy == cp_aggressive) { + json chunk_req = json({{"op", 8}, {"d", {{"guild_id",std::to_string(g->id)},{"query",""},{"limit",0}}}}); + if (client->intents & dpp::i_guild_presences) { + chunk_req["d"]["presences"] = true; + } + client->queue_message(client->jsonobj_to_string(chunk_req)); } - client->queue_message(client->jsonobj_to_string(chunk_req)); } } diff --git a/src/dpp/events/guild_delete.cpp b/src/dpp/events/guild_delete.cpp index 935e6baf05..a68f9f15e9 100644 --- a/src/dpp/events/guild_delete.cpp +++ b/src/dpp/events/guild_delete.cpp @@ -84,11 +84,12 @@ void guild_delete::handle(discord_client* client, json &j, const std::string &ra g->flags |= dpp::g_unavailable; } - if (!client->creator->on_guild_delete.empty()) { - dpp::guild_delete_t gd(client, raw); - gd.deleted = g; - client->creator->on_guild_delete.call(gd); - } + } + + if (!client->creator->on_guild_delete.empty()) { + dpp::guild_delete_t gd(client, raw); + gd.deleted = g; + client->creator->on_guild_delete.call(gd); } } diff --git a/src/dpp/events/guild_emojis_update.cpp b/src/dpp/events/guild_emojis_update.cpp index 5d247a5e73..52c8354577 100644 --- a/src/dpp/events/guild_emojis_update.cpp +++ b/src/dpp/events/guild_emojis_update.cpp @@ -43,15 +43,16 @@ using namespace dpp; */ void guild_emojis_update::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; - dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); - if (g) { - if (client->creator->cache_policy.emoji_policy != dpp::cp_none) { - for (auto & ee : g->emojis) { - dpp::emoji* fe = dpp::find_emoji(ee); - if (fe) { - dpp::get_emoji_cache()->remove(fe); - } + dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); + dpp::guild* g = dpp::find_guild(guild_id); + if (client->creator->cache_policy.emoji_policy != dpp::cp_none) { + for (auto & ee : g->emojis) { + dpp::emoji* fe = dpp::find_emoji(ee); + if (fe) { + dpp::get_emoji_cache()->remove(fe); } + } + if (g) { g->emojis.clear(); for (auto & emoji : d["emojis"]) { dpp::emoji* e = dpp::find_emoji(snowflake_not_null(&emoji, "id")); @@ -63,12 +64,12 @@ void guild_emojis_update::handle(discord_client* client, json &j, const std::str g->emojis.push_back(e->id); } } - if (!client->creator->on_guild_emojis_update.empty()) { - dpp::guild_emojis_update_t geu(client, raw); - geu.emojis = g->emojis; - geu.updating_guild = g; - client->creator->on_guild_emojis_update.call(geu); - } + } + if (!client->creator->on_guild_emojis_update.empty()) { + dpp::guild_emojis_update_t geu(client, raw); + geu.emojis = g->emojis; + geu.updating_guild = g; + client->creator->on_guild_emojis_update.call(geu); } } diff --git a/src/dpp/events/guild_member_add.cpp b/src/dpp/events/guild_member_add.cpp index db4afe2c33..3c1f50f9a5 100644 --- a/src/dpp/events/guild_member_add.cpp +++ b/src/dpp/events/guild_member_add.cpp @@ -40,40 +40,38 @@ using namespace dpp; */ void guild_member_add::handle(discord_client* client, json &j, const std::string &raw) { json d = j["d"]; - - 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); dpp::guild_member_add_t gmr(client, raw); - if (g) { - if (client->creator->cache_policy.user_policy == dpp::cp_none) { - dpp::guild_member gm; - gm.fill_from_json(&d, g->id, snowflake_not_null(&(d["user"]), "id")); - gmr.added = gm; - if (!client->creator->on_guild_member_add.empty()) { - gmr.adding_guild = g; - client->creator->on_guild_member_add.call(gmr); - } + if (client->creator->cache_policy.user_policy == dpp::cp_none) { + dpp::guild_member gm; + gm.fill_from_json(&d, guild_id, snowflake_not_null(&(d["user"]), "id")); + gmr.added = gm; + if (!client->creator->on_guild_member_add.empty()) { + gmr.adding_guild = g; + client->creator->on_guild_member_add.call(gmr); + } + } else { + dpp::user* u = dpp::find_user(snowflake_not_null(&(d["user"]), "id")); + if (!u) { + u = new dpp::user(); + u->fill_from_json(&(d["user"])); + dpp::get_user_cache()->store(u); } else { - dpp::user* u = dpp::find_user(snowflake_not_null(&(d["user"]), "id")); - if (!u) { - u = new dpp::user(); - u->fill_from_json(&(d["user"])); - dpp::get_user_cache()->store(u); - } else { - u->refcount++; - } - dpp::guild_member gm; - gmr.added = {}; - if (u && u->id && g->members.find(u->id) == g->members.end()) { - gm.fill_from_json(&d, g->id, u->id); - g->members[u->id] = gm; - gmr.added = gm; - } else if (u && u->id) { - gmr.added = g->members.find(u->id)->second; - } - if (!client->creator->on_guild_member_add.empty()) { - gmr.adding_guild = g; - client->creator->on_guild_member_add.call(gmr); - } + u->refcount++; + } + dpp::guild_member gm; + gmr.added = {}; + if (g && u && u->id && g->members.find(u->id) == g->members.end()) { + gm.fill_from_json(&d, g->id, u->id); + g->members[u->id] = gm; + gmr.added = gm; + } else if (g && u && u->id) { + gmr.added = g->members.find(u->id)->second; + } + if (!client->creator->on_guild_member_add.empty()) { + gmr.adding_guild = g; + client->creator->on_guild_member_add.call(gmr); } } } diff --git a/src/dpp/events/guild_member_update.cpp b/src/dpp/events/guild_member_update.cpp index dbcdb829f0..c265d08d69 100644 --- a/src/dpp/events/guild_member_update.cpp +++ b/src/dpp/events/guild_member_update.cpp @@ -40,26 +40,29 @@ using namespace dpp; */ void guild_member_update::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; - dpp::guild* g = dpp::find_guild(from_string(d["guild_id"].get())); + dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); + dpp::guild* g = dpp::find_guild(guild_id); if (client->creator->cache_policy.user_policy == dpp::cp_none) { dpp::user u; u.fill_from_json(&(d["user"])); - if (g && !client->creator->on_guild_member_update.empty()) { - dpp::guild_member_update_t gmu(client, raw); - gmu.updating_guild = g; + dpp::guild_member_update_t gmu(client, raw); + gmu.updating_guild = g; + if (!client->creator->on_guild_member_update.empty()) { guild_member m; - auto& user = d;//d["user"]; // d contains roles and other member stuff already - m.fill_from_json(&user, g->id, u.id); + auto& user = d; // d contains roles and other member stuff already + m.fill_from_json(&user, guild_id, u.id); gmu.updated = m; - client->creator->on_guild_member_update.call(gmu); } + client->creator->on_guild_member_update.call(gmu); } else { dpp::user* u = dpp::find_user(from_string(d["user"]["id"].get())); - if (g && u) { + if (u) { auto& user = d;//d["user"]; // d contains roles and other member stuff already guild_member m; - m.fill_from_json(&user, g->id, u->id); - g->members[u->id] = m; + m.fill_from_json(&user, guild_id, u->id); + if (g) { + g->members[u->id] = m; + } if (!client->creator->on_guild_member_update.empty()) { dpp::guild_member_update_t gmu(client, raw); diff --git a/src/dpp/events/guild_role_create.cpp b/src/dpp/events/guild_role_create.cpp index 4ff8693f6b..0f46a66f50 100644 --- a/src/dpp/events/guild_role_create.cpp +++ b/src/dpp/events/guild_role_create.cpp @@ -41,35 +41,36 @@ using namespace dpp; */ void guild_role_create::handle(discord_client* client, json &j, const std::string &raw) { json &d = j["d"]; - dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); - if (g) { - if (client->creator->cache_policy.role_policy == dpp::cp_none) { - json &role = d["role"]; - dpp::role r; - r.fill_from_json(g->id, &role); - if (!client->creator->on_guild_role_create.empty()) { - dpp::guild_role_create_t grc(client, raw); - grc.creating_guild = g; - grc.created = &r; - client->creator->on_guild_role_create.call(grc); - } - } else { - json &role = d["role"]; - dpp::role *r = dpp::find_role(snowflake_not_null(&role, "id")); - if (!r) { - r = new dpp::role(); - } - r->fill_from_json(g->id, &role); - dpp::get_role_cache()->store(r); + dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); + dpp::guild* g = dpp::find_guild(guild_id); + if (client->creator->cache_policy.role_policy == dpp::cp_none) { + json &role = d["role"]; + dpp::role r; + r.fill_from_json(guild_id, &role); + if (!client->creator->on_guild_role_create.empty()) { + dpp::guild_role_create_t grc(client, raw); + grc.creating_guild = g; + grc.created = &r; + client->creator->on_guild_role_create.call(grc); + } + } else { + json &role = d["role"]; + dpp::role *r = dpp::find_role(snowflake_not_null(&role, "id")); + if (!r) { + r = new dpp::role(); + } + r->fill_from_json(guild_id, &role); + dpp::get_role_cache()->store(r); + if (g) { g->roles.push_back(r->id); - if (!client->creator->on_guild_role_create.empty()) { - dpp::guild_role_create_t grc(client, raw); - grc.creating_guild = g; - grc.created = r; - client->creator->on_guild_role_create.call(grc); - } + } + if (!client->creator->on_guild_role_create.empty()) { + dpp::guild_role_create_t grc(client, raw); + grc.creating_guild = g; + grc.created = r; + client->creator->on_guild_role_create.call(grc); } } -} + }}; \ No newline at end of file diff --git a/src/dpp/events/guild_role_delete.cpp b/src/dpp/events/guild_role_delete.cpp index b4d8eeeef5..abdb694e7f 100644 --- a/src/dpp/events/guild_role_delete.cpp +++ b/src/dpp/events/guild_role_delete.cpp @@ -39,35 +39,36 @@ using json = nlohmann::json; */ void guild_role_delete::handle(discord_client* client, json &j, const std::string &raw) { json &d = j["d"]; - dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); - if (g) { - if (client->creator->cache_policy.role_policy == dpp::cp_none) { - dpp::role r; - r.fill_from_json(g->id, &d); - if (!client->creator->on_guild_role_delete.empty()) { - dpp::guild_role_delete_t grd(client, raw); - grd.deleting_guild = g; - grd.deleted = &r; - client->creator->on_guild_role_delete.call(grd); - } - } else { - json& role = d["role"]; - dpp::snowflake id = snowflake_not_null(&role, "id"); - dpp::role *r = dpp::find_role(id); - if (!client->creator->on_guild_role_delete.empty()) { - dpp::guild_role_delete_t grd(client, raw); - grd.deleting_guild = g; - grd.deleted = r ? r : nullptr; - grd.role_id = id; - client->creator->on_guild_role_delete.call(grd); - } - if (r) { + dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); + dpp::guild* g = dpp::find_guild(guild_id); + if (client->creator->cache_policy.role_policy == dpp::cp_none) { + dpp::role r; + r.fill_from_json(guild_id, &d); + if (!client->creator->on_guild_role_delete.empty()) { + dpp::guild_role_delete_t grd(client, raw); + grd.deleting_guild = g; + grd.deleted = &r; + client->creator->on_guild_role_delete.call(grd); + } + } else { + json& role = d["role"]; + dpp::snowflake id = snowflake_not_null(&role, "id"); + dpp::role *r = dpp::find_role(id); + if (!client->creator->on_guild_role_delete.empty()) { + dpp::guild_role_delete_t grd(client, raw); + grd.deleting_guild = g; + grd.deleted = r ? r : nullptr; + grd.role_id = id; + client->creator->on_guild_role_delete.call(grd); + } + if (r) { + if (g) { auto i = std::find(g->roles.begin(), g->roles.end(), r->id); if (i != g->roles.end()) { g->roles.erase(i); } - dpp::get_role_cache()->remove(r); } + dpp::get_role_cache()->remove(r); } } } diff --git a/src/dpp/events/guild_role_update.cpp b/src/dpp/events/guild_role_update.cpp index 0780c79e02..5a85bff455 100644 --- a/src/dpp/events/guild_role_update.cpp +++ b/src/dpp/events/guild_role_update.cpp @@ -42,29 +42,28 @@ using namespace dpp; */ void guild_role_update::handle(discord_client* client, json &j, const std::string &raw) { json &d = j["d"]; - dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); - if (g) { - if (client->creator->cache_policy.role_policy == dpp::cp_none) { - dpp::role r; - r.fill_from_json(g->id, &d); + dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); + dpp::guild* g = dpp::find_guild(guild_id); + if (client->creator->cache_policy.role_policy == dpp::cp_none) { + dpp::role r; + r.fill_from_json(guild_id, &d); + if (!client->creator->on_guild_role_update.empty()) { + dpp::guild_role_update_t gru(client, raw); + gru.updating_guild = g; + gru.updated = &r; + client->creator->on_guild_role_update.call(gru); + } + } else { + json& role = d["role"]; + dpp::role *r = dpp::find_role(snowflake_not_null(&role, "id")); + if (r) { + r->fill_from_json(g->id, &role); if (!client->creator->on_guild_role_update.empty()) { dpp::guild_role_update_t gru(client, raw); gru.updating_guild = g; - gru.updated = &r; + gru.updated = r; client->creator->on_guild_role_update.call(gru); } - } else { - json& role = d["role"]; - dpp::role *r = dpp::find_role(snowflake_not_null(&role, "id")); - if (r) { - r->fill_from_json(g->id, &role); - if (!client->creator->on_guild_role_update.empty()) { - dpp::guild_role_update_t gru(client, raw); - gru.updating_guild = g; - gru.updated = r; - client->creator->on_guild_role_update.call(gru); - } - } } } } diff --git a/src/dpp/events/guild_stickers_update.cpp b/src/dpp/events/guild_stickers_update.cpp index 993a98325f..f483e63e29 100644 --- a/src/dpp/events/guild_stickers_update.cpp +++ b/src/dpp/events/guild_stickers_update.cpp @@ -41,18 +41,17 @@ using namespace dpp; */ void guild_stickers_update::handle(discord_client* client, json &j, const std::string &raw) { json& d = j["d"]; - dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); - if (g) { - if (!client->creator->on_guild_stickers_update.empty()) { - dpp::guild_stickers_update_t gsu(client, raw); - for (auto & sticker : d["stickers"]) { - dpp::sticker s; - s.fill_from_json(&sticker); - gsu.stickers.emplace_back(s); - } - gsu.updating_guild = g; - client->creator->on_guild_stickers_update.call(gsu); + if (!client->creator->on_guild_stickers_update.empty()) { + dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); + dpp::guild* g = dpp::find_guild(guild_id); + dpp::guild_stickers_update_t gsu(client, raw); + for (auto & sticker : d["stickers"]) { + dpp::sticker s; + s.fill_from_json(&sticker); + gsu.stickers.emplace_back(s); } + gsu.updating_guild = g; + client->creator->on_guild_stickers_update.call(gsu); } } diff --git a/src/dpp/events/guild_update.cpp b/src/dpp/events/guild_update.cpp index 36bdb4b084..c0d6ea4b82 100644 --- a/src/dpp/events/guild_update.cpp +++ b/src/dpp/events/guild_update.cpp @@ -39,31 +39,37 @@ using namespace dpp; * @param raw Raw JSON string */ void guild_update::handle(discord_client* client, json &j, const std::string &raw) { - json& d = j["d"]; - dpp::guild* g = dpp::find_guild(from_string(d["id"].get())); - if (g) { - g->fill_from_json(client, &d); - if (!g->is_unavailable()) { - if (client->creator->cache_policy.role_policy != dpp::cp_none && d.find("roles") != d.end()) { - for (size_t rc = 0; rc < g->roles.size(); ++rc) { - dpp::role* oldrole = dpp::find_role(g->roles[rc]); - dpp::get_role_cache()->remove(oldrole); - } - g->roles.clear(); - for (auto & role : d["roles"]) { - dpp::role *r = new dpp::role(); - r->fill_from_json(g->id, &role); - dpp::get_role_cache()->store(r); - g->roles.push_back(r->id); + json& d = j["d"]; + guild newguild; + dpp::guild* g = nullptr; + if (client->creator->cache_policy.guild_policy == cp_none) { + newguild.fill_from_json(client, &d); + g = *newguild; + } else { + g = dpp::find_guild(from_string(d["id"].get())); + if (g) { + g->fill_from_json(client, &d); + if (!g->is_unavailable()) { + if (client->creator->cache_policy.role_policy != dpp::cp_none && d.find("roles") != d.end()) { + for (size_t rc = 0; rc < g->roles.size(); ++rc) { + dpp::role* oldrole = dpp::find_role(g->roles[rc]); + dpp::get_role_cache()->remove(oldrole); + } + g->roles.clear(); + for (auto & role : d["roles"]) { + dpp::role *r = new dpp::role(); + r->fill_from_json(g->id, &role); + dpp::get_role_cache()->store(r); + g->roles.push_back(r->id); + } } } } - - if (!client->creator->on_guild_update.empty()) { - dpp::guild_update_t gu(client, raw); - gu.updated = g; - client->creator->on_guild_update.call(gu); - } + } + if (!client->creator->on_guild_update.empty()) { + dpp::guild_update_t gu(client, raw); + gu.updated = g; + client->creator->on_guild_update.call(gu); } } diff --git a/src/dpp/events/thread_create.cpp b/src/dpp/events/thread_create.cpp index 316834f2ab..cf94707e13 100644 --- a/src/dpp/events/thread_create.cpp +++ b/src/dpp/events/thread_create.cpp @@ -39,12 +39,12 @@ void thread_create::handle(discord_client* client, json& j, const std::string& r dpp::guild* g = dpp::find_guild(t.guild_id); if (g) { g->threads.push_back(t.id); - if (!client->creator->on_thread_create.empty()) { - dpp::thread_create_t tc(client, raw); - tc.created = t; - tc.creating_guild = g; - client->creator->on_thread_create.call(tc); - } + } + if (!client->creator->on_thread_create.empty()) { + dpp::thread_create_t tc(client, raw); + tc.created = t; + tc.creating_guild = g; + client->creator->on_thread_create.call(tc); } } }}; diff --git a/src/dpp/events/thread_list_sync.cpp b/src/dpp/events/thread_list_sync.cpp index 3d25a27fee..f91feb1eb8 100644 --- a/src/dpp/events/thread_list_sync.cpp +++ b/src/dpp/events/thread_list_sync.cpp @@ -43,20 +43,20 @@ void thread_list_sync::handle(discord_client* client, json& j, const std::string g->threads.push_back(snowflake_not_null(&t, "id")); } } - if (!client->creator->on_thread_list_sync.empty()) { - dpp::thread_list_sync_t tls(client, raw); - if (d.find("threads") != d.end()) { - for (auto& t : d["threads"]) { - tls.threads.push_back(thread().fill_from_json(&t)); - } + } + if (!client->creator->on_thread_list_sync.empty()) { + dpp::thread_list_sync_t tls(client, raw); + if (d.find("threads") != d.end()) { + for (auto& t : d["threads"]) { + tls.threads.push_back(thread().fill_from_json(&t)); } - if (d.find("members") != d.end()) { - for (auto& tm : d["members"]) { - tls.members.push_back(thread_member().fill_from_json(&tm)); - } + } + if (d.find("members") != d.end()) { + for (auto& tm : d["members"]) { + tls.members.push_back(thread_member().fill_from_json(&tm)); } - client->creator->on_thread_list_sync.call(tls); } + client->creator->on_thread_list_sync.call(tls); } } }}; diff --git a/src/dpp/events/thread_members_update.cpp b/src/dpp/events/thread_members_update.cpp index 4f119df017..5e78c7f43e 100644 --- a/src/dpp/events/thread_members_update.cpp +++ b/src/dpp/events/thread_members_update.cpp @@ -35,28 +35,26 @@ void thread_members_update::handle(discord_client* client, json& j, const std::s json& d = j["d"]; dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); - if (g) { - if (!client->creator->on_thread_members_update.empty()) { - dpp::thread_members_update_t tms(client, raw); - tms.updating_guild = g; - set_snowflake_not_null(&d, "id", tms.thread_id); - set_int8_not_null(&d, "member_count", tms.member_count); - if (d.find("added_members") != d.end()) { - for (auto& tm : d["added_members"]) { - tms.added.emplace_back(thread_member().fill_from_json(&tm)); - } + if (!client->creator->on_thread_members_update.empty()) { + dpp::thread_members_update_t tms(client, raw); + tms.updating_guild = g; + set_snowflake_not_null(&d, "id", tms.thread_id); + set_int8_not_null(&d, "member_count", tms.member_count); + if (d.find("added_members") != d.end()) { + for (auto& tm : d["added_members"]) { + tms.added.emplace_back(thread_member().fill_from_json(&tm)); } - if (d.find("removed_member_ids") != d.end()) { - try { - for (auto& rm : d["removed_member_ids"]) { - tms.removed_ids.push_back(std::stoull(static_cast(rm))); - } - } catch (const std::exception& e) { - client->creator->log(dpp::ll_error, std::string("thread_members_update: {}") + e.what()); + } + if (d.find("removed_member_ids") != d.end()) { + try { + for (auto& rm : d["removed_member_ids"]) { + tms.removed_ids.push_back(std::stoull(static_cast(rm))); } + } catch (const std::exception& e) { + client->creator->log(dpp::ll_error, std::string("thread_members_update: {}") + e.what()); } - client->creator->on_thread_members_update.call(tms); } + client->creator->on_thread_members_update.call(tms); } } }}; diff --git a/src/dpp/events/thread_update.cpp b/src/dpp/events/thread_update.cpp index 7055e69ff0..6e86f29cb3 100644 --- a/src/dpp/events/thread_update.cpp +++ b/src/dpp/events/thread_update.cpp @@ -38,13 +38,11 @@ void thread_update::handle(discord_client* client, json& j, const std::string& r dpp::thread t; t.fill_from_json(&d); dpp::guild* g = dpp::find_guild(t.guild_id); - if (g) { - if (!client->creator->on_thread_update.empty()) { - dpp::thread_update_t tu(client, raw); - tu.updated = t; - tu.updating_guild = g; - client->creator->on_thread_update.call(tu); - } + if (!client->creator->on_thread_update.empty()) { + dpp::thread_update_t tu(client, raw); + tu.updated = t; + tu.updating_guild = g; + client->creator->on_thread_update.call(tu); } } }}; diff --git a/src/dpp/slashcommand.cpp b/src/dpp/slashcommand.cpp index e82296a1d3..40dfe9928c 100644 --- a/src/dpp/slashcommand.cpp +++ b/src/dpp/slashcommand.cpp @@ -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}) { +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}) { } command_interaction interaction::get_command_interaction() const { From 741480be014b9c22e80529f3641aca4239bd9e52 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 01:20:44 +0000 Subject: [PATCH 2/8] feat: namespace for default cache policies --- include/dpp/appcommand.h | 2 +- include/dpp/cluster.h | 78 +++++++++++++++++++++++++- include/dpp/message.h | 22 ++++++++ src/dpp/events/channel_create.cpp | 5 +- src/dpp/events/channel_update.cpp | 4 +- src/dpp/events/guild_create.cpp | 2 +- src/dpp/events/guild_members_chunk.cpp | 15 ++--- src/dpp/events/guild_role_create.cpp | 2 +- src/dpp/events/guild_update.cpp | 2 +- src/dpp/events/thread_delete.cpp | 12 ++-- src/dpp/slashcommand.cpp | 2 +- 11 files changed, 121 insertions(+), 25 deletions(-) diff --git a/include/dpp/appcommand.h b/include/dpp/appcommand.h index 7cae5d28ad..8f4afd2ff0 100644 --- a/include/dpp/appcommand.h +++ b/include/dpp/appcommand.h @@ -822,7 +822,7 @@ class DPP_EXPORT interaction : public managed, public json_interfacecreator->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")); diff --git a/src/dpp/events/channel_update.cpp b/src/dpp/events/channel_update.cpp index 2e726d8c2a..198749cfdc 100644 --- a/src/dpp/events/channel_update.cpp +++ b/src/dpp/events/channel_update.cpp @@ -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(d["id"].get())); if (c) { diff --git a/src/dpp/events/guild_create.cpp b/src/dpp/events/guild_create.cpp index 55158a8f55..2470863db4 100644 --- a/src/dpp/events/guild_create.cpp +++ b/src/dpp/events/guild_create.cpp @@ -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) diff --git a/src/dpp/events/guild_members_chunk.cpp b/src/dpp/events/guild_members_chunk.cpp index bcbfeba83a..44e64c32ee 100644 --- a/src/dpp/events/guild_members_chunk.cpp +++ b/src/dpp/events/guild_members_chunk.cpp @@ -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) { @@ -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); } } diff --git a/src/dpp/events/guild_role_create.cpp b/src/dpp/events/guild_role_create.cpp index 0f46a66f50..d3bc02b2c2 100644 --- a/src/dpp/events/guild_role_create.cpp +++ b/src/dpp/events/guild_role_create.cpp @@ -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); } } - +} }}; \ No newline at end of file diff --git a/src/dpp/events/guild_update.cpp b/src/dpp/events/guild_update.cpp index c0d6ea4b82..751dd26f05 100644 --- a/src/dpp/events/guild_update.cpp +++ b/src/dpp/events/guild_update.cpp @@ -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(d["id"].get())); if (g) { diff --git a/src/dpp/events/thread_delete.cpp b/src/dpp/events/thread_delete.cpp index c7a7880407..0d86dbdc21 100644 --- a/src/dpp/events/thread_delete.cpp +++ b/src/dpp/events/thread_delete.cpp @@ -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); } } }}; diff --git a/src/dpp/slashcommand.cpp b/src/dpp/slashcommand.cpp index 40dfe9928c..923cf633fa 100644 --- a/src/dpp/slashcommand.cpp +++ b/src/dpp/slashcommand.cpp @@ -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 { From aa6b976666c575d1791435849d69817171c3828e Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 01:28:49 +0000 Subject: [PATCH 3/8] docs: fix spelling --- include/dpp/appcommand.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dpp/appcommand.h b/include/dpp/appcommand.h index 8f4afd2ff0..7cae5d28ad 100644 --- a/include/dpp/appcommand.h +++ b/include/dpp/appcommand.h @@ -822,7 +822,7 @@ class DPP_EXPORT interaction : public managed, public json_interface Date: Wed, 27 Sep 2023 10:08:47 +0000 Subject: [PATCH 4/8] fix: null pointer deref issue spotted by codacy --- src/dpp/events/guild_emojis_update.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dpp/events/guild_emojis_update.cpp b/src/dpp/events/guild_emojis_update.cpp index 52c8354577..e8e171a1cc 100644 --- a/src/dpp/events/guild_emojis_update.cpp +++ b/src/dpp/events/guild_emojis_update.cpp @@ -67,7 +67,7 @@ void guild_emojis_update::handle(discord_client* client, json &j, const std::str } if (!client->creator->on_guild_emojis_update.empty()) { dpp::guild_emojis_update_t geu(client, raw); - geu.emojis = g->emojis; + geu.emojis = g ? g->emojis : nullptr; geu.updating_guild = g; client->creator->on_guild_emojis_update.call(geu); } From b0b4bf6e709e15458976f8c38ea2a308300de8d8 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 14:28:59 +0000 Subject: [PATCH 5/8] fix: shadowing and null deref --- src/dpp/events/guild_create.cpp | 19 ++++++++----- src/dpp/events/guild_emojis_update.cpp | 37 +++++++++++++++----------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/dpp/events/guild_create.cpp b/src/dpp/events/guild_create.cpp index 2470863db4..8fea1392aa 100644 --- a/src/dpp/events/guild_create.cpp +++ b/src/dpp/events/guild_create.cpp @@ -44,22 +44,27 @@ void guild_create::handle(discord_client* client, json &j, const std::string &ra json& d = j["d"]; dpp::guild newguild; dpp::guild* g = nullptr; - + + if (snowflake_not_null(&d, "id") == 0) { + /* This shouldnt ever happen, but it has been seen in the wild i guess? + * Either way a guild with invalid or missing ID doesnt want to cause events. + */ + return; + } + if (client->creator->cache_policy.guild_policy == cp_none) { newguild.fill_from_json(client, &d); g = &newguild; } else { - bool newguild = false; - if (snowflake_not_null(&d, "id") == 0) - return; + bool is_new_guild = false; g = dpp::find_guild(snowflake_not_null(&d, "id")); if (!g) { g = new dpp::guild(); - newguild = true; + is_new_guild = true; } g->fill_from_json(client, &d); g->shard_id = client->shard_id; - if (!g->is_unavailable() && newguild) { + if (!g->is_unavailable() && is_new_guild) { if (client->creator->cache_policy.role_policy != dpp::cp_none) { /* Store guild roles */ g->roles.clear(); @@ -133,7 +138,7 @@ void guild_create::handle(discord_client* client, json &j, const std::string &ra } } dpp::get_guild_cache()->store(g); - if (newguild && g->id && (client->intents & dpp::i_guild_members)) { + if (is_new_guild && g->id && (client->intents & dpp::i_guild_members)) { if (client->creator->cache_policy.user_policy == cp_aggressive) { json chunk_req = json({{"op", 8}, {"d", {{"guild_id",std::to_string(g->id)},{"query",""},{"limit",0}}}}); if (client->intents & dpp::i_guild_presences) { diff --git a/src/dpp/events/guild_emojis_update.cpp b/src/dpp/events/guild_emojis_update.cpp index e8e171a1cc..8dbf12c9e8 100644 --- a/src/dpp/events/guild_emojis_update.cpp +++ b/src/dpp/events/guild_emojis_update.cpp @@ -45,29 +45,36 @@ void guild_emojis_update::handle(discord_client* client, json &j, const std::str json& d = j["d"]; dpp::snowflake guild_id = snowflake_not_null(&d, "guild_id"); dpp::guild* g = dpp::find_guild(guild_id); + std::vector emojis; if (client->creator->cache_policy.emoji_policy != dpp::cp_none) { - for (auto & ee : g->emojis) { - dpp::emoji* fe = dpp::find_emoji(ee); - if (fe) { - dpp::get_emoji_cache()->remove(fe); - } - } if (g) { - g->emojis.clear(); - for (auto & emoji : d["emojis"]) { - dpp::emoji* e = dpp::find_emoji(snowflake_not_null(&emoji, "id")); - if (!e) { - e = new dpp::emoji(); - e->fill_from_json(&emoji); - dpp::get_emoji_cache()->store(e); + for (auto & ee : g->emojis) { + dpp::emoji* fe = dpp::find_emoji(ee); + if (fe) { + dpp::get_emoji_cache()->remove(fe); } - g->emojis.push_back(e->id); } } + for (auto & emoji : d["emojis"]) { + dpp::emoji* e = dpp::find_emoji(snowflake_not_null(&emoji, "id")); + if (!e) { + e = new dpp::emoji(); + e->fill_from_json(&emoji); + dpp::get_emoji_cache()->store(e); + } + emojis.push_back(e->id); + } + if (g) { + g->emojis = emojis; + } + } else { + for (auto & emoji : d["emojis"]) { + emojis.push_back(snowflake_not_null(&emoji, "id")); + } } if (!client->creator->on_guild_emojis_update.empty()) { dpp::guild_emojis_update_t geu(client, raw); - geu.emojis = g ? g->emojis : nullptr; + geu.emojis = emojis; geu.updating_guild = g; client->creator->on_guild_emojis_update.call(geu); } From f7378ce8a61457e3ecce632ec3f456e044abb664 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 14:33:50 +0000 Subject: [PATCH 6/8] fix: the old ways shall crumble and be replace by the new --- src/dpp/events/channel_update.cpp | 2 +- src/dpp/events/guild_update.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dpp/events/channel_update.cpp b/src/dpp/events/channel_update.cpp index 198749cfdc..66f7541b33 100644 --- a/src/dpp/events/channel_update.cpp +++ b/src/dpp/events/channel_update.cpp @@ -46,7 +46,7 @@ void channel_update::handle(discord_client* client, json &j, const std::string & newchannel.fill_from_json(&d); c = &newchannel; } else { - c = dpp::find_channel(from_string(d["id"].get())); + c = dpp::find_channel(snowflake_not_null(&d, "id")); if (c) { c->fill_from_json(&d); } diff --git a/src/dpp/events/guild_update.cpp b/src/dpp/events/guild_update.cpp index 751dd26f05..802f15832d 100644 --- a/src/dpp/events/guild_update.cpp +++ b/src/dpp/events/guild_update.cpp @@ -46,7 +46,7 @@ void guild_update::handle(discord_client* client, json &j, const std::string &ra newguild.fill_from_json(client, &d); g = &newguild; } else { - g = dpp::find_guild(from_string(d["id"].get())); + g = dpp::find_guild(snowflake_not_null(&d, "id")); if (g) { g->fill_from_json(client, &d); if (!g->is_unavailable()) { From 209e1c3e0c4c2f854bfb36d1f5a3776d87084f80 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 15:00:58 +0000 Subject: [PATCH 7/8] fix: remove redundant var --- src/dpp/events/channel_create.cpp | 5 +++++ src/dpp/events/guild_members_chunk.cpp | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/dpp/events/channel_create.cpp b/src/dpp/events/channel_create.cpp index d966c32158..90bd7a115c 100644 --- a/src/dpp/events/channel_create.cpp +++ b/src/dpp/events/channel_create.cpp @@ -46,6 +46,11 @@ void channel_create::handle(discord_client* client, json &j, const std::string & newchannel.fill_from_json(&d); c = &newchannel; g = dpp::find_guild(c->guild_id); + if (c.recipients.size()) { + for (auto & u : c.recipients) { + client->creator->set_dm_channel(u, c.id); + } + } } else { c = dpp::find_channel(snowflake_not_null(&d, "id")); if (!c) { diff --git a/src/dpp/events/guild_members_chunk.cpp b/src/dpp/events/guild_members_chunk.cpp index 44e64c32ee..3533f769e4 100644 --- a/src/dpp/events/guild_members_chunk.cpp +++ b/src/dpp/events/guild_members_chunk.cpp @@ -42,8 +42,7 @@ 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::snowflake guild_id = snowflake_not_null(&d, "guild_id"); - dpp::guild* g = dpp::find_guild(guild_id); + dpp::guild* g = dpp::find_guild(snowflake_not_null(&d, "guild_id")); if (g) { /* Store guild members */ if (client->creator->cache_policy.user_policy == cp_aggressive) { From 4dbd98598090a9282750721a857a0e438eefbfe9 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Wed, 27 Sep 2023 15:02:50 +0000 Subject: [PATCH 8/8] fix: shut up and sign the will already old man --- src/dpp/events/channel_create.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dpp/events/channel_create.cpp b/src/dpp/events/channel_create.cpp index 90bd7a115c..76875105d4 100644 --- a/src/dpp/events/channel_create.cpp +++ b/src/dpp/events/channel_create.cpp @@ -46,9 +46,9 @@ void channel_create::handle(discord_client* client, json &j, const std::string & newchannel.fill_from_json(&d); c = &newchannel; g = dpp::find_guild(c->guild_id); - if (c.recipients.size()) { - for (auto & u : c.recipients) { - client->creator->set_dm_channel(u, c.id); + if (c->recipients.size()) { + for (auto & u : c->recipients) { + client->creator->set_dm_channel(u, c->id); } } } else {