code style changes and remove some constructors

This commit is contained in:
Syping 2024-03-11 04:10:18 +01:00
parent de121beaac
commit 94429ca718
9 changed files with 60 additions and 50 deletions

View file

@ -31,6 +31,8 @@ namespace bot {
class database { class database {
public: public:
explicit database() = default; explicit database() = default;
database(const database&) = delete;
database& operator=(const database&) = delete;
virtual ~database() = default; virtual ~database() = default;
virtual void add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const bot::settings::target &target); 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); virtual void delete_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);

View file

@ -19,7 +19,7 @@
#include <thread> #include <thread>
#include "message_queue.h" #include "message_queue.h"
#include "settings.h" #include "settings.h"
using namespace bot; using bot::message_queue;
using namespace std::chrono_literals; using namespace std::chrono_literals;
void message_queue::add(const message &message) void message_queue::add(const message &message)

View file

@ -18,19 +18,20 @@
#include <sstream> #include <sstream>
#include "slashcommands.h" #include "slashcommands.h"
using bot::slashcommands;
using namespace std::string_literals; 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") 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") 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") 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 { try {
dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); 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();) { for (auto target = channel->targets.begin(); target != channel->targets.end();) {
if (std::find(targets.begin(), targets.end(), target->target) != 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); target = channel->targets.erase(target);
} }
else { else {
@ -89,7 +90,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings::
if (target_found) { if (target_found) {
for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) { for (auto _target = channel->targets.begin(); _target != channel->targets.end(); _target++) {
if (_target->target == 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); channel->targets.erase(_target);
break; 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()) { if (callback.is_error()) {
std::cerr << "[Error] Failed to delete Webhook " << webhook_id << std::endl; 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); 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 { try {
dpp::command_interaction interaction = event.command.get_command_interaction(); 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 { try {
dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); 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.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, 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<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 };
@ -344,7 +345,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, 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<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 };
@ -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()) { 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));
@ -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)); 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()) { 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));
@ -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)); 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(); settings->lock();
const std::vector<bot::translator::language> languages = settings->get_translator()->get_languages(); const std::vector<bot::translator::language> languages = settings->get_translator()->get_languages();

View file

@ -23,16 +23,20 @@
#include "settings.h" #include "settings.h"
namespace bot { namespace bot {
namespace slashcommands { class slashcommands {
extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); public:
extern void process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event); slashcommands() = delete;
extern void process_deleted_webhook(bot::settings::settings *settings, dpp::snowflake webhook_id, const dpp::confirmation_callback_t &callback); static void process_command_event(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); static void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
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); private:
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); static void process_edit_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
extern void register_commands(dpp::cluster *bot, bot::settings::settings *settings); 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 #endif // SLASHCOMMANDS_H

View file

@ -19,7 +19,7 @@
#include <thread> #include <thread>
#include "submit_queue.h" #include "submit_queue.h"
#include "webhook_push.h" #include "webhook_push.h"
using namespace bot; using bot::submit_queue;
using namespace std::chrono_literals; using namespace std::chrono_literals;
void submit_queue::add(const translated_message &message) void submit_queue::add(const translated_message &message)

View file

@ -33,6 +33,8 @@ namespace bot {
public: public:
explicit translator() = default; explicit translator() = default;
virtual ~translator() = default; virtual ~translator() = default;
translator(const translator&) = delete;
translator& operator=(const translator&) = delete;
virtual const std::vector<language> get_languages(); virtual const std::vector<language> get_languages();
virtual const std::string translate(const std::string &text, const std::string &source, const std::string &target); virtual const std::string translate(const std::string &text, const std::string &source, const std::string &target);
}; };

View file

