mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 13:50:22 +01:00
more refactoring
This commit is contained in:
parent
b09da9cfce
commit
7e3696ce90
5 changed files with 47 additions and 32 deletions
|
@ -1,5 +1,4 @@
|
||||||
{
|
{
|
||||||
"avatar_size": 256,
|
|
||||||
"guilds": {
|
"guilds": {
|
||||||
"$guild1_id": {
|
"$guild1_id": {
|
||||||
"$channel1_id": {
|
"$channel1_id": {
|
||||||
|
@ -27,6 +26,9 @@
|
||||||
},
|
},
|
||||||
"preferred_lang": ["en", "de", "fr", ...],
|
"preferred_lang": ["en", "de", "fr", ...],
|
||||||
"storage": "$working_directory",
|
"storage": "$working_directory",
|
||||||
|
"user": {
|
||||||
|
"avatar_size": 256
|
||||||
|
},
|
||||||
"token": "$bot_token",
|
"token": "$bot_token",
|
||||||
"translator": {
|
"translator": {
|
||||||
"hostname": "127.0.0.1",
|
"hostname": "127.0.0.1",
|
||||||
|
|
|
@ -36,18 +36,20 @@ int main(int argc, char* argv[]) {
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (settings.get_translator()->get_languages().empty()) {
|
if (settings.get_translator()->get_languages().empty()) {
|
||||||
std::cerr << "[dtranslatebot] Error: Failed to initialise translateable languages" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] Failed to initialise translateable languages" << std::endl;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!std::filesystem::exists(settings.storage_path())) {
|
if (!std::filesystem::exists(settings.storage_path())) {
|
||||||
std::cerr << "[dtranslatebot] Error: Storage directory " << settings.storage_path() << " can not be found" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] Storage directory " << settings.storage_path() << " can not be found" << std::endl;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
|
dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
|
||||||
|
|
||||||
bot.on_log(dpp::utility::cout_logger());
|
bot.on_log([&bot](const dpp::log_t &event){
|
||||||
|
std::cerr << "[dtranslatebot] [LOG] " << event.message << std::endl;
|
||||||
|
});
|
||||||
|
|
||||||
bot::submit_queue submit_queue;
|
bot::submit_queue submit_queue;
|
||||||
std::thread submit_queue_loop(&bot::submit_queue::run, &submit_queue, &bot);
|
std::thread submit_queue_loop(&bot::submit_queue::run, &submit_queue, &bot);
|
||||||
|
|
|
@ -96,37 +96,51 @@ void process_preflang_settings(const dpp::json &json, std::vector<std::string> *
|
||||||
{
|
{
|
||||||
for (auto json_preferred_lang = json.begin(); json_preferred_lang != json.end(); json_preferred_lang++) {
|
for (auto json_preferred_lang = json.begin(); json_preferred_lang != json.end(); json_preferred_lang++) {
|
||||||
if (std::distance(json.begin(), json_preferred_lang) >= 25) {
|
if (std::distance(json.begin(), json_preferred_lang) >= 25) {
|
||||||
std::cerr << "[dtranslatebot] Error: preferred_lang is limited to 25 languages" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] preferred_lang is limited to 25 languages" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
preferred_langs->push_back(*json_preferred_lang);
|
preferred_langs->push_back(*json_preferred_lang);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void process_user_settings(const dpp::json &json, uint16_t *avatar_size)
|
||||||
|
{
|
||||||
|
auto json_avatar_size = json.find("avatar_size");
|
||||||
|
if (json_avatar_size != json.end()) {
|
||||||
|
*avatar_size = *json_avatar_size;
|
||||||
|
if (*avatar_size < 16)
|
||||||
|
*avatar_size = 16;
|
||||||
|
else if (*avatar_size > 4096)
|
||||||
|
*avatar_size = 4096;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*avatar_size = 256;
|
||||||
|
}
|
||||||
|
|
||||||
bool process_translator_settings(const dpp::json &json, translator *translator)
|
bool process_translator_settings(const dpp::json &json, translator *translator)
|
||||||
{
|
{
|
||||||
if (!json.is_object()) {
|
if (!json.is_object()) {
|
||||||
std::cerr << "[dtranslatebot] Error: Translator settings needs to be in a object" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] Translator settings needs to be in a object" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto json_translate_hostname = json.find("hostname");
|
auto json_translate_hostname = json.find("hostname");
|
||||||
if (json_translate_hostname == json.end()) {
|
if (json_translate_hostname == json.end()) {
|
||||||
std::cerr << "[dtranslatebot] Error: hostname can not be found in Translator settings" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] hostname can not be found in Translator settings" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
translator->hostname = *json_translate_hostname;
|
translator->hostname = *json_translate_hostname;
|
||||||
|
|
||||||
auto json_translate_port = json.find("port");
|
auto json_translate_port = json.find("port");
|
||||||
if (json_translate_port == json.end()) {
|
if (json_translate_port == json.end()) {
|
||||||
std::cerr << "[dtranslatebot] Error: port can not be found in Translator settings" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] port can not be found in Translator settings" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
translator->port = *json_translate_port;
|
translator->port = *json_translate_port;
|
||||||
|
|
||||||
auto json_translate_url = json.find("url");
|
auto json_translate_url = json.find("url");
|
||||||
if (json_translate_url == json.end()) {
|
if (json_translate_url == json.end()) {
|
||||||
std::cerr << "[dtranslatebot] Error: url can not be found in Translate settings" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] url can not be found in Translate settings" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
translator->url = *json_translate_url;
|
translator->url = *json_translate_url;
|
||||||
|
@ -258,7 +272,7 @@ std::unique_ptr<bot::translator::translator> settings::get_translator() const
|
||||||
{
|
{
|
||||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||||
std::unique_ptr<bot::translator::libretranslate> libretranslate(
|
std::unique_ptr<bot::translator::libretranslate> libretranslate(
|
||||||
new bot::translator::libretranslate(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey));
|
new bot::translator::libretranslate(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey));
|
||||||
return libretranslate;
|
return libretranslate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,15 +301,19 @@ void settings::lock()
|
||||||
bool settings::parse(const std::string &data)
|
bool settings::parse(const std::string &data)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
const dpp::json json = dpp::json::parse(data);
|
dpp::json json;
|
||||||
if (!json.is_object()) {
|
try {
|
||||||
std::cerr << "[dtranslatebot] Error: JSON configuration file is corrupt" << std::endl;
|
json = dpp::json::parse(data);
|
||||||
|
}
|
||||||
|
catch (const std::exception &exception) {
|
||||||
|
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl;
|
||||||
|
std::cerr << "[dtranslatebot] [ERROR] Exception while parsing JSON" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto json_token = json.find("token");
|
auto json_token = json.find("token");
|
||||||
if (json_token == json.end()) {
|
if (json_token == json.end()) {
|
||||||
std::cerr << "[dtranslatebot] Error: Bot token can not be found" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] Bot token can not be found" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,26 +329,15 @@ bool settings::parse(const std::string &data)
|
||||||
if (m_storagePath.empty())
|
if (m_storagePath.empty())
|
||||||
m_storagePath = std::filesystem::current_path();
|
m_storagePath = std::filesystem::current_path();
|
||||||
|
|
||||||
// In future we should allow more services here, for now it's only LibreTranslate
|
|
||||||
auto json_translator = json.find("translator");
|
auto json_translator = json.find("translator");
|
||||||
if (json_translator == json.end()) {
|
if (json_translator == json.end()) {
|
||||||
std::cerr << "[dtranslatebot] Error: Translator settings can not be found" << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] Translator settings can not be found" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!process_translator_settings(*json_translator, &m_translator))
|
if (!process_translator_settings(*json_translator, &m_translator))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto json_avatarSize = json.find("avatar_size");
|
m_avatarSize = 256;
|
||||||
if (json_avatarSize != json.end()) {
|
|
||||||
m_avatarSize = *json_avatarSize;
|
|
||||||
if (m_avatarSize < 16)
|
|
||||||
m_avatarSize = 16;
|
|
||||||
else if (m_avatarSize > 4096)
|
|
||||||
m_avatarSize = 4096;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
m_avatarSize = 256;
|
|
||||||
|
|
||||||
m_guilds.clear();
|
m_guilds.clear();
|
||||||
m_prefLangs.clear();
|
m_prefLangs.clear();
|
||||||
m_webhookIds.clear();
|
m_webhookIds.clear();
|
||||||
|
@ -343,10 +350,14 @@ bool settings::parse(const std::string &data)
|
||||||
if (json_preflangs != json.end() && json_preflangs->is_array())
|
if (json_preflangs != json.end() && json_preflangs->is_array())
|
||||||
process_preflang_settings(*json_preflangs, &m_prefLangs);
|
process_preflang_settings(*json_preflangs, &m_prefLangs);
|
||||||
|
|
||||||
|
auto json_user = json.find("user");
|
||||||
|
if (json_user != json.end() && json_user->is_object())
|
||||||
|
process_user_settings(*json_user, &m_avatarSize);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (const std::exception &exception) {
|
catch (const std::exception &exception) {
|
||||||
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
|
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -355,7 +366,7 @@ bool settings::parse_file(const std::string &filename)
|
||||||
{
|
{
|
||||||
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
|
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
|
||||||
if (!ifs.is_open()) {
|
if (!ifs.is_open()) {
|
||||||
std::cerr << "[dtranslatebot] Error: Failed to open JSON configuration file located at " << filename << std::endl;
|
std::cerr << "[dtranslatebot] [ERROR] Failed to open JSON configuration file located at " << filename << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,7 +60,7 @@ const std::vector<language> libretranslate::get_languages()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const std::exception &exception) {
|
catch (const std::exception &exception) {
|
||||||
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
|
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return languages;
|
return languages;
|
||||||
|
@ -94,7 +94,7 @@ const std::string libretranslate::translate(const std::string &text, const std::
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const std::exception &exception) {
|
catch (const std::exception &exception) {
|
||||||
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
|
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
|
|
|
@ -79,7 +79,7 @@ void bot::webhook_push::push_request(dpp::snowflake webhook_id, const std::strin
|
||||||
std::future<dpp::http_request_completion_t> _f = _p.get_future();
|
std::future<dpp::http_request_completion_t> _f = _p.get_future();
|
||||||
bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, [bot, &_p](dpp::json &json, const dpp::http_request_completion_t &event) {
|
bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, [bot, &_p](dpp::json &json, const dpp::http_request_completion_t &event) {
|
||||||
if (event.status != 204)
|
if (event.status != 204)
|
||||||
std::cerr << "[dtranslatebot] Warning: Webhook push returned unexpected code " << event.status << std::endl;
|
std::cerr << "[dtranslatebot] [WARNING] Webhook push returned unexpected code " << event.status << std::endl;
|
||||||
_p.set_value(event);
|
_p.set_value(event);
|
||||||
});
|
});
|
||||||
_f.wait();
|
_f.wait();
|
||||||
|
|
Loading…
Reference in a new issue