mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 05:40:23 +01:00
code optimisations and potential unique_ptr leak fix
This commit is contained in:
parent
eb301aee1c
commit
f871441b78
7 changed files with 48 additions and 36 deletions
|
@ -140,7 +140,7 @@ int main(int argc, char* argv[]) {
|
||||||
bot::settings::target s_target;
|
bot::settings::target s_target;
|
||||||
s_target.target = target;
|
s_target.target = target;
|
||||||
s_target.webhook = webhook;
|
s_target.webhook = webhook;
|
||||||
s_channel.targets.push_back(s_target);
|
s_channel.targets.emplace_back(s_target);
|
||||||
|
|
||||||
settings.lock();
|
settings.lock();
|
||||||
settings.add_channel(s_channel, event.command.guild_id);
|
settings.add_channel(s_channel, event.command.guild_id);
|
||||||
|
@ -158,7 +158,7 @@ int main(int argc, char* argv[]) {
|
||||||
bot::settings::target s_target;
|
bot::settings::target s_target;
|
||||||
s_target.target = target;
|
s_target.target = target;
|
||||||
s_target.webhook = *webhook;
|
s_target.webhook = *webhook;
|
||||||
s_channel.targets.push_back(s_target);
|
s_channel.targets.emplace_back(s_target);
|
||||||
|
|
||||||
settings.lock();
|
settings.lock();
|
||||||
settings.add_channel(s_channel, event.command.guild_id);
|
settings.add_channel(s_channel, event.command.guild_id);
|
||||||
|
@ -217,7 +217,7 @@ int main(int argc, char* argv[]) {
|
||||||
webhook_subcommand.add_option(webhook_option);
|
webhook_subcommand.add_option(webhook_option);
|
||||||
command_translate.add_option(channel_subcommand);
|
command_translate.add_option(channel_subcommand);
|
||||||
command_translate.add_option(webhook_subcommand);
|
command_translate.add_option(webhook_subcommand);
|
||||||
commands.push_back(command_translate);
|
commands.emplace_back(command_translate);
|
||||||
|
|
||||||
if (preferred_languages.size() > 1) {
|
if (preferred_languages.size() > 1) {
|
||||||
dpp::slashcommand command_translate_pref("translate_pref", "Translate current channel (Preferred languages)", bot.me.id);
|
dpp::slashcommand command_translate_pref("translate_pref", "Translate current channel (Preferred languages)", bot.me.id);
|
||||||
|
@ -240,7 +240,7 @@ int main(int argc, char* argv[]) {
|
||||||
webhook_pref_subcommand.add_option(webhook_option);
|
webhook_pref_subcommand.add_option(webhook_option);
|
||||||
command_translate_pref.add_option(channel_pref_subcommand);
|
command_translate_pref.add_option(channel_pref_subcommand);
|
||||||
command_translate_pref.add_option(webhook_pref_subcommand);
|
command_translate_pref.add_option(webhook_pref_subcommand);
|
||||||
commands.push_back(command_translate_pref);
|
commands.emplace_back(command_translate_pref);
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.global_bulk_command_create(commands);
|
bot.global_bulk_command_create(commands);
|
||||||
|
|
|
@ -16,8 +16,9 @@
|
||||||
* responsible for anything with use of the software, you are self responsible.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include <dpp/dpp.h>
|
#include <dpp/json.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "translate_libretranslate.h"
|
#include "translate_libretranslate.h"
|
||||||
|
@ -34,8 +35,8 @@ void bot::settings::settings::add_channel(const bot::settings::channel &channel,
|
||||||
// We will create the guild structure when it is not in memory
|
// We will create the guild structure when it is not in memory
|
||||||
bot::settings::guild guild;
|
bot::settings::guild guild;
|
||||||
guild.id = guild_id;
|
guild.id = guild_id;
|
||||||
guild.channel.push_back(channel);
|
guild.channel.emplace_back(channel);
|
||||||
m_guilds.push_back(guild);
|
m_guilds.emplace_back(guild);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bot::settings::settings::add_translatebot_webhook(const dpp::webhook &webhook)
|
void bot::settings::settings::add_translatebot_webhook(const dpp::webhook &webhook)
|
||||||
|
@ -65,6 +66,7 @@ const bot::settings::channel* bot::settings::settings::get_channel(dpp::snowflak
|
||||||
if (channel->id == channel_id)
|
if (channel->id == channel_id)
|
||||||
return &(*channel);
|
return &(*channel);
|
||||||
}
|
}
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -92,8 +94,8 @@ const bot::settings::translate* bot::settings::settings::get_translate()
|
||||||
std::unique_ptr<bot::translate::translator> bot::settings::settings::get_translator()
|
std::unique_ptr<bot::translate::translator> bot::settings::settings::get_translator()
|
||||||
{
|
{
|
||||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||||
std::unique_ptr<bot::translate::translator> libretranslate(
|
std::unique_ptr<bot::translate::libretranslate> libretranslate(
|
||||||
new bot::translate::libretranslate(m_translate.hostname, m_translate.port, m_translate.url, m_translate.tls, m_translate.apiKey));
|
new bot::translate::libretranslate(m_translate.hostname, m_translate.port, m_translate.url, m_translate.tls, m_translate.apiKey));
|
||||||
return libretranslate;
|
return libretranslate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +248,7 @@ bool bot::settings::settings::parse(const std::string &filename)
|
||||||
bot::settings::target target;
|
bot::settings::target target;
|
||||||
target.target = json_channel_target.value();
|
target.target = json_channel_target.value();
|
||||||
target.webhook = dpp::webhook(json_channel->at("webhook"));
|
target.webhook = dpp::webhook(json_channel->at("webhook"));
|
||||||
channel.targets.push_back(target);
|
channel.targets.emplace_back(target);
|
||||||
m_webhookIds.push_back(target.webhook.id);
|
m_webhookIds.push_back(target.webhook.id);
|
||||||
}
|
}
|
||||||
else if (json_channel_target.value().is_object()) {
|
else if (json_channel_target.value().is_object()) {
|
||||||
|
@ -254,17 +256,17 @@ bool bot::settings::settings::parse(const std::string &filename)
|
||||||
bot::settings::target target;
|
bot::settings::target target;
|
||||||
target.target = json_target.key();
|
target.target = json_target.key();
|
||||||
target.webhook = dpp::webhook(json_target.value());
|
target.webhook = dpp::webhook(json_target.value());
|
||||||
channel.targets.push_back(target);
|
channel.targets.emplace_back(target);
|
||||||
m_webhookIds.push_back(target.webhook.id);
|
m_webhookIds.push_back(target.webhook.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!channel.source.empty() && !channel.targets.empty())
|
if (!channel.source.empty() && !channel.targets.empty())
|
||||||
guild.channel.push_back(channel);
|
guild.channel.emplace_back(channel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_guilds.push_back(guild);
|
m_guilds.emplace_back(guild);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,10 @@ bot::translate::translator::translator()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bot::translate::translator::~translator()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
const std::vector<bot::translate::language> bot::translate::translator::get_languages()
|
const std::vector<bot::translate::language> bot::translate::translator::get_languages()
|
||||||
{
|
{
|
||||||
std::cerr << "WARNING: translator::get_languages() have being called." << std::endl;
|
std::cerr << "WARNING: translator::get_languages() have being called." << std::endl;
|
||||||
|
|
|
@ -32,6 +32,7 @@ namespace bot {
|
||||||
class translator {
|
class translator {
|
||||||
public:
|
public:
|
||||||
explicit translator();
|
explicit translator();
|
||||||
|
virtual ~translator();
|
||||||
virtual const std::vector<bot::translate::language> get_languages();
|
virtual const std::vector<bot::translate::language> get_languages();
|
||||||
virtual const std::string translate(const std::string &text, const std::string &source, const std::string &target);
|
virtual const std::string translate(const std::string &text, const std::string &source, const std::string &target);
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,12 +19,17 @@
|
||||||
#include <dpp/json.h>
|
#include <dpp/json.h>
|
||||||
#include <dpp/httpsclient.h>
|
#include <dpp/httpsclient.h>
|
||||||
#include "translate_libretranslate.h"
|
#include "translate_libretranslate.h"
|
||||||
|
using namespace std::string_literals;
|
||||||
|
|
||||||
bot::translate::libretranslate::libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey) :
|
bot::translate::libretranslate::libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey) :
|
||||||
m_hostname(hostname), m_port(port), m_url(url), m_tls(tls), m_apiKey(apiKey)
|
m_hostname(hostname), m_port(port), m_url(url), m_tls(tls), m_apiKey(apiKey)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bot::translate::libretranslate::~libretranslate()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
const std::vector<bot::translate::language> bot::translate::libretranslate::get_languages()
|
const std::vector<bot::translate::language> bot::translate::libretranslate::get_languages()
|
||||||
{
|
{
|
||||||
std::vector<bot::translate::language> languages;
|
std::vector<bot::translate::language> languages;
|
||||||
|
@ -34,20 +39,20 @@ const std::vector<bot::translate::language> bot::translate::libretranslate::get_
|
||||||
if (http_request.get_status() == 200) {
|
if (http_request.get_status() == 200) {
|
||||||
const dpp::json response = dpp::json::parse(http_request.get_content());
|
const dpp::json response = dpp::json::parse(http_request.get_content());
|
||||||
if (response.is_array()) {
|
if (response.is_array()) {
|
||||||
for (auto json_language = response.begin(); json_language != response.end(); json_language++) {
|
for (const auto &json_language : response) {
|
||||||
if (json_language->is_object()) {
|
if (json_language.is_object()) {
|
||||||
bot::translate::language language;
|
bot::translate::language language;
|
||||||
|
|
||||||
auto json_lang_code = json_language.value().find("code");
|
auto json_lang_code = json_language.find("code");
|
||||||
if (json_lang_code != json_language.value().end())
|
if (json_lang_code != json_language.end())
|
||||||
language.code = json_lang_code.value();
|
language.code = json_lang_code.value();
|
||||||
|
|
||||||
auto json_lang_name = json_language.value().find("name");
|
auto json_lang_name = json_language.find("name");
|
||||||
if (json_lang_name != json_language.value().end())
|
if (json_lang_name != json_language.end())
|
||||||
language.name = json_lang_name.value();
|
language.name = json_lang_name.value();
|
||||||
|
|
||||||
if (!language.code.empty() && !language.name.empty())
|
if (!language.code.empty() && !language.name.empty())
|
||||||
languages.push_back(language);
|
languages.emplace_back(language);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,14 +71,14 @@ const std::vector<bot::translate::language> bot::translate::libretranslate::get_
|
||||||
const std::string bot::translate::libretranslate::translate(const std::string &text, const std::string &source, const std::string &target)
|
const std::string bot::translate::libretranslate::translate(const std::string &text, const std::string &source, const std::string &target)
|
||||||
{
|
{
|
||||||
const dpp::http_headers http_headers = {
|
const dpp::http_headers http_headers = {
|
||||||
{"Content-Type", "application/json"}
|
{"Content-Type"s, "application/json"s}
|
||||||
};
|
};
|
||||||
|
|
||||||
dpp::json json_body = {
|
dpp::json json_body = {
|
||||||
{"q", text},
|
{"q"s, text},
|
||||||
{"source", source},
|
{"source"s, source},
|
||||||
{"target", target},
|
{"target"s, target},
|
||||||
{"format", "text"}
|
{"format"s, "text"s}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!m_apiKey.empty())
|
if (!m_apiKey.empty())
|
||||||
|
|
|
@ -28,8 +28,9 @@ namespace bot {
|
||||||
class libretranslate : public translator {
|
class libretranslate : public translator {
|
||||||
public:
|
public:
|
||||||
explicit libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey = {});
|
explicit libretranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string apiKey = {});
|
||||||
const std::vector<bot::translate::language> get_languages();
|
~libretranslate() override;
|
||||||
const std::string translate(const std::string &text, const std::string &source, const std::string &target);
|
const std::vector<bot::translate::language> get_languages() override;
|
||||||
|
const std::string translate(const std::string &text, const std::string &source, const std::string &target) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_apiKey;
|
std::string m_apiKey;
|
||||||
|
|
|
@ -19,12 +19,14 @@
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include "webhook_push.h"
|
#include "webhook_push.h"
|
||||||
|
using namespace std::string_literals;
|
||||||
|
using namespace std::string_view_literals;
|
||||||
|
|
||||||
void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster *bot)
|
void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster *bot)
|
||||||
{
|
{
|
||||||
dpp::json json_body = {
|
dpp::json json_body = {
|
||||||
{"username", message.author},
|
{"username"s, message.author},
|
||||||
{"avatar_url", message.avatar}
|
{"avatar_url"s, message.avatar}
|
||||||
};
|
};
|
||||||
|
|
||||||
// We will split too long messages into multiple messages
|
// We will split too long messages into multiple messages
|
||||||
|
@ -39,18 +41,15 @@ void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
std::cmatch match;
|
std::cmatch match;
|
||||||
const std::regex eos_regex("^.*(\\.|\\?|\\!|\\。)\\s.*$");
|
if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*(\\.|\\?|\\!|\\。)\\s.*$"s))) {
|
||||||
const std::regex eop_regex("^.*(\\,)\\s.*$");
|
|
||||||
const std::regex eow_regex("^.*()\\s.*$");
|
|
||||||
if (std::regex_match(message_eov.begin(), message_eov.end(), match, eos_regex)) {
|
|
||||||
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
||||||
message_v = message_v.substr(1334 + match.position(1));
|
message_v = message_v.substr(1334 + match.position(1));
|
||||||
}
|
}
|
||||||
else if (std::regex_match(message_eov.begin(), message_eov.end(), match, eop_regex)) {
|
else if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*(\\,)\\s.*$"s))) {
|
||||||
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
||||||
message_v = message_v.substr(1334 + match.position(1));
|
message_v = message_v.substr(1334 + match.position(1));
|
||||||
}
|
}
|
||||||
else if (std::regex_match(message_eov.begin(), message_eov.end(), match, eow_regex)) {
|
else if (std::regex_match(message_eov.begin(), message_eov.end(), match, std::regex("^.*()\\s.*$"s))) {
|
||||||
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
||||||
message_v = message_v.substr(1334 + match.position(1));
|
message_v = message_v.substr(1334 + match.position(1));
|
||||||
}
|
}
|
||||||
|
@ -63,7 +62,7 @@ void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster
|
||||||
|
|
||||||
if (message_v.length() <= 2000) {
|
if (message_v.length() <= 2000) {
|
||||||
json_body["content"] = message_v;
|
json_body["content"] = message_v;
|
||||||
message_v = std::string_view();
|
message_v = ""sv;
|
||||||
push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot);
|
push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue