diff --git a/CMakeLists.txt b/CMakeLists.txt index 66ca3cc..5002e99 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ set(DTRANSLATEBOT_HEADERS src/core/webhook_push.h src/database/file/file.h src/translator/libretranslate/libretranslate.h + src/translator/stub/stub.h ) set(DTRANSLATEBOT_SOURCES src/core/database.cpp @@ -46,6 +47,7 @@ set(DTRANSLATEBOT_SOURCES src/core/webhook_push.cpp src/database/file/file.cpp src/translator/libretranslate/libretranslate.cpp + src/translator/stub/stub.cpp ) # dtranslatebot Module Path diff --git a/src/core/main.cpp b/src/core/main.cpp index d86e55d..58b0885 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char* argv[]) { 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) { + bot.on_ready([&bot, &settings]([[maybe_unused]] const dpp::ready_t &event) { if (dpp::run_once()) { bot::slashcommands::register_commands(&bot, &settings); } diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 7e9528e..46a9af5 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -22,6 +22,7 @@ #include "settings.h" #include "../database/file/file.h" #include "../translator/libretranslate/libretranslate.h" +#include "../translator/stub/stub.h" using namespace bot::settings; void process_database_channels(std::shared_ptr database, bot::settings::guild &guild, std::vector &webhookIds) @@ -225,42 +226,60 @@ bool process_translator_settings(const dpp::json &json, translator &translator) return false; } - auto json_translate_hostname = json.find("hostname"); - if (json_translate_hostname != json.end()) - translator.hostname = *json_translate_hostname; - else - translator.hostname = {}; - - auto json_translate_tls = json.find("tls"); - if (json_translate_tls != json.end()) - translator.tls = *json_translate_tls; - else - translator.tls = false; - - auto json_translate_port = json.find("port"); - if (json_translate_port != json.end()) - translator.port = *json_translate_port; - else - translator.port = 0; - - auto json_translate_url = json.find("url"); - if (json_translate_url == json.end()) { - std::cerr << "[Error] Value url not found in translator object" << std::endl; - return false; + std::string translator_type = "libretranslate"; + auto json_translator_type = json.find("type"); + if (json_translator_type != json.end()) { + translator_type = *json_translator_type; + std::transform(translator_type.begin(), translator_type.end(), translator_type.begin(), ::tolower); } - if (translator.hostname.empty()) { - process_url(*json_translate_url, translator); + + if (translator_type == "libretranslate") { + translator.type = TRANSLATOR_LIBRETRANSLATE; + + auto json_lt_hostname = json.find("hostname"); + if (json_lt_hostname != json.end()) + translator.hostname = *json_lt_hostname; + else + translator.hostname = {}; + + auto json_lt_tls = json.find("tls"); + if (json_lt_tls != json.end()) + translator.tls = *json_lt_tls; + else + translator.tls = false; + + auto json_lt_port = json.find("port"); + if (json_lt_port != json.end()) + translator.port = *json_lt_port; + else + translator.port = 0; + + auto json_lt_url = json.find("url"); + if (json_lt_url == json.end()) { + std::cerr << "[Error] Value url not found in translator object" << std::endl; + return false; + } + if (translator.hostname.empty()) { + process_url(*json_lt_url, translator); + } + else { + translator.url = *json_lt_url; + } + + auto json_lt_apiKey = json.find("apiKey"); + if (json_lt_apiKey != json.end()) + translator.apiKey = *json_lt_apiKey; + else + translator.apiKey.clear(); + } + else if (translator_type == "stub") { + translator.type = TRANSLATOR_STUB; } else { - translator.url = *json_translate_url; + std::cerr << "[Error] Translator " << translator.type << " is unknown" << std::endl; + return false; } - auto json_translate_apiKey = json.find("apiKey"); - if (json_translate_apiKey != json.end()) - translator.apiKey = *json_translate_apiKey; - else - translator.apiKey.clear(); - return true; } @@ -438,7 +457,15 @@ std::shared_ptr settings::get_database() const std::unique_ptr settings::get_translator() const { const std::lock_guard guard(m_mutex); - return std::make_unique(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey); + + switch (m_translator.type) { + case TRANSLATOR_LIBRETRANSLATE: + return std::make_unique(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey); + case TRANSLATOR_STUB: + return std::make_unique(); + default: + return std::make_unique(); + } } const std::string settings::token() const diff --git a/src/core/settings_types.h b/src/core/settings_types.h index e1c5b3c..923be1c 100644 --- a/src/core/settings_types.h +++ b/src/core/settings_types.h @@ -40,7 +40,12 @@ namespace bot { dpp::snowflake id; std::vector channel; }; + enum translator_type { + TRANSLATOR_LIBRETRANSLATE, + TRANSLATOR_STUB + }; struct translator { + translator_type type; std::string hostname; uint16_t port; std::string url; diff --git a/src/translator/libretranslate/libretranslate.h b/src/translator/libretranslate/libretranslate.h index 0bb8afa..235a253 100644 --- a/src/translator/libretranslate/libretranslate.h +++ b/src/translator/libretranslate/libretranslate.h @@ -20,7 +20,6 @@ #define TRANSLATOR_LIBRETRANSLATE_H #include -#include #include "../../core/translator.h" namespace bot { diff --git a/src/translator/stub/stub.cpp b/src/translator/stub/stub.cpp new file mode 100644 index 0000000..7e7a083 --- /dev/null +++ b/src/translator/stub/stub.cpp @@ -0,0 +1,39 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#include "stub.h" +using namespace bot::translator; +using namespace std::string_literals; + +stub::stub() +{ +} + +stub::~stub() +{ +} + +const std::vector stub::get_languages() +{ + return { {"a*", "Any Language"} }; +} + +const std::string stub::translate(const std::string &text, [[maybe_unused]] const std::string &source, [[maybe_unused]] const std::string &target) +{ + return text; +} diff --git a/src/translator/stub/stub.h b/src/translator/stub/stub.h new file mode 100644 index 0000000..c6c73e4 --- /dev/null +++ b/src/translator/stub/stub.h @@ -0,0 +1,36 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#ifndef TRANSLATOR_STUB_H +#define TRANSLATOR_STUB_H + +#include "../../core/translator.h" + +namespace bot { + namespace translator { + class stub : public translator { + public: + explicit stub(); + ~stub() override; + const std::vector get_languages() override; + const std::string translate(const std::string &text, const std::string &source, const std::string &target) override; + }; + } +} + +#endif // TRANSLATOR_STUB_H