From 72ba15c798878fb338a23266240bab1de9958ce2 Mon Sep 17 00:00:00 2001
From: Syping <syping@syping.de>
Date: Mon, 5 Feb 2024 12:55:01 +0100
Subject: [PATCH] clean up and refactor some code

---
 src/main.cpp          | 48 +++++++------------------------------------
 src/message_queue.cpp | 46 ++++++++++++++++++++++++++++++++++++++---
 src/message_queue.h   |  3 +++
 src/slashcommands.cpp |  7 +++++++
 src/slashcommands.h   |  1 +
 src/submit_queue.cpp  |  9 ++++++--
 src/submit_queue.h    |  1 +
 7 files changed, 69 insertions(+), 46 deletions(-)

diff --git a/src/main.cpp b/src/main.cpp
index 4d227bb..648bd9f 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -31,23 +31,25 @@ int main(int argc, char* argv[]) {
         return 0;
     }
 
+    std::cout << "[Launch] Processing configuration..." << std::endl;
     bot::settings::settings settings;
     if (!settings.parse_file(argv[1]))
         return 1;
 
+    std::cout << "[Launch] Requesting supported languages..." << std::endl;
     if (settings.get_translator()->get_languages().empty()) {
         std::cerr << "[Error] Failed to initialise translateable languages" << std::endl;
         return 2;
     }
 
+    std::cout << "[Launch] Checking working directory..." << std::endl;
     if (!std::filesystem::exists(settings.storage_path())) {
         std::cerr << "[Error] Storage directory " << settings.storage_path() << " can not be found" << std::endl;
         return 2;
     }
 
     dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
-
-    bot.on_log([&bot](const dpp::log_t &event){
+    bot.on_log([&bot](const dpp::log_t &event) {
         std::cerr << "[Log] " << event.message << std::endl;
     });
 
@@ -57,51 +59,15 @@ int main(int argc, char* argv[]) {
     bot::message_queue message_queue;
     std::thread message_queue_loop(&bot::message_queue::run, &message_queue, &settings, &submit_queue);
 
-    bot.on_message_create([&bot, &message_queue, &settings](const dpp::message_create_t &event) {
-        if (event.msg.webhook_id) {
-            const std::lock_guard<bot::settings::settings> guard(settings);
-
-            // We will not translate messages from our own bot
-            if (settings.is_translatebot(event.msg.webhook_id))
-                return;
-        }
-
-        // Same as before, just without the involvement of webhooks
-        if (event.msg.author.id == bot.me.id)
-            return;
-
-        const std::lock_guard<bot::settings::settings> guard(settings);
-        if (const bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id)) {
-            bot::message message;
-            message.id = event.msg.id;
-
-            message.author = event.msg.member.get_nickname();
-            if (message.author.empty())
-                message.author = event.msg.author.global_name;
-
-            message.avatar = event.msg.member.get_avatar_url(settings.avatar_size());
-            if (message.avatar.empty())
-                message.avatar = event.msg.author.get_avatar_url(settings.avatar_size());
-
-            message.message = event.msg.content;
-            message.source = channel->source;
-            message.targets = channel->targets;
-            message_queue.add(std::move(message));
-        }
-    });
-
-    bot.on_slashcommand([&bot, &settings](const dpp::slashcommand_t &event) {
-        if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") {
-            bot::slashcommands::process_translate_command(&bot, &settings, event);
-        }
-    });
-
+    bot.on_message_create(std::bind(&bot::message_queue::process_message_event, &message_queue, &bot, &settings, std::placeholders::_1));
+    bot.on_slashcommand(std::bind(&bot::slashcommands::process_command_event, &bot, &settings, std::placeholders::_1));
     bot.on_ready([&bot, &settings](const dpp::ready_t &event) {
         if (dpp::run_once<struct register_bot_commands>()) {
             bot::slashcommands::register_commands(&bot, &settings);
         }
     });
 
+    std::cout << "[Launch] Starting bot..." << std::endl;
     bot.start(dpp::st_wait);
 
     // It's unneccessary, but we choose to exit clean anyway
diff --git a/src/message_queue.cpp b/src/message_queue.cpp
index a92ce20..7276de3 100644
--- a/src/message_queue.cpp
+++ b/src/message_queue.cpp
@@ -24,9 +24,49 @@ using namespace std::chrono_literals;
 
 void message_queue::add(const message &message)
 {
-    m_mutex.lock();
+    const std::lock_guard<std::mutex> guard(m_mutex);
     m_queue.push(message);
-    m_mutex.unlock();
+}
+
+void message_queue::add(message &&message)
+{
+    const std::lock_guard<std::mutex> guard(m_mutex);
+    m_queue.push(message);
+}
+
+void message_queue::process_message_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::message_create_t &event)
+{
+    if (event.msg.webhook_id) {
+        const std::lock_guard<bot::settings::settings> guard(*settings);
+
+        // We will not translate messages from our own bot
+        if (settings->is_translatebot(event.msg.webhook_id))
+            return;
+    }
+
+    // Same as before, just without the involvement of webhooks
+    if (event.msg.author.id == bot->me.id)
+        return;
+
+    const std::lock_guard<bot::settings::settings> guard(*settings);
+    if (const bot::settings::channel *channel = settings->get_channel(event.msg.guild_id, event.msg.channel_id)) {
+        bot::message message;
+        message.id = event.msg.id;
+
+        message.author = event.msg.member.get_nickname();
+        if (message.author.empty())
+            message.author = event.msg.author.global_name;
+
+        message.avatar = event.msg.member.get_avatar_url(settings->avatar_size());
+        if (message.avatar.empty())
+            message.avatar = event.msg.author.get_avatar_url(settings->avatar_size());
+
+        message.message = event.msg.content;
+        message.source = channel->source;
+        message.targets = channel->targets;
+
+        add(std::move(message));
+    }
 }
 
 void message_queue::run(bot::settings::settings *settings, submit_queue *submit_queue)
