diff options
author | ouwou <26526779+ouwou@users.noreply.github.com> | 2022-06-14 02:36:04 -0400 |
---|---|---|
committer | ouwou <26526779+ouwou@users.noreply.github.com> | 2022-06-14 02:36:04 -0400 |
commit | 4456c8771da668cbadb411583624fe0e357fa687 (patch) | |
tree | 789e2d73e6f0994b314b8e18c9cb8209562666e2 /src/discord/discord.cpp | |
parent | caa551a4693c7c7ef475be515b31eb025bc7df96 (diff) | |
download | abaddon-portaudio-4456c8771da668cbadb411583624fe0e357fa687.tar.gz abaddon-portaudio-4456c8771da668cbadb411583624fe0e357fa687.zip |
refactor send message params into one struct
Diffstat (limited to 'src/discord/discord.cpp')
-rw-r--r-- | src/discord/discord.cpp | 99 |
1 files changed, 52 insertions, 47 deletions
diff --git a/src/discord/discord.cpp b/src/discord/discord.cpp index ffc1d4d..9849ae6 100644 --- a/src/discord/discord.cpp +++ b/src/discord/discord.cpp @@ -413,7 +413,7 @@ bool DiscordClient::CanManageMember(Snowflake guild_id, Snowflake actor, Snowfla return actor_highest->Position > target_highest->Position; } -void DiscordClient::ChatMessageCallback(const std::string &nonce, const http::response_type &response) { +void DiscordClient::ChatMessageCallback(const std::string &nonce, const http::response_type &response, const sigc::slot<void(DiscordError)> &callback) { if (!CheckCode(response)) { if (response.status_code == http::TooManyRequests) { try { // not sure if this body is guaranteed @@ -425,75 +425,79 @@ void DiscordClient::ChatMessageCallback(const std::string &nonce, const http::re } else { m_signal_message_send_fail.emit(nonce, 0); } + + // todo actually callback with correct error code (not necessary rn) + callback(DiscordError::GENERIC); + } else { + callback(DiscordError::NONE); } } -void DiscordClient::SendChatMessageAttachments(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel, Snowflake referenced_message) { +void DiscordClient::SendChatMessageNoAttachments(const ChatSubmitParams ¶ms, const sigc::slot<void(DiscordError)> &callback) { const auto nonce = std::to_string(Snowflake::FromNow()); + CreateMessageObject obj; - obj.Content = content; + obj.Content = params.Message; obj.Nonce = nonce; - if (referenced_message.IsValid()) - obj.MessageReference.emplace().MessageID = referenced_message; + if (params.InReplyToID.IsValid()) + obj.MessageReference.emplace().MessageID = params.InReplyToID; - auto req = m_http.CreateRequest(http::REQUEST_POST, "/channels/" + std::to_string(channel) + "/messages"); - req.make_form(); - req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED); - for (size_t i = 0; i < attachment_paths.size(); i++) { - const auto field_name = "files[" + std::to_string(i) + "]"; - req.add_file(field_name, attachment_paths.at(i), "unknown.png"); - } - m_http.Execute(std::move(req), [this, attachment_paths, nonce](const http::response_type &res) { - for (const auto &path : attachment_paths) { - std::error_code ec; - std::filesystem::remove(path, ec); - } - ChatMessageCallback(nonce, res); - }); -} + m_http.MakePOST("/channels/" + std::to_string(params.ChannelID) + "/messages", + nlohmann::json(obj).dump(), + [this, nonce, callback](const http::response_type &r) { + ChatMessageCallback(nonce, r, callback); + }); -void DiscordClient::SendChatMessageText(const std::string &content, Snowflake channel, Snowflake referenced_message) { - // @([^@#]{1,32})#(\\d{4}) - const auto nonce = std::to_string(Snowflake::FromNow()); - CreateMessageObject obj; - obj.Content = content; - obj.Nonce = nonce; - if (referenced_message.IsValid()) - obj.MessageReference.emplace().MessageID = referenced_message; - m_http.MakePOST("/channels/" + std::to_string(channel) + "/messages", nlohmann::json(obj).dump(), sigc::bind<0>(sigc::mem_fun(*this, &DiscordClient::ChatMessageCallback), nonce)); - // dummy data so the content can be shown while waiting for MESSAGE_CREATE + // dummy preview data Message tmp; - tmp.Content = content; + tmp.Content = params.Message; tmp.ID = nonce; - tmp.ChannelID = channel; + tmp.ChannelID = params.ChannelID; tmp.Author = GetUserData(); tmp.IsTTS = false; tmp.DoesMentionEveryone = false; tmp.Type = MessageType::DEFAULT; tmp.IsPinned = false; tmp.Timestamp = "2000-01-01T00:00:00.000000+00:00"; - tmp.Nonce = obj.Nonce; + tmp.Nonce = nonce; tmp.IsPending = true; + m_store.SetMessage(tmp.ID, tmp); - m_signal_message_sent.emit(tmp); + m_signal_message_create.emit(tmp); } -void DiscordClient::SendChatMessage(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel) { - if (attachment_paths.empty()) - SendChatMessageText(content, channel); - else { - puts("attach"); - SendChatMessageAttachments(content, attachment_paths, channel, Snowflake::Invalid); +void DiscordClient::SendChatMessageAttachments(const ChatSubmitParams ¶ms, const sigc::slot<void(DiscordError)> &callback) { + const auto nonce = std::to_string(Snowflake::FromNow()); + + CreateMessageObject obj; + obj.Content = params.Message; + obj.Nonce = nonce; + if (params.InReplyToID.IsValid()) + obj.MessageReference.emplace().MessageID = params.InReplyToID; + + auto req = m_http.CreateRequest(http::REQUEST_POST, "/channels/" + std::to_string(params.ChannelID) + "/messages"); + req.make_form(); + req.add_field("payload_json", nlohmann::json(obj).dump().c_str(), CURL_ZERO_TERMINATED); + for (size_t i = 0; i < params.Attachments.size(); i++) { + const auto field_name = "files[" + std::to_string(i) + "]"; + req.add_file(field_name, params.Attachments.at(i).Path, "unknown.png"); } + m_http.Execute(std::move(req), [this, params, nonce, callback](const http::response_type &res) { + for (const auto &attachment : params.Attachments) { + if (attachment.Type == ChatSubmitParams::AttachmentType::PastedImage) { + std::error_code ec; + std::filesystem::remove(attachment.Path, ec); + } + } + ChatMessageCallback(nonce, res, callback); + }); } -void DiscordClient::SendChatMessage(const std::string &content, const std::vector<std::string> &attachment_paths, Snowflake channel, Snowflake referenced_message) { - if (attachment_paths.empty()) - SendChatMessageText(content, channel, referenced_message); - else { - puts("attach"); - SendChatMessageAttachments(content, attachment_paths, channel, referenced_message); - } +void DiscordClient::SendChatMessage(const ChatSubmitParams ¶ms, const sigc::slot<void(DiscordError)> &callback) { + if (params.Attachments.empty()) + SendChatMessageNoAttachments(params, callback); + else + SendChatMessageAttachments(params, callback); } void DiscordClient::DeleteMessage(Snowflake channel_id, Snowflake id) { @@ -1302,6 +1306,7 @@ void DiscordClient::HandleGatewayMessage(std::string str) { HandleGatewayInvalidSession(m); } break; case GatewayOp::Dispatch: { + puts(m.Type.c_str()); auto iter = m_event_map.find(m.Type); if (iter == m_event_map.end()) { printf("Unknown event %s\n", m.Type.c_str()); |