diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2020-12-15 02:45:37 -0500 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2020-12-15 02:45:37 -0500 |
commit | 315a4a8df80b922bbb8132c625803443c031dfd3 (patch) | |
tree | e748909cf841a65f565c2aa6a12b8dc2c58ca9e8 /discord | |
parent | 2667a4b30dd346f70aa8ca1ee7994c559be6d2bb (diff) | |
download | abaddon-portaudio-315a4a8df80b922bbb8132c625803443c031dfd3.tar.gz abaddon-portaudio-315a4a8df80b922bbb8132c625803443c031dfd3.zip |
fix some reaction stuff
Diffstat (limited to 'discord')
-rw-r--r-- | discord/discord.cpp | 26 | ||||
-rw-r--r-- | discord/discord.hpp | 2 | ||||
-rw-r--r-- | discord/store.cpp | 16 |
3 files changed, 33 insertions, 11 deletions
diff --git a/discord/discord.cpp b/discord/discord.cpp index 2151d86..9e6726c 100644 --- a/discord/discord.cpp +++ b/discord/discord.cpp @@ -121,9 +121,8 @@ void DiscordClient::FetchMessagesInChannel(Snowflake id, std::function<void(cons m_store.BeginTransaction(); for (const auto &msg : msgs) { - m_store.SetMessage(msg.ID, msg); + StoreMessageData(msg); AddMessageToChannel(msg.ID, id); - m_store.SetUser(msg.Author.ID, msg.Author); AddUserToGuild(msg.Author.ID, *msg.GuildID); ids.push_back(msg.ID); } @@ -145,9 +144,8 @@ void DiscordClient::FetchMessagesInChannelBefore(Snowflake channel_id, Snowflake m_store.BeginTransaction(); for (const auto &msg : msgs) { - m_store.SetMessage(msg.ID, msg); + StoreMessageData(msg); AddMessageToChannel(msg.ID, channel_id); - m_store.SetUser(msg.Author.ID, msg.Author); AddUserToGuild(msg.Author.ID, *msg.GuildID); ids.push_back(msg.ID); } @@ -664,9 +662,8 @@ void DiscordClient::HandleGatewayReady(const GatewayMessage &msg) { void DiscordClient::HandleGatewayMessageCreate(const GatewayMessage &msg) { Message data = msg.Data; - m_store.SetMessage(data.ID, data); + StoreMessageData(data); AddMessageToChannel(data.ID, data.ChannelID); - m_store.SetUser(data.Author.ID, data.Author); AddUserToGuild(data.Author.ID, *data.GuildID); m_signal_message_create.emit(data.ID); } @@ -772,6 +769,11 @@ void DiscordClient::HandleGatewayGuildRoleDelete(const GatewayMessage &msg) { void DiscordClient::HandleGatewayMessageReactionAdd(const GatewayMessage &msg) { MessageReactionAddObject data = msg.Data; auto to = m_store.GetMessage(data.MessageID); + if (data.Emoji.ID.IsValid()) { + const auto cur_emoji = m_store.GetEmoji(data.Emoji.ID); + if (!cur_emoji.has_value()) + m_store.SetEmoji(data.Emoji.ID, data.Emoji); + } if (!to.has_value()) return; if (!to->Reactions.has_value()) to->Reactions.emplace(); // add if present @@ -1018,6 +1020,18 @@ bool DiscordClient::CheckCode(const cpr::Response &r) { return true; } +void DiscordClient::StoreMessageData(const Message &msg) { + m_store.SetMessage(msg.ID, msg); + m_store.SetUser(msg.Author.ID, msg.Author); + if (msg.Reactions.has_value()) + for (const auto &r : *msg.Reactions) { + if (!r.Emoji.ID.IsValid()) continue; + const auto cur = m_store.GetEmoji(r.Emoji.ID); + if (!cur.has_value()) + m_store.SetEmoji(r.Emoji.ID, r.Emoji); + } +} + void DiscordClient::LoadEventMap() { m_event_map["READY"] = GatewayEvent::READY; m_event_map["MESSAGE_CREATE"] = GatewayEvent::MESSAGE_CREATE; diff --git a/discord/discord.hpp b/discord/discord.hpp index 767cf7a..c43a764 100644 --- a/discord/discord.hpp +++ b/discord/discord.hpp @@ -154,6 +154,8 @@ private: bool CheckCode(const cpr::Response &r); + void StoreMessageData(const Message &msg); + std::string m_token; void AddMessageToChannel(Snowflake msg_id, Snowflake channel_id); diff --git a/discord/store.cpp b/discord/store.cpp index bf6318a..44933bb 100644 --- a/discord/store.cpp +++ b/discord/store.cpp @@ -328,11 +328,17 @@ std::optional<Emoji> Store::GetEmoji(Snowflake id) const { Emoji ret; ret.ID = id; Get(m_get_emote_stmt, 1, ret.Name); - std::string tmp; - Get(m_get_emote_stmt, 2, tmp); - ret.Roles = nlohmann::json::parse(tmp).get<std::vector<Snowflake>>(); - ret.Creator = std::optional<User>(User()); - Get(m_get_emote_stmt, 3, ret.Creator->ID); + + if (!IsNull(m_get_emote_stmt, 2)) { + std::string tmp; + Get(m_get_emote_stmt, 2, tmp); + ret.Roles = nlohmann::json::parse(tmp).get<std::vector<Snowflake>>(); + } + + if (!IsNull(m_get_emote_stmt, 3)) { + ret.Creator = std::optional<User>(User()); + Get(m_get_emote_stmt, 3, ret.Creator->ID); + } Get(m_get_emote_stmt, 3, ret.NeedsColons); Get(m_get_emote_stmt, 4, ret.IsManaged); Get(m_get_emote_stmt, 5, ret.IsAnimated); |