@@ -39,7 +79,7 @@ void message_queue::run(bot::settings::settings *settings, submit_queue *submit_
             m_queue.pop();
             m_mutex.unlock();
 
-            std::unique_ptr<bot::translator::translator> translator = settings->get_translator();
+            auto translator = settings->get_translator();
 
             for (auto target = message.targets.begin(); target != message.targets.end(); target++) {
                 translated_message tr_message;
diff --git a/src/message_queue.h b/src/message_queue.h
index ad92d2d..5d3cdde 100644
--- a/src/message_queue.h
+++ b/src/message_queue.h
@@ -18,6 +18,7 @@
 
 #ifndef MESSAGE_QUEUE_H
 #define MESSAGE_QUEUE_H
+#include <dpp/cluster.h>
 #include <mutex>
 #include <string>
 #include <queue>
@@ -38,6 +39,8 @@ namespace bot {
     class message_queue {
     public:
         void add(const message &message);
+        void add(message &&message);
+        void process_message_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::message_create_t &event);
         void run(bot::settings::settings *settings, submit_queue *submit_queue);
         void terminate();
 
diff --git a/src/slashcommands.cpp b/src/slashcommands.cpp
index 8f0fa49..26d7cbc 100644
--- a/src/slashcommands.cpp
+++ b/src/slashcommands.cpp
@@ -19,6 +19,13 @@
 #include "slashcommands.h"
 using namespace std::string_literals;
 
+void bot::slashcommands::process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
+{
+    if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") {
+        bot::slashcommands::process_translate_command(bot, settings, event);
+    }
+}
+
 void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
 {
     try {
diff --git a/src/slashcommands.h b/src/slashcommands.h
index 0fd45c2..f727150 100644
--- a/src/slashcommands.h
+++ b/src/slashcommands.h
@@ -24,6 +24,7 @@
 
 namespace bot {
     namespace slashcommands {
+        extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
         extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
         extern void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
     }
diff --git a/src/submit_queue.cpp b/src/submit_queue.cpp
index d8459e5..e752fab 100644
--- a/src/submit_queue.cpp
+++ b/src/submit_queue.cpp
@@ -24,9 +24,14 @@ using namespace std::chrono_literals;
 
 void submit_queue::add(const translated_message &message)
 {
-    m_mutex.lock();
+    const std::lock_guard<std::mutex> guard(m_mutex);
+    m_queue.push(message);
+}
+
+void submit_queue::add(translated_message &&message)
+{
+    const std::lock_guard<std::mutex> guard(m_mutex);
     m_queue.push(message);
-    m_mutex.unlock();
 }
 
 void submit_queue::run(dpp::cluster *bot)
diff --git a/src/submit_queue.h b/src/submit_queue.h
index 04fd16d..2d88546 100644
--- a/src/submit_queue.h
+++ b/src/submit_queue.h
@@ -35,6 +35,7 @@ namespace bot {
     class submit_queue {
     public:
         void add(const translated_message &message);
+        void add(translated_message &&message);
         void run(dpp::cluster *bot);
         void terminate();