From ccd2736c63542eeef0fa36b852e0926e2351ffb4 Mon Sep 17 00:00:00 2001
From: Syping <syping@syping.de>
Date: Thu, 18 Jan 2024 16:09:25 +0100
Subject: [PATCH] CMakeLists.txt and Webhook push improvements

---
 CMakeLists.txt        | 44 +++++++++++++++++++++++--------------------
 src/main.cpp          |  4 ++--
 src/message_queue.cpp |  7 ++++---
 src/settings.cpp      |  8 ++++----
 src/settings.h        |  8 ++++----
 src/submit_queue.cpp  |  3 +--
 src/webhook_push.cpp  | 24 ++++++++++-------------
 src/webhook_push.h    |  3 +--
 8 files changed, 50 insertions(+), 51 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1c398bd..3c460c6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,36 +17,40 @@
 ****************************************************************************]]
 
 cmake_minimum_required(VERSION 3.16)
-project(dtranslatebot VERSION 0.1 DESCRIPTION "Discord Translation Bot")
+project(dtranslatebot VERSION 0.1 LANGUAGES CXX)
+include(GNUInstallDirs)
 
-list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
-
-add_executable(${PROJECT_NAME}
-    src/main.cpp
-    src/message_queue.cpp
+# dtranslatebot Source files
+set(DTRANSLATEBOT_HEADERS
     src/message_queue.h
-    src/settings.cpp
     src/settings.h
-    src/submit_queue.cpp
     src/submit_queue.h
-    src/webhook_push.cpp
     src/webhook_push.h
 )
+set(DTRANSLATEBOT_SOURCES
+    src/main.cpp
+    src/message_queue.cpp
+    src/settings.cpp
+    src/submit_queue.cpp
+    src/webhook_push.cpp
+)
 
-set(THREADS_PREFER_PTHREAD_FLAG ON)
+# dtranslatebot Module Path
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
+
+# D++ Discord API Library for Bots
 find_package(DPP REQUIRED)
+
+# pthread Support
+set(THREADS_PREFER_PTHREAD_FLAG ON)
 find_package(Threads REQUIRED)
 
-target_link_libraries(${PROJECT_NAME}
-    ${DPP_LIBRARIES}
-    Threads::Threads
-)
-
-target_include_directories(${PROJECT_NAME} PRIVATE
-    ${DPP_INCLUDE_DIR}
-)
-
-set_target_properties(${PROJECT_NAME} PROPERTIES
+# dtranslatebot Target + Installs
+add_executable(dtranslatebot ${DTRANSLATEBOT_HEADERS} ${DTRANSLATEBOT_SOURCES})
+target_link_libraries(dtranslatebot Threads::Threads ${DPP_LIBRARIES})
+target_include_directories(dtranslatebot PRIVATE ${DPP_INCLUDE_DIR})
+set_target_properties(dtranslatebot PROPERTIES
     CXX_STANDARD 17
     CXX_STANDARD_REQUIRED ON
 )
+install(TARGETS dtranslatebot DESTINATION "${CMAKE_INSTALL_BINDIR}")
diff --git a/src/main.cpp b/src/main.cpp
index c1d52f6..2cfc21d 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -52,14 +52,14 @@ int main(int argc, char* argv[]) {
         }
 
         const std::lock_guard<bot::settings::settings> guard(settings);
-        bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id);
+        const bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id);
         if (channel) {
             bot::message message;
             message.id = event.msg.id;
 
             message.author = event.msg.member.get_nickname();
             if (message.author.empty())
-                message.author = event.msg.author.format_username();
+                message.author = event.msg.author.global_name;
 
             message.avatar = event.msg.member.get_avatar_url(settings.get_avatar_size());
             if (message.avatar.empty())
diff --git a/src/message_queue.cpp b/src/message_queue.cpp
index 03ff513..0c46ebd 100644
--- a/src/message_queue.cpp
+++ b/src/message_queue.cpp
@@ -49,7 +49,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
             m_mutex.unlock();
 
             settings->lock();
-            bot::settings::translate *translate = settings->get_translate();
+            const bot::settings::translate *translate = settings->get_translate();
             const std::string tr_apiKey = translate->apiKey;
             const std::string tr_hostname = translate->hostname;
             const uint16_t tr_port = translate->port;
@@ -68,8 +68,9 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
                 if (!tr_apiKey.empty())
                     json_body.emplace("apiKey", tr_apiKey);
 
-                dpp::http_headers http_headers;
-                http_headers.emplace("Content-Type", "application/json");
+                const dpp::http_headers http_headers = {
+                    {"Content-Type", "application/json"}
+                };
 
                 std::string tr_message = message.message;
 
diff --git a/src/settings.cpp b/src/settings.cpp
index 2f33576..1e89813 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -26,7 +26,7 @@ uint16_t bot::settings::settings::get_avatar_size()
     return m_avatarSize;
 }
 
-bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id)
+const bot::settings::channel *bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id)
 {
     for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
         if (channel->id == channel_id)
@@ -35,7 +35,7 @@ bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guil
     return nullptr;
 }
 
