rename queue to message_queue, add exception handling to settings

This commit is contained in:
Syping 2024-01-11 21:49:01 +01:00
parent b710fa3050
commit acbab3c97b
6 changed files with 84 additions and 77 deletions

View file

@ -19,12 +19,14 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(dtranslatebot VERSION 0.1 DESCRIPTION "Discord Translation Bot") project(dtranslatebot VERSION 0.1 DESCRIPTION "Discord Translation Bot")
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
src/main.cpp src/main.cpp
src/queue.cpp src/message_queue.cpp
src/message_queue.h
src/settings.cpp src/settings.cpp
src/settings.h
) )
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)

View file

@ -19,7 +19,7 @@
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "queue.h" #include "message_queue.h"
#include "settings.h" #include "settings.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -36,10 +36,10 @@ int main(int argc, char* argv[]) {
bot.on_log(dpp::utility::cout_logger()); bot.on_log(dpp::utility::cout_logger());
bot::queue queue; bot::message_queue message_queue;
std::thread queue_loop(&bot::queue::run, &queue, &bot, &settings); std::thread message_queue_loop(&bot::message_queue::run, &message_queue, &bot, &settings);
bot.on_message_create([&bot, &queue, &settings](const dpp::message_create_t &event) { bot.on_message_create([&bot, &message_queue, &settings](const dpp::message_create_t &event) {
if (event.msg.author.is_bot()) if (event.msg.author.is_bot())
return; return;
@ -55,7 +55,7 @@ int main(int argc, char* argv[]) {
message.webhook = channel->webhook; message.webhook = channel->webhook;
message.source = channel->source; message.source = channel->source;
message.target = channel->target; message.target = channel->target;
queue.add(message); message_queue.add(message);
} }
} }
settings.unlock(); settings.unlock();

View file

@ -18,18 +18,18 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "queue.h" #include "message_queue.h"
#include "settings.h" #include "settings.h"
using namespace std::chrono_literals; using namespace std::chrono_literals;
void bot::queue::add(const bot::message &message) void bot::message_queue::add(const bot::message &message)
{ {
m_mutex.lock(); m_mutex.lock();
m_queue.push_back(message); m_queue.push_back(message);
m_mutex.unlock(); m_mutex.unlock();
} }
void bot::queue::run(dpp::cluster *bot, bot::settings::settings *settings) void bot::message_queue::run(dpp::cluster *bot, bot::settings::settings *settings)
{ {
while (true) { while (true) {
m_mutex.lock(); m_mutex.lock();

View file

@ -16,8 +16,8 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#ifndef QUEUE_H #ifndef MESSAGE_QUEUE_H
#define QUEUE_H #define MESSAGE_QUEUE_H
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -36,7 +36,7 @@ namespace bot {
std::string target; std::string target;
}; };
class queue { class message_queue {
public: public:
void add(const bot::message &message); void add(const bot::message &message);
void run(dpp::cluster *bot, bot::settings::settings *settings); void run(dpp::cluster *bot, bot::settings::settings *settings);
@ -47,4 +47,4 @@ namespace bot {
}; };
} }
#endif //QUEUE_H #endif //MESSAGE_QUEUE_H

View file

@ -17,6 +17,7 @@
*****************************************************************************/ *****************************************************************************/
#include <dpp/dpp.h> #include <dpp/dpp.h>
#include <mutex>
#include <iostream> #include <iostream>
#include "settings.h" #include "settings.h"
@ -76,84 +77,87 @@ bool bot::settings::settings::parse(const std::string &filename)
return false; return false;
} }
m_mutex.lock(); const std::lock_guard<std::recursive_mutex> guard(m_mutex);
m_token = json_token.value(); try {
m_token = json_token.value();
auto json_translate = json.find("translate"); auto json_translate = json.find("translate");
if (json_translate == json.end()) { if (json_translate == json.end()) {
std::cerr << "Translate settings can not be found" << std::endl; std::cerr << "Translate settings can not be found" << std::endl;
m_mutex.unlock(); return false;
return false; }
}
auto json_translate_hostname = json_translate.value().find("hostname"); auto json_translate_hostname = json_translate.value().find("hostname");
if (json_translate_hostname == json_translate.value().end()) { if (json_translate_hostname == json_translate.value().end()) {
std::cerr << "\"hostname\" can not be found in Translate settings" << std::endl; std::cerr << "\"hostname\" can not be found in Translate settings" << std::endl;
m_mutex.unlock(); return false;
return false; }
} m_translate_settings.hostname = json_translate_hostname.value();
m_translate_settings.hostname = json_translate_hostname.value();
auto json_translate_port = json_translate.value().find("port"); auto json_translate_port = json_translate.value().find("port");
if (json_translate_port == json_translate.value().end()) { if (json_translate_port == json_translate.value().end()) {
std::cerr << "\"port\" can not be found in Translate settings" << std::endl; std::cerr << "\"port\" can not be found in Translate settings" << std::endl;
m_mutex.unlock(); return false;
return false; }
} m_translate_settings.port = json_translate_port.value();
m_translate_settings.port = json_translate_port.value();
auto json_translate_url = json_translate.value().find("url"); auto json_translate_url = json_translate.value().find("url");
if (json_translate_url == json_translate.value().end()) { if (json_translate_url == json_translate.value().end()) {
std::cerr << "\"url\" can not be found in Translate settings" << std::endl; std::cerr << "\"url\" can not be found in Translate settings" << std::endl;
m_mutex.unlock(); return false;
return false; }
} m_translate_settings.url = json_translate_url.value();
m_translate_settings.url = json_translate_url.value();
auto json_translate_tls = json_translate.value().find("tls"); auto json_translate_tls = json_translate.value().find("tls");
if (json_translate_tls != json_translate.value().end()) if (json_translate_tls != json_translate.value().end())
m_translate_settings.tls = json_translate_tls.value(); m_translate_settings.tls = json_translate_tls.value();
else else
m_translate_settings.tls = false; m_translate_settings.tls = false;
auto json_translate_apiKey = json_translate.value().find("apiKey"); auto json_translate_apiKey = json_translate.value().find("apiKey");
if (json_translate_apiKey != json_translate.value().end()) if (json_translate_apiKey != json_translate.value().end())
m_translate_settings.apiKey = json_translate_apiKey.value(); m_translate_settings.apiKey = json_translate_apiKey.value();
else else
m_translate_settings.apiKey.clear(); m_translate_settings.apiKey.clear();
auto json_guilds = json.find("guilds"); auto json_guilds = json.find("guilds");
if (json_guilds != json.end()) { if (json_guilds != json.end()) {
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;
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++) {
bot::settings::channel channel; bot::settings::channel channel;
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");
if (json_channel_source != json_channel.value().end()) if (json_channel_source != json_channel.value().end())
channel.source = json_channel_source.value(); channel.source = json_channel_source.value();
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())
channel.target = json_channel_target.value(); channel.target = json_channel_target.value();
auto json_channel_webhook = json_channel.value().find("webhook"); auto json_channel_webhook = json_channel.value().find("webhook");
if (json_channel_webhook != json_channel.value().end()) if (json_channel_webhook != json_channel.value().end())
channel.webhook = json_channel_webhook.value(); channel.webhook = json_channel_webhook.value();
if (!channel.source.empty() && !channel.target.empty() && !channel.webhook.empty()) if (!channel.source.empty() && !channel.target.empty() && !channel.webhook.empty())
guild.channel.push_back(channel); guild.channel.push_back(channel);
}
m_guilds.push_back(guild);
} }
m_guilds.push_back(guild);
} }
} }
return true;
} }
catch (const std::exception &exception) {
m_mutex.unlock(); std::cerr << "Exception thrown while parsing configuration: " << exception.what() << std::endl;
return true; }
catch (...) {
std::cerr << "Exception thrown while parsing configuration: unknown" << std::endl;
}
return false;
} }
void bot::settings::settings::unlock() void bot::settings::settings::unlock()

View file

@ -20,6 +20,7 @@
#define SETTINGS_H #define SETTINGS_H
#include <cstdint> #include <cstdint>
#include <mutex> #include <mutex>
#include <string>
#include <vector> #include <vector>
namespace bot { namespace bot {