improve logging and some refactor

This commit is contained in:
Syping 2024-02-04 09:24:32 +01:00
parent 86351b9537
commit b09da9cfce
8 changed files with 112 additions and 82 deletions

View file

@ -20,15 +20,15 @@
"source": "en",
"target": {
"de": "https://discord.com/api/webhooks/$guild2_de_webhook_id/$guild2_de_webhook_token",
"fr": "https://discord.com/api/webhooks/$guild2_fr_webhook_id/$guild2_fr_webhook_token",
"ru": "https://discord.com/api/webhooks/$guild2_ru_webhook_id/$guild2_ru_webhook_token"
"fr": "https://discord.com/api/webhooks/$guild2_fr_webhook_id/$guild2_fr_webhook_token"
}
}
}
},
"preferred_lang": ["en", "de", "fr", "ru"],
"preferred_lang": ["en", "de", "fr", ...],
"storage": "$working_directory",
"token": "$bot_token",
"translate": {
"translator": {
"hostname": "127.0.0.1",
"port": 80,
"url": "/",

View file

@ -36,16 +36,16 @@ int main(int argc, char* argv[]) {
return 1;
if (settings.get_translator()->get_languages().empty()) {
std::cerr << "Failed to initialise translateable languages" << std::endl;
std::cerr << "[dtranslatebot] Error: Failed to initialise translateable languages" << std::endl;
return 2;
}
if (!std::filesystem::exists(settings.get_storage_path())) {
std::cerr << "Storage directory " << settings.get_storage_path() << " can not be found" << std::endl;
if (!std::filesystem::exists(settings.storage_path())) {
std::cerr << "[dtranslatebot] Error: Storage directory " << settings.storage_path() << " can not be found" << std::endl;
return 2;
}
dpp::cluster bot(settings.get_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(dpp::utility::cout_logger());
@ -77,9 +77,9 @@ int main(int argc, char* argv[]) {
if (message.author.empty())
message.author = event.msg.author.global_name;
message.avatar = event.msg.member.get_avatar_url(settings.get_avatar_size());
message.avatar = event.msg.member.get_avatar_url(settings.avatar_size());
if (message.avatar.empty())
message.avatar = event.msg.author.get_avatar_url(settings.get_avatar_size());
message.avatar = event.msg.author.get_avatar_url(settings.avatar_size());
message.message = event.msg.content;
message.source = channel->source;

View file

@ -38,7 +38,7 @@ void process_guild_settings(const dpp::json &json, std::vector<guild> *guilds, s
else if (json_guild_id->is_string())
guild.id = std::stoull(std::string(*json_guild_id));
else
throw std::invalid_argument("Guild id isvalue() not a number or a string");
throw std::invalid_argument("Guild id is not a number or a string");
}
else
guild.id = std::stoull(json_guild.key());
@ -92,41 +92,41 @@ void process_guild_settings(const dpp::json &json, std::vector<guild> *guilds, s
}
}
void process_preflang_settings(const dpp::json &json, std::vector<std::string> *preflangs)
void process_preflang_settings(const dpp::json &json, std::vector<std::string> *preferred_langs)
{
for (auto json_preflang = json.begin(); json_preflang != json.end(); json_preflang++) {
if (std::distance(json.begin(), json_preflang) >= 25) {
std::cerr << "\"preferred_lang\" is limited to 25 languages" << std::endl;
for (auto json_preferred_lang = json.begin(); json_preferred_lang != json.end(); json_preferred_lang++) {
if (std::distance(json.begin(), json_preferred_lang) >= 25) {
std::cerr << "[dtranslatebot] Error: preferred_lang is limited to 25 languages" << std::endl;
break;
}
preflangs->push_back(*json_preflang);
preferred_langs->push_back(*json_preferred_lang);
}
}
bool process_translator_settings(const dpp::json &json, translator *translator)
{
if (!json.is_object()) {
std::cerr << "Translate settings needs to be in a object" << std::endl;
std::cerr << "[dtranslatebot] Error: Translator settings needs to be in a object" << std::endl;
return false;
}
auto json_translate_hostname = json.find("hostname");
if (json_translate_hostname == json.end()) {
std::cerr << "\"hostname\" can not be found in Translate settings" << std::endl;
std::cerr << "[dtranslatebot] Error: hostname can not be found in Translator settings" << std::endl;
return false;
}
translator->hostname = *json_translate_hostname;
auto json_translate_port = json.find("port");
if (json_translate_port == json.end()) {
std::cerr << "\"port\" can not be found in Translate settings" << std::endl;
std::cerr << "[dtranslatebot] Error: port can not be found in Translator settings" << std::endl;
return false;
}
translator->port = *json_translate_port;
auto json_translate_url = json.find("url");
if (json_translate_url == json.end()) {
std::cerr << "\"url\" can not be found in Translate settings" << std::endl;
std::cerr << "[dtranslatebot] Error: url can not be found in Translate settings" << std::endl;
return false;
}
translator->url = *json_translate_url;
@ -148,6 +148,7 @@ bool process_translator_settings(const dpp::json &json, translator *translator)
void settings::add_channel(const channel &channel, dpp::snowflake guild_id)
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
guild->channel.push_back(channel);
@ -164,6 +165,7 @@ void settings::add_channel(const channel &channel, dpp::snowflake guild_id)
bool settings::add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id)
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
@ -180,16 +182,24 @@ bool settings::add_target(const target &target, dpp::snowflake guild_id, dpp::sn
void settings::add_translatebot_webhook(dpp::snowflake webhook_id)
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
m_webhookIds.push_back(webhook_id);
}
uint16_t settings::get_avatar_size()
uint16_t settings::avatar_size()
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
return m_avatarSize;
}
const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id)
const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id) const
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_channel(const guild*, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id)
return &(*channel);
@ -197,8 +207,14 @@ const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_
return nullptr;
}
const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_channel(dpp::snowflake, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
@ -211,8 +227,14 @@ const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake cha
return nullptr;
}
const guild* settings::get_guild(dpp::snowflake guild_id)
const guild* settings::get_guild(dpp::snowflake guild_id) const
{
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_guild(dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id)
return &(*guild);
@ -220,22 +242,19 @@ const guild* settings::get_guild(dpp::snowflake guild_id)
return nullptr;
}
const std::vector<std::string> settings::get_preferred_languages()
const std::vector<std::string> settings::preferred_languages() const
{
return m_preflangs;
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
return m_prefLangs;
}
const std::filesystem::path settings::get_storage_path()
const std::filesystem::path settings::storage_path() const
{
return m_storagepath;
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
return m_storagePath;
}
const translator* settings::get_translate()
{
return &m_translator;
}
std::unique_ptr<bot::translator::translator> settings::get_translator()
std::unique_ptr<bot::translator::translator> settings::get_translator() const
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
std::unique_ptr<bot::translator::libretranslate> libretranslate(
@ -243,13 +262,15 @@ std::unique_ptr<bot::translator::translator> settings::get_translator()
return libretranslate;
}
const std::string settings::get_token()
const std::string settings::token() const
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
return m_token;
}
bool settings::is_translatebot(dpp::snowflake webhook_id)
bool settings::is_translatebot(dpp::snowflake webhook_id) const
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (auto id = m_webhookIds.begin(); id != m_webhookIds.end(); id++) {
if (*id == webhook_id)
return true;
@ -260,6 +281,7 @@ bool settings::is_translatebot(dpp::snowflake webhook_id)
void settings::lock()
{
m_mutex.lock();
m_externallyLockedCount++;
}
bool settings::parse(const std::string &data)
@ -267,13 +289,13 @@ bool settings::parse(const std::string &data)
try {
const dpp::json json = dpp::json::parse(data);
if (!json.is_object()) {
std::cerr << "JSON configuration file is corrupt" << std::endl;
std::cerr << "[dtranslatebot] Error: JSON configuration file is corrupt" << std::endl;
return false;
}
auto json_token = json.find("token");
if (json_token == json.end()) {
std::cerr << "Bot token can not be found" << std::endl;
std::cerr << "[dtranslatebot] Error: Bot token can not be found" << std::endl;
return false;
}
@ -282,17 +304,17 @@ bool settings::parse(const std::string &data)
auto json_storage = json.find("storage");
if (json_storage != json.end())
m_storagepath = std::string(*json_storage);
m_storagePath = std::string(*json_storage);
else if (char *storagepath = getenv("DTRANSLATEBOT_STORAGE"))
m_storagepath = storagepath;
m_storagePath = storagepath;
if (m_storagepath.empty())
m_storagepath = std::filesystem::current_path();
if (m_storagePath.empty())
m_storagePath = std::filesystem::current_path();
// In future we should allow more services here, for now it's only LibreTranslate
auto json_translator = json.find("translator");
if (json_translator == json.end()) {
std::cerr << "Translator settings can not be found" << std::endl;
std::cerr << "[dtranslatebot] Error: Translator settings can not be found" << std::endl;
return false;
}
if (!process_translator_settings(*json_translator, &m_translator))
@ -310,7 +332,7 @@ bool settings::parse(const std::string &data)
m_avatarSize = 256;
m_guilds.clear();
m_preflangs.clear();
m_prefLangs.clear();
m_webhookIds.clear();
auto json_guilds = json.find("guilds");
@ -319,15 +341,12 @@ bool settings::parse(const std::string &data)
auto json_preflangs = json.find("preferred_lang");
if (json_preflangs != json.end() && json_preflangs->is_array())
process_preflang_settings(*json_preflangs, &m_preflangs);
process_preflang_settings(*json_preflangs, &m_prefLangs);
return true;
}
catch (const dpp::json::exception &exception) {
std::cerr << "Exception thrown while parsing configuration: " << exception.what() << std::endl;
}
catch (const std::exception &exception) {
std::cerr << "Exception thrown while parsing configuration: " << exception.what() << std::endl;
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
}
return false;
}
@ -336,7 +355,7 @@ bool settings::parse_file(const std::string &filename)
{
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
std::cerr << "Failed to open JSON configuration file located at " << filename << std::endl;
std::cerr << "[dtranslatebot] Error: Failed to open JSON configuration file located at " << filename << std::endl;
return false;
}
@ -349,4 +368,5 @@ bool settings::parse_file(const std::string &filename)
void settings::unlock()
{
m_mutex.unlock();
m_externallyLockedCount--;
}

View file

@ -52,31 +52,40 @@ namespace bot {
class settings {
public:
/* add functions */
void add_channel(const channel &channel, dpp::snowflake guild_id);
bool add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id);
void add_translatebot_webhook(dpp::snowflake webhook_id);
uint16_t get_avatar_size();
const channel* get_channel(const guild *guild, dpp::snowflake channel_id);
const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
const guild* get_guild(dpp::snowflake guild_id);
const std::vector<std::string> get_preferred_languages();
const std::filesystem::path get_storage_path();
const translator* get_translate();
std::unique_ptr<bot::translator::translator> get_translator();
const std::string get_token();
bool is_translatebot(dpp::snowflake webhook_id);
/* get functions */
uint16_t avatar_size();
const channel* get_channel(const guild *guild, dpp::snowflake channel_id) const;
const channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const;
const guild* get_guild(dpp::snowflake guild_id) const;
const std::vector<std::string> preferred_languages() const;
const std::filesystem::path storage_path() const;
std::unique_ptr<bot::translator::translator> get_translator() const;
const std::string token() const;
/* is functions */
bool is_translatebot(dpp::snowflake webhook_id) const;
/* lock functions */
void lock();
bool parse(const std::string &data);
bool parse_file(const std::string &filename);
void unlock();
/* parse functions */
bool parse(const std::string &data);
bool parse_file(const std::string &filename);
private:
mutable std::recursive_mutex m_mutex;
size_t m_externallyLockedCount;
uint16_t m_avatarSize;
std::recursive_mutex m_mutex;
std::vector<guild> m_guilds;
std::vector<std::string> m_preflangs;
std::filesystem::path m_storagepath;
translator m_translator;
std::vector<std::string> m_prefLangs;
std::filesystem::path m_storagePath;
bot::settings::translator m_translator;
std::string m_token;
std::vector<dpp::snowflake> m_webhookIds;
};

View file

@ -17,6 +17,7 @@
*****************************************************************************/
#include "slashcommands.h"
using namespace std::string_literals;
void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
{
@ -59,7 +60,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
bot->create_webhook(webhook, [&bot, &settings, event, source, target](const dpp::confirmation_callback_t &callback) {
if (callback.is_error()) {
event.reply(dpp::message("Failed to generate webhook!\n" + callback.http_info.body).set_flags(dpp::m_ephemeral));
event.reply(dpp::message("Failed to generate webhook!").set_flags(dpp::m_ephemeral));
return;
}
const dpp::webhook webhook = callback.get<dpp::webhook>();
@ -114,8 +115,8 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
}
}
catch (const std::exception &exception) {
std::cerr << "Failed to process command /" << event.command.get_command_name() << ": " << exception.what() << std::endl;
event.reply(dpp::message("Failed to process command /" + event.command.get_command_name() + "\n" + exception.what()).set_flags(dpp::m_ephemeral));
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
event.reply(dpp::message("Exception while processing command:\n"s + exception.what()).set_flags(dpp::m_ephemeral));
}
}
@ -123,7 +124,7 @@ void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::set
{
settings->lock();
const std::vector<bot::translator::language> languages = settings->get_translator()->get_languages();
const std::vector<std::string> preferred_languages = settings->get_preferred_languages();
const std::vector<std::string> preferred_languages = settings->preferred_languages();
settings->unlock();
std::vector<dpp::slashcommand> commands;

View file

@ -16,7 +16,9 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#ifndef NDEBUG
#include <iostream>
#endif
#include "translator_core.h"
using namespace bot::translator;
@ -30,12 +32,16 @@ translator::~translator()
const std::vector<language> translator::get_languages()
{
std::cerr << "WARNING: translator::get_languages() have being called." << std::endl;
#ifndef NDEBUG
std::cerr << "[DEBUG] translator::get_languages() have being called." << std::endl;
#endif
return {};
}
const std::string translator::translate(const std::string &text, const std::string &source, const std::string &target)
{
std::cerr << "WARNING: translator:translate(const std::string&, const std::string&, const std::string&) have being called." << std::endl;
#ifndef NDEBUG
std::cerr << "[DEBUG] translator:translate(const std::string&, const std::string&, const std::string&) have being called." << std::endl;
#endif
return {};
}

View file

@ -59,11 +59,8 @@ const std::vector<language> libretranslate::get_languages()
}
}
}
catch (const dpp::json::exception &exception) {
std::cerr << "Exception thrown while parsing supported languages JSON: " << exception.what() << std::endl;
}
catch (const std::exception &exception) {
std::cerr << "Exception thrown while getting supported languages: " << exception.what() << std::endl;
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
}
return languages;
@ -96,11 +93,8 @@ const std::string libretranslate::translate(const std::string &text, const std::
}
}
}
catch (const dpp::json::exception &exception) {
std::cerr << "Exception thrown while parsing translated JSON: " << exception.what() << std::endl;
}
catch (const std::exception &exception) {
std::cerr << "Exception thrown while translating: " << exception.what() << std::endl;
std::cerr << "[dtranslatebot] Exception: " << exception.what() << std::endl;
}
return text;

View file

@ -62,7 +62,7 @@ void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster
if (message_v.length() <= 2000) {
json_body["content"] = message_v;
message_v = ""sv;
message_v = {};
push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot);
}
}
@ -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();
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)
std::cerr << "Webhook push returned unexpected code " << event.status << " with response: " << event.body << std::endl;
std::cerr << "[dtranslatebot] Warning: Webhook push returned unexpected code " << event.status << std::endl;
_p.set_value(event);
});
_f.wait();