-bot::settings::channel* bot::settings::settings::get_channel(uint64_t guild_id, uint64_t channel_id)
+const bot::settings::channel *bot::settings::settings::get_channel(uint64_t guild_id, uint64_t channel_id)
 {
     for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
         if (guild->id == guild_id) {
@@ -48,7 +48,7 @@ bot::settings::channel* bot::settings::settings::get_channel(uint64_t guild_id,
     return nullptr;
 }
 
-bot::settings::guild* bot::settings::settings::get_guild(uint64_t guild_id)
+const bot::settings::guild *bot::settings::settings::get_guild(uint64_t guild_id)
 {
     for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
         if (guild->id == guild_id)
@@ -57,7 +57,7 @@ bot::settings::guild* bot::settings::settings::get_guild(uint64_t guild_id)
     return nullptr;
 }
 
-bot::settings::translate* bot::settings::settings::get_translate()
+const bot::settings::translate *bot::settings::settings::get_translate()
 {
     return &m_translate;
 }
diff --git a/src/settings.h b/src/settings.h
index fd7209f..0eb5f80 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -49,10 +49,10 @@ namespace bot {
         class settings {
         public:
             uint16_t get_avatar_size();
-            bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id);
-            bot::settings::channel* get_channel(uint64_t guild_id, uint64_t channel_id);
-            bot::settings::guild* get_guild(uint64_t guild_id);
-            bot::settings::translate* get_translate();
+            const bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id);
+            const bot::settings::channel* get_channel(uint64_t guild_id, uint64_t channel_id);
+            const bot::settings::guild* get_guild(uint64_t guild_id);
+            const bot::settings::translate* get_translate();
             const std::string get_token();
             bool is_translatebot(uint64_t webhook_id);
             void lock();
diff --git a/src/submit_queue.cpp b/src/submit_queue.cpp
index 08f1519..f910a6b 100644
--- a/src/submit_queue.cpp
+++ b/src/submit_queue.cpp
@@ -38,8 +38,7 @@ void bot::submit_queue::run(dpp::cluster *bot)
             m_queue.erase(m_queue.begin());
             m_mutex.unlock();
 
-            dpp::webhook webhook(message.webhook);
-            bot::webhook_push webhook_push(webhook, message);
+            bot::webhook_push webhook_push(message.webhook, message, bot);
 
             std::this_thread::yield();
         }
diff --git a/src/webhook_push.cpp b/src/webhook_push.cpp
index 9826416..863e6f5 100644
--- a/src/webhook_push.cpp
+++ b/src/webhook_push.cpp
@@ -16,11 +16,10 @@
 * responsible for anything with use of the software, you are self responsible.
 *****************************************************************************/
 
-#include <sstream>
 #include "webhook_push.h"
 
-bot::webhook_push::webhook_push(const dpp::webhook &webhook, const bot::translated_message &message) :
-    m_webhook(webhook), m_message(message)
+bot::webhook_push::webhook_push(const std::string &webhook, const bot::translated_message &message, dpp::cluster *bot) :
+    m_message(message)
 {
     const dpp::json json_body = {
         {"content", message.message},
@@ -28,22 +27,19 @@ bot::webhook_push::webhook_push(const dpp::webhook &webhook, const bot::translat
         {"avatar_url", message.avatar}
     };
 
-    dpp::http_headers http_headers;
-    http_headers.emplace("Content-Type", "application/json");
-
-    std::ostringstream webhook_url;
-    webhook_url << "/api/webhooks/" << m_webhook.id << "/" << m_webhook.token;
-
     try {
-        dpp::https_client https_request("discord.com", 443, webhook_url.str(), "POST", json_body.dump(), http_headers);
-        m_content = https_request.get_content();
-        m_status = https_request.get_status();
+        dpp::http_request webhook_request(webhook, nullptr, dpp::m_post, json_body.dump(), "application/json");
+        const dpp::http_request_completion_t result = webhook_request.run(bot);
+        if (result.status != 204)
+            std::cerr << "Webhook push returned unexpected code " << result.status << " with response: " << result.body << std::endl;
+        m_content = result.body;
+        m_status = result.status;
     }
     catch (const std::exception &exception) {
-        std::cerr << "Exception thrown while submitting: " << exception.what() << std::endl;
+        std::cerr << "Exception thrown while Webhook push: " << exception.what() << std::endl;
     }
     catch (...) {
-        std::cerr << "Exception thrown while submitting: unknown" << std::endl;
+        std::cerr << "Exception thrown while Webhook push: unknown" << std::endl;
     }
 }
 
diff --git a/src/webhook_push.h b/src/webhook_push.h
index 196b590..588b53e 100644
--- a/src/webhook_push.h
+++ b/src/webhook_push.h
@@ -25,14 +25,13 @@
 namespace bot {
     class webhook_push {
     public:
-        explicit webhook_push(const dpp::webhook &webhook, const bot::translated_message &message);
+        explicit webhook_push(const std::string &webhook, const bot::translated_message &message, dpp::cluster *bot);
         const std::string get_content() const;
         uint16_t get_status() const;
 
     private:
         std::string m_content;
         uint16_t m_status;
-        dpp::webhook m_webhook;
         bot::translated_message m_message;
     };
 }