output refactor

This commit is contained in:
Syping 2024-02-04 10:12:19 +01:00
parent 7e3696ce90
commit e2f3739fa1
6 changed files with 23 additions and 25 deletions

View file

@ -36,19 +36,19 @@ int main(int argc, char* argv[]) {
return 1; return 1;
if (settings.get_translator()->get_languages().empty()) { if (settings.get_translator()->get_languages().empty()) {
std::cerr << "[dtranslatebot] [ERROR] Failed to initialise translateable languages" << std::endl; std::cerr << "[Error] Failed to initialise translateable languages" << std::endl;
return 2; return 2;
} }
if (!std::filesystem::exists(settings.storage_path())) { if (!std::filesystem::exists(settings.storage_path())) {
std::cerr << "[dtranslatebot] [ERROR] Storage directory " << settings.storage_path() << " can not be found" << std::endl; std::cerr << "[Error] Storage directory " << settings.storage_path() << " can not be found" << std::endl;
return 2; return 2;
} }
dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content); dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
bot.on_log([&bot](const dpp::log_t &event){ bot.on_log([&bot](const dpp::log_t &event){
std::cerr << "[dtranslatebot] [LOG] " << event.message << std::endl; std::cerr << "[Log] " << event.message << std::endl;
}); });
bot::submit_queue submit_queue; bot::submit_queue submit_queue;

View file

@ -96,7 +96,7 @@ void process_preflang_settings(const dpp::json &json, std::vector<std::string> *
{ {
for (auto json_preferred_lang = json.begin(); json_preferred_lang != json.end(); json_preferred_lang++) { for (auto json_preferred_lang = json.begin(); json_preferred_lang != json.end(); json_preferred_lang++) {
if (std::distance(json.begin(), json_preferred_lang) >= 25) { if (std::distance(json.begin(), json_preferred_lang) >= 25) {
std::cerr << "[dtranslatebot] [ERROR] preferred_lang is limited to 25 languages" << std::endl; std::cerr << "[Error] Value preferred_lang is limited to 25 languages" << std::endl;
break; break;
} }
preferred_langs->push_back(*json_preferred_lang); preferred_langs->push_back(*json_preferred_lang);
@ -113,34 +113,32 @@ void process_user_settings(const dpp::json &json, uint16_t *avatar_size)
else if (*avatar_size > 4096) else if (*avatar_size > 4096)
*avatar_size = 4096; *avatar_size = 4096;
} }
else
*avatar_size = 256;
} }
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()) {
std::cerr << "[dtranslatebot] [ERROR] Translator settings needs to be in a object" << std::endl; std::cerr << "[Error] Value translator needs to be a object" << std::endl;
return false; return false;
} }
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 << "[dtranslatebot] [ERROR] hostname can not be found in Translator settings" << std::endl; std::cerr << "[Error] Value hostname not found in translator object" << std::endl;
return false; return false;
} }
translator->hostname = *json_translate_hostname; translator->hostname = *json_translate_hostname;
auto json_translate_port = json.find("port"); auto json_translate_port = json.find("port");
if (json_translate_port == json.end()) { if (json_translate_port == json.end()) {
std::cerr << "[dtranslatebot] [ERROR] port can not be found in Translator settings" << std::endl; std::cerr << "[Error] Value port not found in translator object" << std::endl;
return false; return false;
} }
translator->port = *json_translate_port; translator->port = *json_translate_port;
auto json_translate_url = json.find("url"); auto json_translate_url = json.find("url");
if (json_translate_url == json.end()) { if (json_translate_url == json.end()) {
std::cerr << "[dtranslatebot] [ERROR] url can not be found in Translate settings" << std::endl; std::cerr << "[Error] Value url not found in translator object" << std::endl;
return false; return false;
} }
translator->url = *json_translate_url; translator->url = *json_translate_url;
@ -210,7 +208,7 @@ const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_
{ {
if (!m_externallyLockedCount) { if (!m_externallyLockedCount) {
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_channel(const guild*, dpp::snowflake) have being called without settings being locked." << std::endl; std::cerr << "[Debug] settings::get_channel(const guild*, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif #endif
return nullptr; return nullptr;
} }
@ -225,7 +223,7 @@ const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake cha
{ {
if (!m_externallyLockedCount) { if (!m_externallyLockedCount) {
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_channel(dpp::snowflake, dpp::snowflake) have being called without settings being locked." << std::endl; std::cerr << "[Debug] settings::get_channel(dpp::snowflake, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif #endif
return nullptr; return nullptr;
} }
@ -245,7 +243,7 @@ const guild* settings::get_guild(dpp::snowflake guild_id) const
{ {
if (!m_externallyLockedCount) { if (!m_externallyLockedCount) {
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_guild(dpp::snowflake) have being called without settings being locked." << std::endl; std::cerr << "[Debug] settings::get_guild(dpp::snowflake) have being called without settings being locked." << std::endl;
#endif #endif
return nullptr; return nullptr;
} }
@ -306,14 +304,14 @@ bool settings::parse(const std::string &data)
json = dpp::json::parse(data); json = dpp::json::parse(data);
} }
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl; std::cerr << "[Exception] " << exception.what() << std::endl;
std::cerr << "[dtranslatebot] [ERROR] Exception while parsing JSON" << std::endl; std::cerr << "[Error] Exception while parsing JSON" << std::endl;
return false; return false;
} }
auto json_token = json.find("token"); auto json_token = json.find("token");
if (json_token == json.end()) { if (json_token == json.end()) {
std::cerr << "[dtranslatebot] [ERROR] Bot token can not be found" << std::endl; std::cerr << "[Error] Value token not found" << std::endl;
return false; return false;
} }
@ -331,7 +329,7 @@ bool settings::parse(const std::string &data)
auto json_translator = json.find("translator"); auto json_translator = json.find("translator");
if (json_translator == json.end()) { if (json_translator == json.end()) {
std::cerr << "[dtranslatebot] [ERROR] Translator settings can not be found" << std::endl; std::cerr << "[Error] Value translator not found" << std::endl;
return false; return false;
} }
if (!process_translator_settings(*json_translator, &m_translator)) if (!process_translator_settings(*json_translator, &m_translator))
@ -357,7 +355,7 @@ bool settings::parse(const std::string &data)
return true; return true;
} }
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl; std::cerr << "[Exception] " << exception.what() << std::endl;
} }
return false; return false;
} }
@ -366,7 +364,7 @@ bool settings::parse_file(const std::string &filename)
{ {
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()) {
std::cerr << "[dtranslatebot] [ERROR] Failed to open JSON configuration file located at " << filename << std::endl; std::cerr << "[Error] Failed to open JSON configuration file located at " << filename << std::endl;
return false; return false;
} }

