mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 05:40:23 +01:00
allow setting multiple targets per channel
This commit is contained in:
parent
acbab3c97b
commit
e09293cc4e
6 changed files with 65 additions and 46 deletions
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<std::pair<std::string,std::string>> targets;
|
||||
};
|
||||
|
||||
class message_queue {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <dpp/dpp.h>
|
||||
#include <mutex>
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#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);
|
||||
|
|
|
@ -28,8 +28,7 @@ namespace bot {
|
|||
struct channel {
|
||||
uint64_t id;
|
||||
std::string source;
|
||||
std::string target;
|
||||
std::string webhook;
|
||||
std::vector<std::pair<std::string,std::string>> targets;
|
||||
};
|
||||
struct guild {
|
||||
uint64_t id;
|
||||
|
|
Loading…
Reference in a new issue