@ -26,6 +26,7 @@
namespace bot { namespace bot {
class webhook_push { class webhook_push {
public: public:
webhook_push() = delete;
static void run(const bot::translated_message &message, dpp::cluster *bot); static void run(const bot::translated_message &message, dpp::cluster *bot);
private: private:

View file

@ -98,7 +98,7 @@ void file::add_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id
} }
bot::settings::channel channel; bot::settings::channel channel;
cache_get_channel(channel_id, &channel); cache_get_channel(channel_id, channel);
channel.targets.push_back(target); channel.targets.push_back(target);
cache_add_channel(guild_id, channel_id); cache_add_channel(guild_id, channel_id);
guild->channel.push_back(std::move(channel)); 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; bot::settings::channel channel;
cache_get_channel(channel_id, &channel); cache_get_channel(channel_id, channel);
channel.targets.push_back(target); channel.targets.push_back(target);
cache_add_channel(guild_id, channel_id); cache_add_channel(guild_id, channel_id);
m_dataCache.push_back({ guild_id, { std::move(channel) } }); 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<dpp::snowflake> channels; std::vector<dpp::snowflake> channels;
cache_guild(guild_id, &channels); cache_guild(guild_id, channels);
for (auto channel = channels.begin(); channel != channels.end(); channel++) { for (auto channel = channels.begin(); channel != channels.end(); channel++) {
if (*channel == channel_id) { if (*channel == channel_id) {
channels.erase(channel); channels.erase(channel);
@ -175,7 +175,7 @@ void file::delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel
} }
bot::settings::channel 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++) { for (auto _target = channel.targets.begin(); _target != channel.targets.end(); _target++) {
if (_target->target == target) { if (_target->target == target) {
channel.targets.erase(_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; 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++) { for (auto _target = channel.targets.begin(); _target != channel.targets.end(); _target++) {
if (_target->target == target) { if (_target->target == target) {
channel.targets.erase(_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; bot::settings::channel channel;
cache_get_channel(channel_id, &channel); cache_get_channel(channel_id, channel);
return channel; return channel;
} }
@ -281,7 +281,7 @@ std::vector<dpp::snowflake> file::get_channels(dpp::snowflake guild_id)
} }
std::vector<dpp::snowflake> channels; std::vector<dpp::snowflake> channels;
cache_guild(guild_id, &channels); cache_guild(guild_id, channels);
return channels; return channels;
} }
@ -330,7 +330,7 @@ std::vector<dpp::snowflake> file::get_guilds()
{ {
const std::lock_guard<std::mutex> guard(m_mutex); const std::lock_guard<std::mutex> guard(m_mutex);
std::vector<dpp::snowflake> guilds; std::vector<dpp::snowflake> guilds;
list_guilds(&guilds); list_guilds(guilds);
return guilds; return guilds;
} }
@ -348,7 +348,7 @@ void file::set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id
} }
bot::settings::channel channel; bot::settings::channel channel;
cache_get_channel(channel_id, &channel); cache_get_channel(channel_id, channel);
channel.source = source; channel.source = source;
cache_add_channel(guild_id, channel_id); cache_add_channel(guild_id, channel_id);
guild->channel.push_back(std::move(channel)); 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; bot::settings::channel channel;
cache_get_channel(channel_id, &channel); cache_get_channel(channel_id, channel);
channel.source = source; channel.source = source;
cache_add_channel(guild_id, channel_id); cache_add_channel(guild_id, channel_id);
m_dataCache.push_back({ guild_id, { std::move(channel) } }); 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<dpp::snowflake> channels; std::vector<dpp::snowflake> channels;
cache_guild(guild_id, &channels); cache_guild(guild_id, channels);
if (std::find(channels.begin(), channels.end(), channel_id) == channels.end()) if (std::find(channels.begin(), channels.end(), channel_id) == channels.end())
channels.push_back(channel_id); channels.push_back(channel_id);
m_channelCache.push_back({ guild_id, std::move(channels) }); 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"); 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()) { if (json.is_object()) {
auto json_channel_source = json.find("source"); auto json_channel_source = json.find("source");
if (json_channel_source != json.end()) if (json_channel_source != json.end())
channel->source = *json_channel_source; channel.source = *json_channel_source;
auto json_channel_target = json.find("target"); auto json_channel_target = json.find("target");
if (json_channel_target != json.end()) { 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()) { else if (json_target->is_string()) {
target.webhook = dpp::webhook(*json_target); 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<dpp::snowflake> *channels) void file::cache_guild(dpp::snowflake guild_id, std::vector<dpp::snowflake> &channels)
{ {
const std::filesystem::path guild_file = m_storagePath / "guild" / (std::to_string(guild_id) + ".json"); 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<dpp::snowflake> *cha
if (json.is_array()) { if (json.is_array()) {
for (auto channel = json.begin(); channel != json.end(); channel++) { for (auto channel = json.begin(); channel != json.end(); channel++) {
if (channel->is_number()) if (channel->is_number())
channels->push_back(*channel); channels.push_back(*channel);
else if (channel->is_string()) 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<dpp::snowflake> *cha
} }
} }
void file::list_guilds(std::vector<dpp::snowflake> *guilds) void file::list_guilds(std::vector<dpp::snowflake> &guilds)
{ {
const std::filesystem::path guild_dir = m_storagePath / "guild"; const std::filesystem::path guild_dir = m_storagePath / "guild";
@ -488,7 +488,7 @@ void file::list_guilds(std::vector<dpp::snowflake> *guilds)
if (std::all_of(guild_filename.begin(), guild_filename.end(), ::isdigit)) { if (std::all_of(guild_filename.begin(), guild_filename.end(), ::isdigit)) {
try { try {
dpp::snowflake guild_id = std::stoull(guild_filename); dpp::snowflake guild_id = std::stoull(guild_filename);
guilds->push_back(guild_id); guilds.push_back(guild_id);
} }
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "[Exception] " << exception.what() << std::endl; std::cerr << "[Exception] " << exception.what() << std::endl;

View file

@ -53,9 +53,9 @@ namespace bot {
private: private:
void cache_add_channel(dpp::snowflake guild_id, dpp::snowflake channel_id); 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_get_channel(dpp::snowflake channel_id, bot::settings::channel &channel);
void cache_guild(dpp::snowflake guild_id, std::vector<dpp::snowflake> *channels); void cache_guild(dpp::snowflake guild_id, std::vector<dpp::snowflake> &channels);
void list_guilds(std::vector<dpp::snowflake> *guilds); void list_guilds(std::vector<dpp::snowflake> &guilds);
void sync_cache(); void sync_cache();
#if defined(__unix__) #if defined(__unix__)
int fd; int fd;