View file

@ -115,7 +115,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
} }
} }
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl; std::cerr << "[Exception] " << exception.what() << std::endl;
event.reply(dpp::message("Exception while processing command:\n"s + exception.what()).set_flags(dpp::m_ephemeral)); event.reply(dpp::message("Exception while processing command:\n"s + exception.what()).set_flags(dpp::m_ephemeral));
} }
} }

View file

@ -33,7 +33,7 @@ translator::~translator()
const std::vector<language> translator::get_languages() const std::vector<language> translator::get_languages()
{ {
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << "[DEBUG] translator::get_languages() have being called." << std::endl; std::cerr << "[Debug] translator::get_languages() have being called." << std::endl;
#endif #endif
return {}; return {};
} }
@ -41,7 +41,7 @@ const std::vector<language> translator::get_languages()
const std::string translator::translate(const std::string &text, const std::string &source, const std::string &target) const std::string translator::translate(const std::string &text, const std::string &source, const std::string &target)
{ {
#ifndef NDEBUG #ifndef NDEBUG
std::cerr << "[DEBUG] translator:translate(const std::string&, const std::string&, const std::string&) have being called." << std::endl; std::cerr << "[Debug] translator:translate(const std::string&, const std::string&, const std::string&) have being called." << std::endl;
#endif #endif
return {}; return {};
} }

View file

@ -60,7 +60,7 @@ const std::vector<language> libretranslate::get_languages()
} }
} }
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl; std::cerr << "[Exception] " << exception.what() << std::endl;
} }
return languages; return languages;
@ -94,7 +94,7 @@ const std::string libretranslate::translate(const std::string &text, const std::
} }
} }
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl; std::cerr << "[Exception] " << exception.what() << std::endl;
} }
return text; return text;

View file

@ -79,7 +79,7 @@ void bot::webhook_push::push_request(dpp::snowflake webhook_id, const std::strin
std::future<dpp::http_request_completion_t> _f = _p.get_future(); std::future<dpp::http_request_completion_t> _f = _p.get_future();
bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, [bot, &_p](dpp::json &json, const dpp::http_request_completion_t &event) { bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, [bot, &_p](dpp::json &json, const dpp::http_request_completion_t &event) {
if (event.status != 204) if (event.status != 204)
std::cerr << "[dtranslatebot] [WARNING] Webhook push returned unexpected code " << event.status << std::endl; std::cerr << "[Warning] Webhook push returned unexpected code " << event.status << std::endl;
_p.set_value(event); _p.set_value(event);
}); });
_f.wait(); _f.wait();