From e09293cc4ea68b9886bb2696a459e022416dd8e0 Mon Sep 17 00:00:00 2001 From: Syping Date: Fri, 12 Jan 2024 00:09:18 +0100 Subject: [PATCH] allow setting multiple targets per channel --- etc/dtranslatebot.json | 14 ++++++++-- src/main.cpp | 3 +-- src/message_queue.cpp | 60 ++++++++++++++++++++++-------------------- src/message_queue.h | 5 +--- src/settings.cpp | 26 +++++++++++++----- src/settings.h | 3 +-- 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/etc/dtranslatebot.json b/etc/dtranslatebot.json index f57284d..6a46836 100644 --- a/etc/dtranslatebot.json +++ b/etc/dtranslatebot.json @@ -5,12 +5,22 @@ "channel_1": { "source": "en", "target": "de", - "webhook": "https://..." + "webhook": "https://german.webhook" }, "channel_2": { "source": "de", "target": "en", - "webhook": "https://..." + "webhook": "https://english.webhook" + } + }, + "guild_2": { + "channel_1": { + "source": "en", + "target": { + "de": "https://german.webhook", + "fr": "https://french.webhook", + "ru": "https://russian.webhook" + } } } }, diff --git a/src/main.cpp b/src/main.cpp index 1374798..d82b786 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,9 +52,8 @@ int main(int argc, char* argv[]) { message.author = event.msg.author.format_username(); message.avatar = event.msg.author.avatar.to_string(); message.message = event.msg.content; - message.webhook = channel->webhook; message.source = channel->source; - message.target = channel->target; + message.targets = channel->targets; message_queue.add(message); } } diff --git a/src/message_queue.cpp b/src/message_queue.cpp index 58a3147..0197bee 100644 --- a/src/message_queue.cpp +++ b/src/message_queue.cpp @@ -40,45 +40,47 @@ void bot::message_queue::run(dpp::cluster *bot, bot::settings::settings *setting settings->lock(); bot::settings::translate *translate = settings->get_translate(); - - dpp::json body = { - {"q", message.message}, - {"source", message.source}, - {"target", message.target}, - {"format", "text"}, - }; - - if (!translate->apiKey.empty()) - body.emplace("apiKey", translate->apiKey); - + const std::string tr_apiKey = translate->apiKey; const std::string tr_hostname = translate->hostname; const uint16_t tr_port = translate->port; const std::string tr_url = translate->url; const bool tr_tls = translate->tls; settings->unlock(); - dpp::http_headers http_headers; - http_headers.emplace("Content-Type", "application/json"); + for (auto target = message.targets.begin(); target != message.targets.end(); target++) { + dpp::json json_body = { + {"q", message.message}, + {"source", message.source}, + {"target", target->first}, + {"format", "text"}, + }; - std::string tr_message = message.message; - dpp::https_client http_request(tr_hostname, tr_port, tr_url, "POST", body.dump(), http_headers, !tr_tls); - if (http_request.get_status() == 200) { - dpp::json response = dpp::json::parse(http_request.get_content()); - if (response.is_object()) { - auto tr_text = response.find("translatedText"); - if (tr_text != response.end()) - tr_message = tr_text.value(); + if (!tr_apiKey.empty()) + json_body.emplace("apiKey", tr_apiKey); + + dpp::http_headers http_headers; + http_headers.emplace("Content-Type", "application/json"); + + std::string tr_message = message.message; + dpp::https_client http_request(tr_hostname, tr_port, tr_url, "POST", json_body.dump(), http_headers, !tr_tls); + if (http_request.get_status() == 200) { + dpp::json response = dpp::json::parse(http_request.get_content()); + if (response.is_object()) { + auto tr_text = response.find("translatedText"); + if (tr_text != response.end()) + tr_message = tr_text.value(); + } } - } - dpp::webhook webhook(message.webhook); - webhook.name = message.author; + dpp::webhook webhook(target->second); + webhook.name = message.author; - try { - bot->execute_webhook_sync(webhook, dpp::message(tr_message)); - } - catch (dpp::rest_exception exception) { - std::cerr << "REST Error: " << exception.what() << std::endl; + try { + bot->execute_webhook_sync(webhook, dpp::message(tr_message)); + } + catch (dpp::rest_exception &exception) { + std::cerr << "REST Error: " << exception.what() << std::endl; + } } std::this_thread::yield(); diff --git a/src/message_queue.h b/src/message_queue.h index f608ff9..8f0ba21 100644 --- a/src/message_queue.h +++ b/src/message_queue.h @@ -29,11 +29,8 @@ namespace bot { std::string author; std::string avatar; std::string message; - /* Webhook URL */ - std::string webhook; - /* Translation Parameters */ std::string source; - std::string target; + std::vector> targets; }; class message_queue { diff --git a/src/settings.cpp b/src/settings.cpp index 1a5ea3e..3aed006 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "settings.h" bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id) @@ -87,6 +88,11 @@ bool bot::settings::settings::parse(const std::string &filename) return false; } + if (!json_translate->is_object()) { + std::cerr << "Translate settings needs to be in a object" << std::endl; + return false; + } + auto json_translate_hostname = json_translate.value().find("hostname"); if (json_translate_hostname == json_translate.value().end()) { std::cerr << "\"hostname\" can not be found in Translate settings" << std::endl; @@ -135,14 +141,20 @@ bool bot::settings::settings::parse(const std::string &filename) channel.source = json_channel_source.value(); auto json_channel_target = json_channel.value().find("target"); - if (json_channel_target != json_channel.value().end()) - channel.target = json_channel_target.value(); + if (json_channel_target != json_channel.value().end()) { + if (json_channel_target.value().is_string()) { + const std::string target = json_channel_target.value(); + const std::string webhook = json_channel->at("webhook"); + channel.targets.push_back(std::make_pair(target, webhook)); + } + else if (json_channel_target.value().is_object()) { + for (auto json_target = json_channel_target.value().begin(); json_target != json_channel_target.value().end(); json_target++) { + channel.targets.push_back(std::make_pair(json_target.key(), json_target.value())); + } + } + } - auto json_channel_webhook = json_channel.value().find("webhook"); - if (json_channel_webhook != json_channel.value().end()) - channel.webhook = json_channel_webhook.value(); - - if (!channel.source.empty() && !channel.target.empty() && !channel.webhook.empty()) + if (!channel.source.empty() && !channel.targets.empty()) guild.channel.push_back(channel); } m_guilds.push_back(guild); diff --git a/src/settings.h b/src/settings.h index 187aceb..6f9cc4b 100644 --- a/src/settings.h +++ b/src/settings.h @@ -28,8 +28,7 @@ namespace bot { struct channel { uint64_t id; std::string source; - std::string target; - std::string webhook; + std::vector> targets; }; struct guild { uint64_t id;