allow setting multiple targets per channel

This commit is contained in:
Syping 2024-01-12 00:09:18 +01:00
parent acbab3c97b
commit e09293cc4e
6 changed files with 65 additions and 46 deletions

View file

@ -5,12 +5,22 @@
"channel_1": { "channel_1": {
"source": "en", "source": "en",
"target": "de", "target": "de",
"webhook": "https://..." "webhook": "https://german.webhook"
}, },
"channel_2": { "channel_2": {
"source": "de", "source": "de",
"target": "en", "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"
}
} }
} }
}, },

View file

@ -52,9 +52,8 @@ int main(int argc, char* argv[]) {
message.author = event.msg.author.format_username(); message.author = event.msg.author.format_username();
message.avatar = event.msg.author.avatar.to_string(); message.avatar = event.msg.author.avatar.to_string();
message.message = event.msg.content; message.message = event.msg.content;
message.webhook = channel->webhook;
message.source = channel->source; message.source = channel->source;
message.target = channel->target; message.targets = channel->targets;
message_queue.add(message); message_queue.add(message);
} }
} }

View file

@ -40,45 +40,47 @@ void bot::message_queue::run(dpp::cluster *bot, bot::settings::settings *setting
settings->lock(); settings->lock();
bot::settings::translate *translate = settings->get_translate(); bot::settings::translate *translate = settings->get_translate();
const std::string tr_apiKey = translate->apiKey;
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_hostname = translate->hostname; const std::string tr_hostname = translate->hostname;
const uint16_t tr_port = translate->port; const uint16_t tr_port = translate->port;
const std::string tr_url = translate->url; const std::string tr_url = translate->url;
const bool tr_tls = translate->tls; const bool tr_tls = translate->tls;
settings->unlock(); settings->unlock();
dpp::http_headers http_headers; for (auto target = message.targets.begin(); target != message.targets.end(); target++) {
http_headers.emplace("Content-Type", "application/json"); dpp::json json_body = {
{"q", message.message},
{"source", message.source},
{"target", target->first},
{"format", "text"},
};
std::string tr_message = message.message; if (!tr_apiKey.empty())
dpp::https_client http_request(tr_hostname, tr_port, tr_url, "POST", body.dump(), http_headers, !tr_tls); json_body.emplace("apiKey", tr_apiKey);
if (http_request.get_status() == 200) {
dpp::json response = dpp::json::parse(http_request.get_content()); dpp::http_headers http_headers;
if (response.is_object()) { http_headers.emplace("Content-Type", "application/json");
auto tr_text = response.find("translatedText");
if (tr_text != response.end()) std::string tr_message = message.message;
tr_message = tr_text.value(); 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); dpp::webhook webhook(target->second);
webhook.name = message.author; webhook.name = message.author;
try { try {
bot->execute_webhook_sync(webhook, dpp::message(tr_message)); bot->execute_webhook_sync(webhook, dpp::message(tr_message));
} }
catch (dpp::rest_exception exception) { catch (dpp::rest_exception &exception) {
std::cerr << "REST Error: " << exception.what() << std::endl; std::cerr << "REST Error: " << exception.what() << std::endl;
}
} }
std::this_thread::yield(); std::this_thread::yield();

View file

@ -29,11 +29,8 @@ namespace bot {
std::string author; std::string author;
std::string avatar; std::string avatar;
std::string message; std::string message;
/* Webhook URL */
std::string webhook;
/* Translation Parameters */
std::string source; std::string source;
std::string target; std::vector<std::pair<std::string,std::string>> targets;
}; };
class message_queue { class message_queue {

View file

@ -19,6 +19,7 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <mutex> #include <mutex>
#include <iostream> #include <iostream>
#include <utility>
#include "settings.h" #include "settings.h"
bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id) 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; 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"); auto json_translate_hostname = json_translate.value().find("hostname");
if (json_translate_hostname == json_translate.value().end()) { if (json_translate_hostname == json_translate.value().end()) {
std::cerr << "\"hostname\" can not be found in Translate settings" << std::endl; 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(); channel.source = json_channel_source.value();
auto json_channel_target = json_channel.value().find("target"); auto json_channel_target = json_channel.value().find("target");
if (json_channel_target != json_channel.value().end()) if (json_channel_target != json_channel.value().end()) {
channel.target = json_channel_target.value(); 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 (!channel.source.empty() && !channel.targets.empty())
if (json_channel_webhook != json_channel.value().end())
channel.webhook = json_channel_webhook.value();
if (!channel.source.empty() && !channel.target.empty() && !channel.webhook.empty())
guild.channel.push_back(channel); guild.channel.push_back(channel);
} }
m_guilds.push_back(guild); m_guilds.push_back(guild);

View file

@ -28,8 +28,7 @@ namespace bot {
struct channel { struct channel {
uint64_t id; uint64_t id;
std::string source; std::string source;
std::string target; std::vector<std::pair<std::string,std::string>> targets;
std::string webhook;
}; };
struct guild { struct guild {
uint64_t id; uint64_t id;