From 94429ca718d6490adc4ff299372c52920d69584b Mon Sep 17 00:00:00 2001 From: Syping Date: Mon, 11 Mar 2024 04:10:18 +0100 Subject: [PATCH] code style changes and remove some constructors --- src/core/database.h | 2 ++ src/core/message_queue.cpp | 2 +- src/core/slashcommands.cpp | 31 +++++++++++++++-------------- src/core/slashcommands.h | 24 +++++++++++++---------- src/core/submit_queue.cpp | 2 +- src/core/translator.h | 2 ++ src/core/webhook_push.h | 1 + src/database/file/file.cpp | 40 +++++++++++++++++++------------------- src/database/file/file.h | 6 +++--- 9 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/core/database.h b/src/core/database.h index e22f92e..2589f46 100644 --- a/src/core/database.h +++ b/src/core/database.h @@ -31,6 +31,8 @@ namespace bot { class database { public: explicit database() = default; + database(const database&) = delete; + database& operator=(const database&) = delete; virtual ~database() = default; virtual void add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const bot::settings::target &target); virtual void delete_channel(dpp::snowflake guild_id, dpp::snowflake channel_id); diff --git a/src/core/message_queue.cpp b/src/core/message_queue.cpp index fffb71a..a4f1085 100644 --- a/src/core/message_queue.cpp +++ b/src/core/message_queue.cpp @@ -19,7 +19,7 @@ #include #include "message_queue.h" #include "settings.h" -using namespace bot; +using bot::message_queue; using namespace std::chrono_literals; void message_queue::add(const message &message) diff --git a/src/core/slashcommands.cpp b/src/core/slashcommands.cpp index 37ca996..b1bbc21 100644 --- a/src/core/slashcommands.cpp +++ b/src/core/slashcommands.cpp @@ -18,19 +18,20 @@ #include #include "slashcommands.h" +using bot::slashcommands; using namespace std::string_literals; -void bot::slashcommands::process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) +void slashcommands::process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { if (event.command.get_command_name() == "edit") - bot::slashcommands::process_edit_command(bot, settings, event); + slashcommands::process_edit_command(bot, settings, event); else if (event.command.get_command_name() == "list") - bot::slashcommands::process_list_command(bot, settings, event); + slashcommands::process_list_command(bot, settings, event); else if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") - bot::slashcommands::process_translate_command(bot, settings, event); + slashcommands::process_translate_command(bot, settings, event); } -void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) +void slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { try { dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); @@ -57,7 +58,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: } for (auto target = channel->targets.begin(); target != channel->targets.end();) { if (std::find(targets.begin(), targets.end(), target->target) != targets.end()) { - bot->delete_webhook(target->webhook.id, std::bind(&bot::slashcommands::process_deleted_webhook, settings, target->webhook.id, std::placeholders::_1)); + bot->delete_webhook(target->webhook.id, std::bind(&slashcommands::process_deleted_webhook, settings, target->webhook.id, std::placeholders::_1)); target = channel->targets.erase(target); } else { @@ -89,7 +90,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: if (target_found) { for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) { if (_target->target == target) { - bot->delete_webhook(_target->webhook.id, std::bind(&bot::slashcommands::process_deleted_webhook, settings, _target->webhook.id, std::placeholders::_1)); + bot->delete_webhook(_target->webhook.id, std::bind(&slashcommands::process_deleted_webhook, settings, _target->webhook.id, std::placeholders::_1)); channel->targets.erase(_target); break; } @@ -166,7 +167,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: } } -void bot::slashcommands::process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback) +void slashcommands::process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback) { if (callback.is_error()) { std::cerr << "[Error] Failed to delete Webhook " << webhook_id << std::endl; @@ -175,7 +176,7 @@ void bot::slashcommands::process_deleted_webhook(bot::settings::settings *settin settings->erase_translatebot_webhook(webhook_id); } -void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) +void slashcommands::process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { try { dpp::command_interaction interaction = event.command.get_command_interaction(); @@ -273,7 +274,7 @@ void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings:: } } -void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) +void slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event) { try { dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); @@ -317,7 +318,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, settings, event, source, target, std::placeholders::_1)); + bot->create_webhook(webhook, std::bind(&slashcommands::process_translate_webhook_new_channel, settings, event, source, target, std::placeholders::_1)); } else if (dpp::webhook *webhook = std::get_if(&v_target)) { const bot::settings::target s_target = { target, *webhook }; @@ -344,7 +345,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, settings, event, target, std::placeholders::_1)); + bot->create_webhook(webhook, std::bind(&slashcommands::process_translate_webhook_add_target, settings, event, target, std::placeholders::_1)); } else if (dpp::webhook *webhook = std::get_if(&v_target)) { const bot::settings::target s_target = { target, *webhook }; @@ -379,7 +380,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti } } -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) +void 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)); @@ -400,7 +401,7 @@ void bot::slashcommands::process_translate_webhook_add_target(bot::settings::set event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral)); } -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) +void 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)); @@ -423,7 +424,7 @@ void bot::slashcommands::process_translate_webhook_new_channel(bot::settings::se event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral)); } -void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::settings *settings) +void slashcommands::register_commands(dpp::cluster *bot, bot::settings::settings *settings) { settings->lock(); const std::vector languages = settings->get_translator()->get_languages(); diff --git a/src/core/slashcommands.h b/src/core/slashcommands.h index abcb0be..a31d37f 100644 --- a/src/core/slashcommands.h +++ b/src/core/slashcommands.h @@ -23,16 +23,20 @@ #include "settings.h" namespace bot { - namespace slashcommands { - extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); - extern void process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); - 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(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); - } + class slashcommands { + public: + slashcommands() = delete; + static void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); + static void register_commands(dpp::cluster *bot, bot::settings::settings *settings); + + private: + static void process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); + static void process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback); + static void process_list_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); + static void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); + static 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); + static 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); + }; } #endif // SLASHCOMMANDS_H diff --git a/src/core/submit_queue.cpp b/src/core/submit_queue.cpp index e752fab..12ce874 100644 --- a/src/core/submit_queue.cpp +++ b/src/core/submit_queue.cpp @@ -19,7 +19,7 @@ #include #include "submit_queue.h" #include "webhook_push.h" -using namespace bot; +using bot::submit_queue; using namespace std::chrono_literals; void submit_queue::add(const translated_message &message) diff --git a/src/core/translator.h b/src/core/translator.h index b4f14c0..c230abd 100644 --- a/src/core/translator.h +++ b/src/core/translator.h @@ -33,6 +33,8 @@ namespace bot { public: explicit translator() = default; virtual ~translator() = default; + translator(const translator&) = delete; + translator& operator=(const translator&) = delete; virtual const std::vector get_languages(); virtual const std::string translate(const std::string &text, const std::string &source, const std::string &target); }; diff --git a/src/core/webhook_push.h b/src/core/webhook_push.h index 643b20d..d4b03f9 100644 --- a/src/core/webhook_push.h +++ b/src/core/webhook_push.h @@ -26,6 +26,7 @@ namespace bot { class webhook_push { public: + webhook_push() = delete; static void run(const bot::translated_message &message, dpp::cluster *bot); private: diff --git a/src/database/file/file.cpp b/src/database/file/file.cpp index 31c4cfb..e7c6a6d 100644 --- a/src/database/file/file.cpp +++ b/src/database/file/file.cpp @@ -98,7 +98,7 @@ void file::add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); channel.targets.push_back(target); cache_add_channel(guild_id, channel_id); guild->channel.push_back(std::move(channel)); @@ -107,7 +107,7 @@ void file::add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); channel.targets.push_back(target); cache_add_channel(guild_id, channel_id); m_dataCache.push_back({ guild_id, { std::move(channel) } }); @@ -141,7 +141,7 @@ void file::delete_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) } std::vector channels; - cache_guild(guild_id, &channels); + cache_guild(guild_id, channels); for (auto channel = channels.begin(); channel != channels.end(); channel++) { if (*channel == channel_id) { channels.erase(channel); @@ -175,7 +175,7 @@ void file::delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); for (auto _target = channel.targets.begin(); _target != channel.targets.end(); _target++) { if (_target->target == target) { channel.targets.erase(_target); @@ -188,7 +188,7 @@ void file::delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); for (auto _target = channel.targets.begin(); _target != channel.targets.end(); _target++) { if (_target->target == target) { channel.targets.erase(_target); @@ -266,7 +266,7 @@ bot::settings::channel file::get_channel(dpp::snowflake guild_id, dpp::snowflake } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); return channel; } @@ -281,7 +281,7 @@ std::vector file::get_channels(dpp::snowflake guild_id) } std::vector channels; - cache_guild(guild_id, &channels); + cache_guild(guild_id, channels); return channels; } @@ -330,7 +330,7 @@ std::vector file::get_guilds() { const std::lock_guard guard(m_mutex); std::vector guilds; - list_guilds(&guilds); + list_guilds(guilds); return guilds; } @@ -348,7 +348,7 @@ void file::set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); channel.source = source; cache_add_channel(guild_id, channel_id); guild->channel.push_back(std::move(channel)); @@ -357,7 +357,7 @@ void file::set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id } bot::settings::channel channel; - cache_get_channel(channel_id, &channel); + cache_get_channel(channel_id, channel); channel.source = source; cache_add_channel(guild_id, channel_id); m_dataCache.push_back({ guild_id, { std::move(channel) } }); @@ -390,16 +390,16 @@ void file::cache_add_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) } std::vector channels; - cache_guild(guild_id, &channels); + cache_guild(guild_id, channels); if (std::find(channels.begin(), channels.end(), channel_id) == channels.end()) channels.push_back(channel_id); m_channelCache.push_back({ guild_id, std::move(channels) }); } -void file::cache_get_channel(dpp::snowflake channel_id, bot::settings::channel *channel) +void file::cache_get_channel(dpp::snowflake channel_id, settings::channel &channel) { - channel->id = channel_id; + channel.id = channel_id; const std::filesystem::path channel_file = m_storagePath / "channel" / (std::to_string(channel_id) + ".json"); @@ -418,7 +418,7 @@ void file::cache_get_channel(dpp::snowflake channel_id, bot::settings::channel * if (json.is_object()) { auto json_channel_source = json.find("source"); if (json_channel_source != json.end()) - channel->source = *json_channel_source; + channel.source = *json_channel_source; auto json_channel_target = json.find("target"); if (json_channel_target != json.end()) { @@ -433,7 +433,7 @@ void file::cache_get_channel(dpp::snowflake channel_id, bot::settings::channel * else if (json_target->is_string()) { target.webhook = dpp::webhook(*json_target); } - channel->targets.push_back(std::move(target)); + channel.targets.push_back(std::move(target)); } } } @@ -444,7 +444,7 @@ void file::cache_get_channel(dpp::snowflake channel_id, bot::settings::channel * } } -void file::cache_guild(dpp::snowflake guild_id, std::vector *channels) +void file::cache_guild(dpp::snowflake guild_id, std::vector &channels) { const std::filesystem::path guild_file = m_storagePath / "guild" / (std::to_string(guild_id) + ".json"); @@ -463,9 +463,9 @@ void file::cache_guild(dpp::snowflake guild_id, std::vector *cha if (json.is_array()) { for (auto channel = json.begin(); channel != json.end(); channel++) { if (channel->is_number()) - channels->push_back(*channel); + channels.push_back(*channel); else if (channel->is_string()) - channels->push_back(std::stoull(std::string(*channel))); + channels.push_back(std::stoull(std::string(*channel))); } } } @@ -474,7 +474,7 @@ void file::cache_guild(dpp::snowflake guild_id, std::vector *cha } } -void file::list_guilds(std::vector *guilds) +void file::list_guilds(std::vector &guilds) { const std::filesystem::path guild_dir = m_storagePath / "guild"; @@ -488,7 +488,7 @@ void file::list_guilds(std::vector *guilds) if (std::all_of(guild_filename.begin(), guild_filename.end(), ::isdigit)) { try { dpp::snowflake guild_id = std::stoull(guild_filename); - guilds->push_back(guild_id); + guilds.push_back(guild_id); } catch (const std::exception &exception) { std::cerr << "[Exception] " << exception.what() << std::endl; diff --git a/src/database/file/file.h b/src/database/file/file.h index c743011..f34bd4d 100644 --- a/src/database/file/file.h +++ b/src/database/file/file.h @@ -53,9 +53,9 @@ namespace bot { private: void cache_add_channel(dpp::snowflake guild_id, dpp::snowflake channel_id); - void cache_get_channel(dpp::snowflake channel_id, bot::settings::channel *channel); - void cache_guild(dpp::snowflake guild_id, std::vector *channels); - void list_guilds(std::vector *guilds); + void cache_get_channel(dpp::snowflake channel_id, bot::settings::channel &channel); + void cache_guild(dpp::snowflake guild_id, std::vector &channels); + void list_guilds(std::vector &guilds); void sync_cache(); #if defined(__unix__) int fd;