improvements to configuration parser

This commit is contained in:
Syping 2024-01-12 11:18:15 +01:00
parent e09293cc4e
commit a746eda39b
6 changed files with 111 additions and 61 deletions

View file

@ -1,25 +1,27 @@
{ {
"token": "bot_token", "token": "$bot_token",
"guilds": { "guilds": {
"guild_1": { "$guild1_id": {
"channel_1": { "$channel1_id": {
"source": "en", "source": "en",
"target": "de", "target": "de",
"webhook": "https://german.webhook" "webhook": "https://discord.com/api/webhooks/$guild1_de_webhook_id/$guild1_de_webhook_token"
}, },
"channel_2": { "$channel2_id": {
"source": "de", "source": "de",
"target": "en", "target": "en",
"webhook": "https://english.webhook" "webhook": "https://discord.com/api/webhooks/$guild1_en_webhook_id/$guild1_en_webhook_token"
} }
}, },
"guild_2": { "My Discord Guild": {
"channel_1": { "id": "$guild2_id",
"General English": {
"id": "$channel3_id",
"source": "en", "source": "en",
"target": { "target": {
"de": "https://german.webhook", "de": "https://discord.com/api/webhooks/$guild2_de_webhook_id/$guild2_de_webhook_token",
"fr": "https://french.webhook", "fr": "https://discord.com/api/webhooks/$guild2_fr_webhook_id/$guild2_fr_webhook_token",
"ru": "https://russian.webhook" "ru": "https://discord.com/api/webhooks/$guild2_ru_webhook_id/$guild2_ru_webhook_token"
} }
} }
} }

View file

@ -44,11 +44,10 @@ int main(int argc, char* argv[]) {
return; return;
settings.lock(); settings.lock();
bot::settings::guild *guild = settings.get_guild(event.msg.guild_id); bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id);
if (guild) {
bot::settings::channel *channel = settings.get_channel(guild, event.msg.channel_id);
if (channel) { if (channel) {
bot::message message; bot::message message;
message.id = event.msg.id;
message.author = event.msg.author.format_username(); message.author = event.msg.author.format_username();
message.avatar = event.msg.author.avatar.to_string(); message.avatar = event.msg.author.avatar.to_string();
message.message = event.msg.content; message.message = event.msg.content;
@ -56,7 +55,6 @@ int main(int argc, char* argv[]) {
message.targets = channel->targets; message.targets = channel->targets;
message_queue.add(message); message_queue.add(message);
} }
}
settings.unlock(); settings.unlock();
}); });

View file

@ -51,7 +51,7 @@ void bot::message_queue::run(dpp::cluster *bot, bot::settings::settings *setting
dpp::json json_body = { dpp::json json_body = {
{"q", message.message}, {"q", message.message},
{"source", message.source}, {"source", message.source},
{"target", target->first}, {"target", target->target},
{"format", "text"}, {"format", "text"},
}; };
@ -72,13 +72,13 @@ void bot::message_queue::run(dpp::cluster *bot, bot::settings::settings *setting
} }
} }
dpp::webhook webhook(target->second); dpp::webhook webhook(target->webhook);
webhook.name = message.author; webhook.name = message.author;
try { try {
bot->execute_webhook_sync(webhook, dpp::message(tr_message)); bot->execute_webhook_sync(webhook, dpp::message(tr_message));
} }
catch (dpp::rest_exception &exception) { catch (const dpp::rest_exception &exception) {
std::cerr << "REST Error: " << exception.what() << std::endl; std::cerr << "REST Error: " << exception.what() << std::endl;
} }
} }

View file

