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": {
"guild_1": {
"channel_1": {
"$guild1_id": {
"$channel1_id": {
"source": "en",
"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",
"target": "en",
"webhook": "https://english.webhook"
"webhook": "https://discord.com/api/webhooks/$guild1_en_webhook_id/$guild1_en_webhook_token"
}
},
"guild_2": {
"channel_1": {
"My Discord Guild": {
"id": "$guild2_id",
"General English": {
"id": "$channel3_id",
"source": "en",
"target": {
"de": "https://german.webhook",
"fr": "https://french.webhook",
"ru": "https://russian.webhook"
"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"
}
}
}

View file

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

View file

@ -51,7 +51,7 @@ void bot::message_queue::run(dpp::cluster *bot, bot::settings::settings *setting
dpp::json json_body = {
{"q", message.message},
{"source", message.source},
{"target", target->first},
{"target", target->target},
{"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;
try {
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;
}
}

View file

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

View file

@ -19,7 +19,6 @@
#include <dpp/dpp.h>
#include <mutex>
#include <iostream>
#include <utility>
#include "settings.h"
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;
}
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)
{
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}, {});
ifs.close();
try {
dpp::json json = dpp::json::parse(sdata);
if (!json.is_object()) {
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);
try {
m_token = json_token.value();
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++) {
if (json_guild.value().is_object()) {
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());
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;
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());
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");
if (json_channel_target != json_channel.value().end()) {
if (json_channel_target.value().is_string()) {
const std::string target = json_channel_target.value();
const std::string webhook = json_channel->at("webhook");
channel.targets.push_back(std::make_pair(target, webhook));
bot::settings::target target;
target.target = json_channel_target.value();
target.webhook = json_channel->at("webhook");
channel.targets.push_back(target);
}
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++) {
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())
guild.channel.push_back(channel);
}
}
m_guilds.push_back(guild);
}
}
}
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;
}

View file

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