From 90f63df899ae3abeb964b55f97bd46df525b816b Mon Sep 17 00:00:00 2001 From: Brain Date: Wed, 27 Sep 2023 17:17:41 +0100 Subject: [PATCH] feat: allow disabling of guild and channel caches (#895) --- include/dpp/cluster.h | 78 ++++++++++- include/dpp/message.h | 32 +++++ src/dpp/discordvoiceclient.cpp | 2 +- src/dpp/events/channel_create.cpp | 51 ++++--- src/dpp/events/channel_delete.cpp | 16 +-- src/dpp/events/channel_update.cpp | 23 ++-- src/dpp/events/guild_create.cpp | 165 ++++++++++++----------- src/dpp/events/guild_delete.cpp | 11 +- src/dpp/events/guild_emojis_update.cpp | 42 +++--- src/dpp/events/guild_member_add.cpp | 62 +++++---- src/dpp/events/guild_member_update.cpp | 23 ++-- src/dpp/events/guild_members_chunk.cpp | 12 +- src/dpp/events/guild_role_create.cpp | 53 ++++---- 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_delete.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 +- 23 files changed, 484 insertions(+), 335 deletions(-) diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 926f91d3ec..55d8bc6b8f 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -213,12 +213,12 @@ class DPP_EXPORT cluster { * @param cluster_id The ID of this cluster, should be between 0 and MAXCLUSTERS-1 * @param maxclusters The total number of clusters that are active, which may be on separate processes or even separate machines. * @param compressed Whether or not to use compression for shards on this cluster. Saves a ton of bandwidth at the cost of some CPU - * @param policy Set the user caching policy for the cluster, either lazy (only cache users/members when they message the bot) or aggressive (request whole member lists on seeing new guilds too) + * @param policy Set the caching policy for the cluster, either lazy (only cache users/members when they message the bot) or aggressive (request whole member lists on seeing new guilds too) * @param request_threads The number of threads to allocate for making HTTP requests to Discord. This defaults to 12. You can increase this at runtime via the object returned from get_rest(). * @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 = cache_policy::cpol_default, uint32_t request_threads = 12, uint32_t request_threads_raw = 1); /** * @brief dpp::cluster is non-copyable @@ -695,6 +695,9 @@ class DPP_EXPORT cluster { /** * @brief Called when a new guild is created. * D++ will request members for the guild for its cache using guild_members_chunk. + * + * @warning If the cache policy has disabled guild caching, the pointer in this event will become invalid after the + * event ends. You should make a copy of any data you wish to preserve beyond this. * * @see https://discord.com/developers/docs/topics/gateway-events#guild-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. @@ -706,6 +709,9 @@ class DPP_EXPORT cluster { /** * @brief Called when a new channel is created on a guild. * + * @warning If the cache policy has disabled channel caching, the pointer in this event will become invalid after the + * event ends. You should make a copy of any data you wish to preserve beyond this. + * * @see https://discord.com/developers/docs/topics/gateway-events#channel-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type channel_create_t&, and returns void. @@ -736,6 +742,8 @@ class DPP_EXPORT cluster { /** * @brief Called when an existing role is updated on a guild. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-role-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_role_update_t&, and returns void. @@ -746,6 +754,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a role is deleted in a guild. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-role-delete * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_role_delete_t&, and returns void. @@ -780,6 +790,8 @@ class DPP_EXPORT cluster { * This will be sent either when we establish a new voice channel connection, * or as discord rearrange their infrastructure. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type voice_server_update_t&, and returns void. */ @@ -790,6 +802,8 @@ class DPP_EXPORT cluster { * @brief Called when new emojis are added to a guild. * The complete set of emojis is sent every time. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-emojis-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_emojis_update_t&, and returns void. @@ -801,6 +815,8 @@ class DPP_EXPORT cluster { * @brief Called when new stickers are added to a guild. * The complete set of stickers is sent every time. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-stickers-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_stickers_update_t&, and returns void. @@ -871,6 +887,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a new member joins a guild. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-member-add * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_member_add_t&, and returns void. @@ -891,6 +909,10 @@ class DPP_EXPORT cluster { /** * @brief Called when details of a guild are updated. * + * @warning If the cache policy has disabled guild caching, the pointer in this event will become invalid after the + * event ends. You should make a copy of any data you wish to preserve beyond this. If the guild cache is disabled, + * only changed elements in the updated guild object will be set. all other values will be empty or defaults. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_update_t&, and returns void. @@ -904,6 +926,8 @@ class DPP_EXPORT cluster { * An integration is a connection to a guild of a user's associated accounts, * e.g. youtube or twitch, for automatic assignment of roles etc. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-integrations-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_integrations_update_t&, and returns void. @@ -914,6 +938,8 @@ class DPP_EXPORT cluster { /** * @brief Called when details of a guild member (e.g. their roles or nickname) are updated. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-member-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_member_update_t&, and returns void. @@ -924,6 +950,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a new invite is created for a guild. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#invite-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type invite_create_t&, and returns void. @@ -978,6 +1006,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a ban is added to a guild. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-ban-add * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_ban_add_t&, and returns void. @@ -988,6 +1018,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a ban is removed from a guild. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-ban-remove * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_ban_remove_t&, and returns void. @@ -1000,6 +1032,8 @@ class DPP_EXPORT cluster { * An integration is a connection to a guild of a user's associated accounts, * e.g. youtube or twitch, for automatic assignment of roles etc. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#integration-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type integration_create_t&, and returns void. @@ -1013,6 +1047,8 @@ class DPP_EXPORT cluster { * An integration is a connection to a guild of a user's associated accounts, * e.g. youtube or twitch, for automatic assignment of roles etc. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#integration-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type integration_update_t&, and returns void. @@ -1025,6 +1061,8 @@ class DPP_EXPORT cluster { * An integration is a connection to a guild of a user's associated accounts, * e.g. youtube or twitch, for automatic assignment of roles etc. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#integration-delete * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type integration_delete_t&, and returns void. @@ -1036,6 +1074,8 @@ class DPP_EXPORT cluster { * @brief Called when a thread is created. * Note that threads are not cached by D++, but a list of thread IDs is accessible in a guild object * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#thread-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type thread_create_t&, and returns void. @@ -1046,6 +1086,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a thread is updated * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#thread-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type thread_update_t&, and returns void. @@ -1056,6 +1098,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a thread is deleted * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#thread-delete * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type thread_delete_t&, and returns void. @@ -1067,6 +1111,8 @@ class DPP_EXPORT cluster { * @brief Called when thread list is synced (upon gaining access to a channel). * Note that threads are not cached by D++, but a list of thread IDs is accessible in a guild object * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#thread-list-sync * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type thread_list_sync_t&, and returns void. @@ -1077,6 +1123,8 @@ class DPP_EXPORT cluster { /** * @brief Called when current user's thread member object is updated * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#thread-member-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type thread_member_update_t&, and returns void. @@ -1087,6 +1135,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a thread's member list is updated (without GUILD_MEMBERS intent, is only called for current user) * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#thread-members-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type thread_members_update_t&, and returns void. @@ -1097,6 +1147,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a new scheduled event is created * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_scheduled_event_create_t&, and returns void. @@ -1107,6 +1159,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a new scheduled event is updated * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_scheduled_event_update_t&, and returns void. @@ -1117,6 +1171,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a new scheduled event is deleted * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-delete * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_scheduled_event_delete_t&, and returns void. @@ -1127,6 +1183,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a user is added to a scheduled event * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-user-add * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type guild_scheduled_event_user_add_t&, and returns void. @@ -1135,7 +1193,9 @@ class DPP_EXPORT cluster { /** - * @brief Called when a user is removed to a scheduled event + * @brief Called when a user is removed from a scheduled event + * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. * * @see https://discord.com/developers/docs/topics/gateway-events#guild-scheduled-event-user-remove * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. @@ -1152,6 +1212,8 @@ class DPP_EXPORT cluster { * of dpp::voice_buffer_send_t to determine if you should fill the buffer with more * content. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type voice_buffer_send_t&, and returns void. */ @@ -1161,6 +1223,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a user is talking on a voice channel. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type voice_user_talking_t&, and returns void. */ @@ -1172,6 +1236,8 @@ class DPP_EXPORT cluster { * Note that this is not directly attached to the READY event of the websocket, * as there is further connection that needs to be done before audio is ready to send. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type voice_ready_t&, and returns void. */ @@ -1216,6 +1282,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a new stage instance is created on a stage channel. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#stage-instance-create * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type stage_instance_create_t&, and returns void. @@ -1226,6 +1294,8 @@ class DPP_EXPORT cluster { /** * @brief Called when a stage instance is updated. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#stage-instance-update * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type stage_instance_update_t&, and returns void. @@ -1236,6 +1306,8 @@ class DPP_EXPORT cluster { /** * @brief Called when an existing stage instance is deleted from a stage channel. * + * @warning If the cache policy has disabled guild caching, the pointer to the guild in this event may be nullptr. + * * @see https://discord.com/developers/docs/topics/gateway-events#stage-instance-delete * @note Use operator() to attach a lambda to this event, and the detach method to detach the listener using the returned ID. * The function signature for this event takes a single `const` reference of type stage_instance_delete_t&, and returns void. diff --git a/include/dpp/message.h b/include/dpp/message.h index 422722fb0c..ea38829b2c 100644 --- a/include/dpp/message.h +++ b/include/dpp/message.h @@ -1172,6 +1172,38 @@ 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; +}; + +/** + * @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 }; + }; /** 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..76875105d4 100644 --- a/src/dpp/events/channel_create.cpp +++ b/src/dpp/events/channel_create.cpp @@ -38,28 +38,41 @@ 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; - 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(&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); + } } - } - 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); + } else { + 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); + } + } + 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..66f7541b33 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(&d); + c = &newchannel; + } else { + c = dpp::find_channel(snowflake_not_null(&d, "id")); + 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..8fea1392aa 100644 --- a/src/dpp/events/guild_create.cpp +++ b/src/dpp/events/guild_create.cpp @@ -42,97 +42,110 @@ 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) + 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; - 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(); + + if (client->creator->cache_policy.guild_policy == cp_none) { + newguild.fill_from_json(client, &d); + g = &newguild; + } else { + bool is_new_guild = false; + g = dpp::find_guild(snowflake_not_null(&d, "id")); + if (!g) { + g = new dpp::guild(); + is_new_guild = true; + } + g->fill_from_json(client, &d); + g->shard_id = client->shard_id; + if (!g->is_unavailable() && is_new_guild) { + 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 (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) { + 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..8dbf12c9e8 100644 --- a/src/dpp/events/guild_emojis_update.cpp +++ b/src/dpp/events/guild_emojis_update.cpp @@ -43,32 +43,40 @@ 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) { + 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) { + if (g) { for (auto & ee : g->emojis) { dpp::emoji* fe = dpp::find_emoji(ee); if (fe) { dpp::get_emoji_cache()->remove(fe); } } - 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); - } - 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 (!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 (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 = 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_members_chunk.cpp b/src/dpp/events/guild_members_chunk.cpp index bcbfeba83a..3533f769e4 100644 --- a/src/dpp/events/guild_members_chunk.cpp +++ b/src/dpp/events/guild_members_chunk.cpp @@ -64,12 +64,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 4ff8693f6b..d3bc02b2c2 100644 --- a/src/dpp/events/guild_role_create.cpp +++ b/src/dpp/events/guild_role_create.cpp @@ -41,33 +41,34 @@ 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); } } } 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..802f15832d 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(snowflake_not_null(&d, "id")); + 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_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/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..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}) { +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 {