@ -26,11 +26,12 @@
namespace bot { namespace bot {
struct message { struct message {
uint64_t id;
std::string author; std::string author;
std::string avatar; std::string avatar;
std::string message; std::string message;
std::string source; std::string source;
std::vector<std::pair<std::string,std::string>> targets; std::vector<bot::settings::target> targets;
}; };
class message_queue { class message_queue {

View file

@ -19,7 +19,6 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <mutex> #include <mutex>
#include <iostream> #include <iostream>
#include <utility>
#include "settings.h" #include "settings.h"
bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id) bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guild *guild, uint64_t channel_id)
@ -31,6 +30,19 @@ bot::settings::channel* bot::settings::settings::get_channel(bot::settings::guil
return nullptr; return nullptr;
} }
bot::settings::channel* bot::settings::settings::get_channel(uint64_t guild_id, uint64_t channel_id)
{
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++) {
if (channel->id == channel_id)
return &(*channel);
}
}
}
return nullptr;
}
bot::settings::guild* bot::settings::settings::get_guild(uint64_t guild_id) bot::settings::guild* bot::settings::settings::get_guild(uint64_t guild_id)
{ {
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) { for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
@ -66,6 +78,7 @@ bool bot::settings::settings::parse(const std::string &filename)
std::string sdata(std::istreambuf_iterator<char>{ifs}, {}); std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
ifs.close(); ifs.close();
try {
dpp::json json = dpp::json::parse(sdata); dpp::json json = dpp::json::parse(sdata);
if (!json.is_object()) { if (!json.is_object()) {
std::cerr << "JSON configuration file is corrupt" << std::endl; std::cerr << "JSON configuration file is corrupt" << std::endl;
@ -79,7 +92,6 @@ bool bot::settings::settings::parse(const std::string &filename)
} }
const std::lock_guard<std::recursive_mutex> guard(m_mutex); const std::lock_guard<std::recursive_mutex> guard(m_mutex);
try {
m_token = json_token.value(); m_token = json_token.value();
auto json_translate = json.find("translate"); auto json_translate = json.find("translate");
@ -131,9 +143,33 @@ bool bot::settings::settings::parse(const std::string &filename)
for (auto json_guild = json_guilds.value().begin(); json_guild != json_guilds.value().end(); json_guild++) { for (auto json_guild = json_guilds.value().begin(); json_guild != json_guilds.value().end(); json_guild++) {
if (json_guild.value().is_object()) { if (json_guild.value().is_object()) {
bot::settings::guild guild; bot::settings::guild guild;
auto json_guild_id = json_guild.value().find("id");
if (json_guild_id != json_guild.value().end()) {
if (json_guild_id->is_number())
guild.id = json_guild_id.value();
else if (json_guild_id->is_string())
guild.id = std::stoull(std::string(json_guild_id.value()));
else
throw std::invalid_argument("Guild id is not a number or a string");
}
else
guild.id = std::stoull(json_guild.key()); guild.id = std::stoull(json_guild.key());
for (auto json_channel = json_guild.value().begin(); json_channel != json_guild.value().end(); json_channel++) { for (auto json_channel = json_guild.value().begin(); json_channel != json_guild.value().end(); json_channel++) {
if (json_channel.value().is_object()) {
bot::settings::channel channel; bot::settings::channel channel;
auto json_channel_id = json_channel.value().find("id");
if (json_channel_id != json_channel.value().end()) {
if (json_channel_id->is_number())
channel.id = json_channel_id.value();
else if (json_channel_id->is_string())
channel.id = std::stoull(std::string(json_channel_id.value()));
else
throw std::invalid_argument("Channel id is not a number or a string");
}
else
channel.id = std::stoull(json_channel.key()); channel.id = std::stoull(json_channel.key());
auto json_channel_source = json_channel.value().find("source"); auto json_channel_source = json_channel.value().find("source");
@ -143,13 +179,17 @@ bool bot::settings::settings::parse(const std::string &filename)
auto json_channel_target = json_channel.value().find("target"); auto json_channel_target = json_channel.value().find("target");
if (json_channel_target != json_channel.value().end()) { if (json_channel_target != json_channel.value().end()) {
if (json_channel_target.value().is_string()) { if (json_channel_target.value().is_string()) {
const std::string target = json_channel_target.value(); bot::settings::target target;
const std::string webhook = json_channel->at("webhook"); target.target = json_channel_target.value();
channel.targets.push_back(std::make_pair(target, webhook)); target.webhook = json_channel->at("webhook");
channel.targets.push_back(target);
} }
else if (json_channel_target.value().is_object()) { else if (json_channel_target.value().is_object()) {
for (auto json_target = json_channel_target.value().begin(); json_target != json_channel_target.value().end(); json_target++) { for (auto json_target = json_channel_target.value().begin(); json_target != json_channel_target.value().end(); json_target++) {
channel.targets.push_back(std::make_pair(json_target.key(), json_target.value())); bot::settings::target target;
target.target = json_target.key();
target.webhook = json_target.value();
channel.targets.push_back(target);
} }
} }
} }
@ -157,12 +197,16 @@ bool bot::settings::settings::parse(const std::string &filename)
if (!channel.source.empty() && !channel.targets.empty()) if (!channel.source.empty() && !channel.targets.empty())
guild.channel.push_back(channel); guild.channel.push_back(channel);
} }
}
m_guilds.push_back(guild); m_guilds.push_back(guild);
} }
} }
} }
return true; return true;
} }
catch (const dpp::json::exception &exception) {
std::cerr << "Exception thrown while parsing configuration: " << exception.what() << std::endl;
}
catch (const std::exception &exception) { catch (const std::exception &exception) {
std::cerr << "Exception thrown while parsing configuration: " << exception.what() << std::endl; std::cerr << "Exception thrown while parsing configuration: " << exception.what() << std::endl;
} }

View file

@ -25,10 +25,14 @@
namespace bot { namespace bot {
namespace settings { namespace settings {
struct target {
std::string target;
std::string webhook;
};
struct channel { struct channel {
uint64_t id; uint64_t id;
std::string source; std::string source;
std::vector<std::pair<std::string,std::string>> targets; std::vector<bot::settings::target> targets;
}; };
struct guild { struct guild {
uint64_t id; uint64_t id;
@ -45,6 +49,7 @@ namespace bot {
class settings { class settings {
public: public:
bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id); bot::settings::channel* get_channel(bot::settings::guild *guild, uint64_t channel_id);
bot::settings::channel* get_channel(uint64_t guild_id, uint64_t channel_id);
bot::settings::guild* get_guild(uint64_t guild_id); bot::settings::guild* get_guild(uint64_t guild_id);
bot::settings::translate* get_translate(); bot::settings::translate* get_translate();
const std::string get_token(); const std::string get_token();