mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-21 21:30:24 +01:00
mostly database fixes
This commit is contained in:
parent
13474ebefb
commit
10135e6ec1
6 changed files with 103 additions and 15 deletions
|
@ -66,6 +66,14 @@ std::variant<std::monostate,bot::settings::target> database::find_channel_target
|
|||
return {};
|
||||
}
|
||||
|
||||
bot::settings::channel database::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cerr << "[Debug] database::get_channel(dpp::snowflake, dpp::snowflake) have being called." << std::endl;
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<dpp::snowflake> database::get_channels(dpp::snowflake guild_id)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
|
|
|
@ -37,6 +37,7 @@ namespace bot {
|
|||
virtual void delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target);
|
||||
virtual void delete_guild(dpp::snowflake guild_id);
|
||||
virtual std::variant<std::monostate,bot::settings::target> find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target);
|
||||
virtual bot::settings::channel get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
|
||||
virtual std::vector<dpp::snowflake> get_channels(dpp::snowflake guild_id);
|
||||
virtual std::string get_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id);
|
||||
virtual std::vector<bot::settings::target> get_channel_targets(dpp::snowflake guild_id, dpp::snowflake channel_id);
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
#include <cctype>
|
||||
#include <dpp/discordclient.h>
|
||||
#include <dpp/json.h>
|
||||
#include <dpp/utility.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
@ -253,6 +252,25 @@ std::variant<std::monostate,bot::settings::target> file::find_channel_target(dpp
|
|||
return {};
|
||||
}
|
||||
|
||||
bot::settings::channel file::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id)
|
||||
{
|
||||
const std::lock_guard<std::mutex> guard(m_mutex);
|
||||
|
||||
for (auto guild = m_dataCache.begin(); guild != m_dataCache.end(); guild++) {
|
||||
if (guild->id == guild_id) {
|
||||
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
|
||||
if (channel->id == channel_id)
|
||||
return *channel;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bot::settings::channel channel;
|
||||
cache_get_channel(channel_id, &channel);
|
||||
return std::move(channel);
|
||||
}
|
||||
|
||||
std::vector<dpp::snowflake> file::get_channels(dpp::snowflake guild_id)
|
||||
{
|
||||
const std::lock_guard<std::mutex> guard(m_mutex);
|
||||
|
@ -415,11 +433,9 @@ void file::cache_get_channel(dpp::snowflake channel_id, bot::settings::channel *
|
|||
for (auto json_target = json_channel_target->begin(); json_target != json_channel_target->end(); json_target++) {
|
||||
bot::settings::target target;
|
||||
target.target = json_target.key();
|
||||
if (json_target->is_array()) {
|
||||
if (json_target->size() == 2) {
|
||||
target.webhook.id = std::stoull(std::string(json_target->front()));
|
||||
target.webhook.token = json_target->back();
|
||||
}
|
||||
if (json_target->is_array() && json_target->size() == 2) {
|
||||
target.webhook.id = std::stoull(std::string(json_target->front()));
|
||||
target.webhook.token = json_target->back();
|
||||
}
|
||||
else if (json_target->is_string()) {
|
||||
target.webhook = dpp::webhook(*json_target);
|
||||
|
@ -474,8 +490,8 @@ void file::list_guilds(std::vector<dpp::snowflake> *guilds)
|
|||
|
||||
for (const auto &guild_file : std::filesystem::directory_iterator(guild_dir)) {
|
||||
const std::filesystem::path &guild_file_path = guild_file.path();
|
||||
if (guild_file_path.extension() == "json") {
|
||||
const std::string &guild_filename = guild_file_path.filename();
|
||||
if (guild_file_path.extension() == ".json") {
|
||||
const std::string &guild_filename = guild_file_path.stem();
|
||||
if (std::all_of(guild_filename.begin(), guild_filename.end(), ::isdigit)) {
|
||||
try {
|
||||
dpp::snowflake guild_id = std::stoull(guild_filename);
|
||||
|
|
|
@ -34,6 +34,7 @@ namespace bot {
|
|||
void delete_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) override;
|
||||
void delete_guild(dpp::snowflake guild_id) override;
|
||||
std::variant<std::monostate,bot::settings::target> find_channel_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target) override;
|
||||
bot::settings::channel get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
|
||||
std::vector<dpp::snowflake> get_channels(dpp::snowflake guild_id) override;
|
||||
std::string get_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
|
||||
std::vector<bot::settings::target> get_channel_targets(dpp::snowflake guild_id, dpp::snowflake channel_id) override;
|
||||
|
|
|
@ -24,6 +24,65 @@
|
|||
#include "translator_libretranslate.h"
|
||||
using namespace bot::settings;
|
||||
|
||||
void process_database_channels(std::shared_ptr<bot::database::database> database, bot::settings::guild *guild, std::vector<dpp::snowflake> *webhookIds)
|
||||
{
|
||||
const std::vector<dpp::snowflake> db_channels = database->get_channels(guild->id);
|
||||
for (auto db_channel_id = db_channels.begin(); db_channel_id != db_channels.end(); db_channel_id++) {
|
||||
bool channel_found = false;
|
||||
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
|
||||
if (channel->id == *db_channel_id) {
|
||||
const bot::settings::channel db_channel = database->get_channel(guild->id, channel->id);
|
||||
channel->source = db_channel.source;
|
||||
for (auto db_target = db_channel.targets.begin(); db_target != db_channel.targets.end(); db_target++) {
|
||||
bool target_found = false;
|
||||
for (auto target = channel->targets.begin(); target != channel->targets.end(); target++) {
|
||||
if (target->target == db_target->target) {
|
||||
target->webhook = db_target->webhook;
|
||||
webhookIds->push_back(db_target->webhook.id);
|
||||
target_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!target_found) {
|
||||
channel->targets.push_back(*db_target);
|
||||
webhookIds->push_back(db_target->webhook.id);
|
||||
}
|
||||
}
|
||||
channel_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!channel_found) {
|
||||
const bot::settings::channel db_channel = database->get_channel(guild->id, *db_channel_id);
|
||||
guild->channel.push_back(db_channel);
|
||||
for (auto db_target = db_channel.targets.begin(); db_target != db_channel.targets.end(); db_target++)
|
||||
webhookIds->push_back(db_target->webhook.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void process_database(std::shared_ptr<bot::database::database> database, std::vector<guild> *guilds, std::vector<dpp::snowflake> *webhookIds)
|
||||
{
|
||||
std::cout << "[Launch] Loading database..." << std::endl;
|
||||
const std::vector<dpp::snowflake> db_guilds = database->get_guilds();
|
||||
for (auto db_guild_id = db_guilds.begin(); db_guild_id != db_guilds.end(); db_guild_id++) {
|
||||
bool guild_found = false;
|
||||
for (auto guild = guilds->begin(); guild != guilds->end(); guild++) {
|
||||
if (guild->id == *db_guild_id) {
|
||||
process_database_channels(database, &*guild, webhookIds);
|
||||
guild_found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!guild_found) {
|
||||
bot::settings::guild guild;
|
||||
guild.id = *db_guild_id;
|
||||
process_database_channels(database, &guild, webhookIds);
|
||||
guilds->push_back(std::move(guild));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void process_guild_settings(const dpp::json &json, std::vector<guild> *guilds, std::vector<dpp::snowflake> *webhookIds)
|
||||
{
|
||||
for (auto json_guild = json.begin(); json_guild != json.end(); json_guild++) {
|
||||
|
@ -213,7 +272,7 @@ const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_
|
|||
}
|
||||
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
|
||||
if (channel->id == channel_id)
|
||||
return &(*channel);
|
||||
return &*channel;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -230,7 +289,7 @@ const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake cha
|
|||
if (guild->id == guild_id) {
|
||||
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
|
||||
if (channel->id == channel_id)
|
||||
return &(*channel);
|
||||
return &*channel;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -248,7 +307,7 @@ const guild* settings::get_guild(dpp::snowflake guild_id) const
|
|||
}
|
||||
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
|
||||
if (guild->id == guild_id)
|
||||
return &(*guild);
|
||||
return &*guild;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -261,6 +320,7 @@ const std::vector<std::string> settings::preferred_languages() const
|
|||
|
||||
std::shared_ptr<bot::database::database> settings::get_database() const
|
||||
{
|
||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||
return m_database;
|
||||
}
|
||||
|
||||
|
@ -352,6 +412,8 @@ bool settings::parse(const std::string &data)
|
|||
if (json_user != json.end() && json_user->is_object())
|
||||
process_user_settings(*json_user, &m_avatarSize);
|
||||
|
||||
process_database(m_database, &m_guilds, &m_webhookIds);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception &exception) {
|
||||
|
|
|
@ -84,12 +84,12 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
|
|||
settings->lock();
|
||||
settings->add_channel(s_channel, event.command.guild_id);
|
||||
settings->add_translatebot_webhook(webhook.id);
|
||||
auto database = settings->get_database();
|
||||
settings->unlock();
|
||||
|
||||
std::shared_ptr<bot::database::database> database = settings->get_database();
|
||||
database->set_channel_source(event.command.guild_id, event.command.channel_id, source);
|
||||
database->add_channel_target(event.command.guild_id, event.command.channel_id, s_target);
|
||||
database->sync(); // do async later
|
||||
database->sync();
|
||||
|
||||
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
||||
});
|
||||
|
@ -107,12 +107,12 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti
|
|||
settings->lock();
|
||||
settings->add_channel(s_channel, event.command.guild_id);
|
||||
settings->add_translatebot_webhook(webhook->id);
|
||||
auto database = settings->get_database();
|
||||
settings->unlock();
|
||||
|
||||
std::shared_ptr<bot::database::database> database = settings->get_database();
|
||||
database->set_channel_source(event.command.guild_id, event.command.channel_id, source);
|
||||
database->add_channel_target(event.command.guild_id, event.command.channel_id, s_target);
|
||||
database->sync(); // do async later
|
||||
database->sync();
|
||||
|
||||
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue