mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-25 07:10:23 +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)
|
||||
{
|
||||
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");
|
||||
if (json_translate_hostname == json.end()) {
|
||||
std::cerr << "[Error] Value hostname not found in translator object" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (json_translate_hostname != json.end())
|
||||
translator->hostname = *json_translate_hostname;
|
||||
|
||||
auto json_translate_port = json.find("port");
|
||||
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;
|
||||
else
|
||||
translator->hostname = {};
|
||||
|
||||
auto json_translate_tls = json.find("tls");
|
||||
if (json_translate_tls != json.end())
|
||||
|
@ -208,6 +236,24 @@ bool process_translator_settings(const dpp::json &json, translator *translator)
|
|||
else
|
||||
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");
|
||||
if (json_translate_apiKey != json.end())
|
||||
translator->apiKey = *json_translate_apiKey;
|
||||
|
@ -395,7 +441,7 @@ void settings::lock()
|
|||
m_externallyLockedCount++;
|
||||
}
|
||||
|
||||
bool settings::parse(const std::string &data)
|
||||
bool settings::parse(const std::string &data, bool initialize)
|
||||
{
|
||||
try {
|
||||
dpp::json json;
|
||||
|
@ -417,7 +463,6 @@ bool settings::parse(const std::string &data)
|
|||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||
m_token = *json_token;
|
||||
|
||||
// Support more Database systems in the future
|
||||
std::filesystem::path storage_path;
|
||||
auto json_storage = json.find("storage");
|
||||
if (json_storage != json.end())
|
||||
|
@ -439,9 +484,6 @@ bool settings::parse(const std::string &data)
|
|||
return false;
|
||||
|
||||
m_avatarSize = 256;
|
||||
m_guilds.clear();
|
||||
m_prefLangs.clear();
|
||||
m_webhookIds.clear();
|
||||
|
||||
auto json_guilds = json.find("guilds");
|
||||
if (json_guilds != json.end() && json_guilds->is_object())
|
||||
|
@ -465,7 +507,7 @@ bool settings::parse(const std::string &data)
|
|||
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);
|
||||
if (!ifs.is_open()) {
|
||||
|
@ -476,7 +518,7 @@ bool settings::parse_file(const std::string &filename)
|
|||
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
|
||||
ifs.close();
|
||||
|
||||
return parse(sdata);
|
||||
return parse(sdata, initialize);
|
||||
}
|
||||
|
||||
void settings::unlock()
|
||||
|
|
|
@ -56,8 +56,8 @@ namespace bot {
|
|||
void unlock();
|
||||
|
||||
/* parse functions */
|
||||
bool parse(const std::string &data);
|
||||
bool parse_file(const std::string &filename);
|
||||
bool parse(const std::string &data, bool initialize = true);
|
||||
bool parse_file(const std::string &filename, bool initialize = true);
|
||||
|
||||
private:
|
||||
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.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)) {
|
||||
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.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)) {
|
||||
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()) {
|
||||
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));
|
||||
}
|
||||
|
||||
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()) {
|
||||
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_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_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_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_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(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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue