mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 13:50:22 +01:00
simplify configuration and some code improvements
This commit is contained in:
parent
8884fd2876
commit
12e81ed24c
4 changed files with 76 additions and 34 deletions
|
@ -174,6 +174,49 @@ void process_user_settings(const dpp::json &json, uint16_t *avatar_size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void process_url(const std::string &url, translator *translator)
|
||||||
|
{
|
||||||
|
std::string_view url_v = url;
|
||||||
|
if (url_v.substr(0, 7) == "http://") {
|
||||||
|
translator->tls = false;
|
||||||
|
if (!translator->port)
|
||||||
|
translator->port = 80;
|
||||||
|
url_v = url_v.substr(7);
|
||||||
|
}
|
||||||
|
else if (url_v.substr(0, 8) == "https://") {
|
||||||
|
translator->tls = true;
|
||||||
|
if (!translator->port)
|
||||||
|
translator->port = 443;
|
||||||
|
url_v = url_v.substr(8);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
translator->tls = false;
|
||||||
|
if (!translator->port)
|
||||||
|
translator->port = 80;
|
||||||
|
}
|
||||||
|
auto slash_pos = url_v.find_first_of('/');
|
||||||
|
if (slash_pos != std::string_view::npos) {
|
||||||
|
translator->url = url_v.substr(slash_pos);
|
||||||
|
url_v = url_v.substr(0, slash_pos);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
translator->url = "/";
|
||||||
|
url_v = url_v.substr(0, slash_pos);
|
||||||
|
}
|
||||||
|
auto colon_pos = url_v.find_first_of(':');
|
||||||
|
if (colon_pos != std::string_view::npos) {
|
||||||
|
translator->hostname = url_v.substr(0, colon_pos);
|
||||||
|
const int port = std::stoi(std::string(url_v.substr(colon_pos + 1)));
|
||||||
|
if (port > 0 && port < 65536)
|
||||||
|
translator->port = static_cast<uint16_t>(port);
|
||||||
|
else
|
||||||
|
throw std::invalid_argument("Port is out of range");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
translator->hostname = url_v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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()) {
|
||||||
|
@ -182,25 +225,10 @@ bool process_translator_settings(const dpp::json &json, translator *translator)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 << "[Error] Value hostname not found in translator object" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
translator->hostname = *json_translate_hostname;
|
translator->hostname = *json_translate_hostname;
|
||||||
|
else
|
||||||
auto json_translate_port = json.find("port");
|
translator->hostname = {};
|
||||||
if (json_translate_port == json.end()) {
|
|
||||||
std::cerr << "[Error] Value port not found in translator object" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
translator->port = *json_translate_port;
|
|
||||||
|
|
||||||
auto json_translate_url = json.find("url");
|
|
||||||
if (json_translate_url == json.end()) {
|
|
||||||
std::cerr << "[Error] Value url not found in translator object" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
translator->url = *json_translate_url;
|
|
||||||
|
|
||||||
auto json_translate_tls = json.find("tls");
|
auto json_translate_tls = json.find("tls");
|
||||||
if (json_translate_tls != json.end())
|
if (json_translate_tls != json.end())
|
||||||
|
@ -208,6 +236,24 @@ bool process_translator_settings(const dpp::json &json, translator *translator)
|
||||||
else
|
else
|
||||||
translator->tls = false;
|
translator->tls = false;
|
||||||
|
|
||||||
|
auto json_translate_port = json.find("port");
|
||||||
|
if (json_translate_port != json.end())
|
||||||
|
translator->port = *json_translate_port;
|
||||||
|
else
|
||||||
|
translator->port = 0;
|
||||||
|
|
||||||
|
auto json_translate_url = json.find("url");
|
||||||
|
if (json_translate_url == json.end()) {
|
||||||
|
std::cerr << "[Error] Value url not found in translator object" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (translator->hostname.empty()) {
|
||||||
|
process_url(*json_translate_url, translator);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
translator->url = *json_translate_url;
|
||||||
|
}
|
||||||
|
|
||||||
auto json_translate_apiKey = json.find("apiKey");
|
auto json_translate_apiKey = json.find("apiKey");
|
||||||
if (json_translate_apiKey != json.end())
|
if (json_translate_apiKey != json.end())
|
||||||
translator->apiKey = *json_translate_apiKey;
|
translator->apiKey = *json_translate_apiKey;
|
||||||
|
@ -395,7 +441,7 @@ void settings::lock()
|
||||||
m_externallyLockedCount++;
|
m_externallyLockedCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool settings::parse(const std::string &data)
|
bool settings::parse(const std::string &data, bool initialize)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
dpp::json json;
|
dpp::json json;
|
||||||
|
@ -417,7 +463,6 @@ bool settings::parse(const std::string &data)
|
||||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||||
m_token = *json_token;
|
m_token = *json_token;
|
||||||
|
|
||||||
// Support more Database systems in the future
|
|
||||||
std::filesystem::path storage_path;
|
std::filesystem::path storage_path;
|
||||||
auto json_storage = json.find("storage");
|
auto json_storage = json.find("storage");
|
||||||
if (json_storage != json.end())
|
if (json_storage != json.end())
|
||||||
|
@ -439,9 +484,6 @@ bool settings::parse(const std::string &data)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_avatarSize = 256;
|
m_avatarSize = 256;
|
||||||
m_guilds.clear();
|
|
||||||
m_prefLangs.clear();
|
|
||||||
m_webhookIds.clear();
|
|
||||||
|
|
||||||
auto json_guilds = json.find("guilds");
|
auto json_guilds = json.find("guilds");
|
||||||
if (json_guilds != json.end() && json_guilds->is_object())
|
if (json_guilds != json.end() && json_guilds->is_object())
|
||||||
|
@ -465,7 +507,7 @@ bool settings::parse(const std::string &data)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool settings::parse_file(const std::string &filename)
|
bool settings::parse_file(const std::string &filename, bool initialize)
|
||||||
{
|
{
|
||||||
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()) {
|
||||||
|
@ -476,7 +518,7 @@ bool settings::parse_file(const std::string &filename)
|
||||||
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
|
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
|
||||||
ifs.close();
|
ifs.close();
|
||||||
|
|
||||||
return parse(sdata);
|
return parse(sdata, initialize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void settings::unlock()
|
void settings::unlock()
|
||||||
|
|
|
@ -56,8 +56,8 @@ namespace bot {
|
||||||
void unlock();
|
void unlock();
|
||||||
|
|
||||||
/* parse functions */
|
/* parse functions */
|
||||||
bool parse(const std::string &data);
|
bool parse(const std::string &data, bool initialize = true);
|
||||||
bool parse_file(const std::string &filename);
|
bool parse_file(const std::string &filename, bool initialize = true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable std::recursive_mutex m_mutex;
|
mutable std::recursive_mutex m_mutex;
|
||||||
|
|
|
@ -271,7 +271,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
|
||||||
webhook.guild_id = channel->guild_id;
|
webhook.guild_id = channel->guild_id;
|
||||||
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
|
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
|
||||||
|
|
||||||
bot->create_webhook(webhook, std::bind(&bot::slashcommands::process_translate_webhook_new_channel, bot, settings, event, source, target, std::placeholders::_1));
|
bot->create_webhook(webhook, std::bind(&bot::slashcommands::process_translate_webhook_new_channel, settings, event, source, target, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
|
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
|
||||||
const bot::settings::target s_target = { target, *webhook };
|
const bot::settings::target s_target = { target, *webhook };
|
||||||
|
@ -298,7 +298,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
|
||||||
webhook.guild_id = channel->guild_id;
|
webhook.guild_id = channel->guild_id;
|
||||||
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
|
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
|
||||||
|
|
||||||
bot->create_webhook(webhook, std::bind(&bot::slashcommands::process_translate_webhook_add_target, bot, settings, event, target, std::placeholders::_1));
|
bot->create_webhook(webhook, std::bind(&bot::slashcommands::process_translate_webhook_add_target, settings, event, target, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
|
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
|
||||||
const bot::settings::target s_target = { target, *webhook };
|
const bot::settings::target s_target = { target, *webhook };
|
||||||
|
@ -333,7 +333,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bot::slashcommands::process_translate_webhook_add_target(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &target, const dpp::confirmation_callback_t &callback)
|
void bot::slashcommands::process_translate_webhook_add_target(bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &target, const dpp::confirmation_callback_t &callback)
|
||||||
{
|
{
|
||||||
if (callback.is_error()) {
|
if (callback.is_error()) {
|
||||||
event.reply(dpp::message("Failed to generate webhook!").set_flags(dpp::m_ephemeral));
|
event.reply(dpp::message("Failed to generate webhook!").set_flags(dpp::m_ephemeral));
|
||||||
|
@ -354,7 +354,7 @@ void bot::slashcommands::process_translate_webhook_add_target(dpp::cluster *bot,
|
||||||
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
||||||
}
|
}
|
||||||
|
|
||||||
void bot::slashcommands::process_translate_webhook_new_channel(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &source, const std::string &target, const dpp::confirmation_callback_t &callback)
|
void bot::slashcommands::process_translate_webhook_new_channel(bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &source, const std::string &target, const dpp::confirmation_callback_t &callback)
|
||||||
{
|
{
|
||||||
if (callback.is_error()) {
|
if (callback.is_error()) {
|
||||||
event.reply(dpp::message("Failed to generate webhook!").set_flags(dpp::m_ephemeral));
|
event.reply(dpp::message("Failed to generate webhook!").set_flags(dpp::m_ephemeral));
|
||||||
|
|
|
@ -29,8 +29,8 @@ namespace bot {
|
||||||
extern void process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback);
|
extern void process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback);
|
||||||
extern void process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
extern void process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
||||||
extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
||||||
extern void process_translate_webhook_add_target(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &target, const dpp::confirmation_callback_t &callback);
|
extern void process_translate_webhook_add_target(bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &target, const dpp::confirmation_callback_t &callback);
|
||||||
extern void process_translate_webhook_new_channel(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &source, const std::string &target, const dpp::confirmation_callback_t &callback);
|
extern void process_translate_webhook_new_channel(bot::settings::settings *settings, const dpp::slashcommand_t &event, const std::string &source, const std::string &target, const dpp::confirmation_callback_t &callback);
|
||||||
extern void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
|
extern void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue