mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 13:50:22 +01:00
add stub translator for testing purposes
This commit is contained in:
parent
94429ca718
commit
fb709b8919
7 changed files with 142 additions and 34 deletions
|
@ -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
|
||||
|
|
|
@ -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<struct register_bot_commands>()) {
|
||||
bot::slashcommands::register_commands(&bot, &settings);
|
||||
}
|
||||
|
|
|
@ -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<bot::database::database> database, bot::settings::guild &guild, std::vector<dpp::snowflake> &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<bot::database::database> settings::get_database() const
|
|||
std::unique_ptr<bot::translator::translator> settings::get_translator() const
|
||||
{
|
||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||
return std::make_unique<bot::translator::libretranslate>(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<bot::translator::libretranslate>(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey);
|
||||
case TRANSLATOR_STUB:
|
||||
return std::make_unique<bot::translator::stub>();
|
||||
default:
|
||||
return std::make_unique<bot::translator::translator>();
|
||||
}
|
||||
}
|
||||
|
||||
const std::string settings::token() const
|
||||
|
|
|
@ -40,7 +40,12 @@ namespace bot {
|
|||
dpp::snowflake id;
|
||||
std::vector<bot::settings::channel> channel;
|
||||
};
|
||||
enum translator_type {
|
||||
TRANSLATOR_LIBRETRANSLATE,
|
||||
TRANSLATOR_STUB
|
||||
};
|
||||
struct translator {
|
||||
translator_type type;
|
||||
std::string hostname;
|
||||
uint16_t port;
|
||||
std::string url;
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
#define TRANSLATOR_LIBRETRANSLATE_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include "../../core/translator.h"
|
||||
|
||||
namespace bot {
|
||||
|
|
39
src/translator/stub/stub.cpp
Normal file
39
src/translator/stub/stub.cpp
Normal file
|
@ -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<language> 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;
|
||||
}
|
36
src/translator/stub/stub.h
Normal file
36
src/translator/stub/stub.h
Normal file
|
@ -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<language> get_languages() override;
|
||||
const std::string translate(const std::string &text, const std::string &source, const std::string &target) override;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TRANSLATOR_STUB_H
|
Loading…
Reference in a new issue