mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-21 21:30:24 +01:00
clean up and refactor some code
This commit is contained in:
parent
e2f3739fa1
commit
72ba15c798
7 changed files with 69 additions and 46 deletions
48
src/main.cpp
48
src/main.cpp
|
@ -31,23 +31,25 @@ int main(int argc, char* argv[]) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "[Launch] Processing configuration..." << std::endl;
|
||||
bot::settings::settings settings;
|
||||
if (!settings.parse_file(argv[1]))
|
||||
return 1;
|
||||
|
||||
std::cout << "[Launch] Requesting supported languages..." << std::endl;
|
||||
if (settings.get_translator()->get_languages().empty()) {
|
||||
std::cerr << "[Error] Failed to initialise translateable languages" << std::endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
std::cout << "[Launch] Checking working directory..." << std::endl;
|
||||
if (!std::filesystem::exists(settings.storage_path())) {
|
||||
std::cerr << "[Error] Storage directory " << settings.storage_path() << " can not be found" << std::endl;
|
||||
return 2;
|
||||
}
|
||||
|
||||
dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
|
||||
|
||||
bot.on_log([&bot](const dpp::log_t &event){
|
||||
bot.on_log([&bot](const dpp::log_t &event) {
|
||||
std::cerr << "[Log] " << event.message << std::endl;
|
||||
});
|
||||
|
||||
|
@ -57,51 +59,15 @@ int main(int argc, char* argv[]) {
|
|||
bot::message_queue message_queue;
|
||||
std::thread message_queue_loop(&bot::message_queue::run, &message_queue, &settings, &submit_queue);
|
||||
|
||||
bot.on_message_create([&bot, &message_queue, &settings](const dpp::message_create_t &event) {
|
||||
if (event.msg.webhook_id) {
|
||||
const std::lock_guard<bot::settings::settings> guard(settings);
|
||||
|
||||
// We will not translate messages from our own bot
|
||||
if (settings.is_translatebot(event.msg.webhook_id))
|
||||
return;
|
||||
}
|
||||
|
||||
// Same as before, just without the involvement of webhooks
|
||||
if (event.msg.author.id == bot.me.id)
|
||||
return;
|
||||
|
||||
const std::lock_guard<bot::settings::settings> guard(settings);
|
||||
if (const bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id)) {
|
||||
bot::message message;
|
||||
message.id = event.msg.id;
|
||||
|
||||
message.author = event.msg.member.get_nickname();
|
||||
if (message.author.empty())
|
||||
message.author = event.msg.author.global_name;
|
||||
|
||||
message.avatar = event.msg.member.get_avatar_url(settings.avatar_size());
|
||||
if (message.avatar.empty())
|
||||
message.avatar = event.msg.author.get_avatar_url(settings.avatar_size());
|
||||
|
||||
message.message = event.msg.content;
|
||||
message.source = channel->source;
|
||||
message.targets = channel->targets;
|
||||
message_queue.add(std::move(message));
|
||||
}
|
||||
});
|
||||
|
||||
bot.on_slashcommand([&bot, &settings](const dpp::slashcommand_t &event) {
|
||||
if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") {
|
||||
bot::slashcommands::process_translate_command(&bot, &settings, event);
|
||||
}
|
||||
});
|
||||
|
||||
bot.on_message_create(std::bind(&bot::message_queue::process_message_event, &message_queue, &bot, &settings, std::placeholders::_1));
|
||||
bot.on_slashcommand(std::bind(&bot::slashcommands::process_command_event, &bot, &settings, std::placeholders::_1));
|
||||
bot.on_ready([&bot, &settings](const dpp::ready_t &event) {
|
||||
if (dpp::run_once<struct register_bot_commands>()) {
|
||||
bot::slashcommands::register_commands(&bot, &settings);
|
||||
}
|
||||
});
|
||||
|
||||
std::cout << "[Launch] Starting bot..." << std::endl;
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
// It's unneccessary, but we choose to exit clean anyway
|
||||
|
|
|
@ -24,9 +24,49 @@ using namespace std::chrono_literals;
|
|||
|
||||
void message_queue::add(const message &message)
|
||||
{
|
||||
m_mutex.lock();
|
||||
const std::lock_guard<std::mutex> guard(m_mutex);
|
||||
m_queue.push(message);
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
void message_queue::add(message &&message)
|
||||
{
|
||||
const std::lock_guard<std::mutex> guard(m_mutex);
|
||||
m_queue.push(message);
|
||||
}
|
||||
|
||||
void message_queue::process_message_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::message_create_t &event)
|
||||
{
|
||||
if (event.msg.webhook_id) {
|
||||
const std::lock_guard<bot::settings::settings> guard(*settings);
|
||||
|
||||
// We will not translate messages from our own bot
|
||||
if (settings->is_translatebot(event.msg.webhook_id))
|
||||
return;
|
||||
}
|
||||
|
||||
// Same as before, just without the involvement of webhooks
|
||||
if (event.msg.author.id == bot->me.id)
|
||||
return;
|
||||
|
||||
const std::lock_guard<bot::settings::settings> guard(*settings);
|
||||
if (const bot::settings::channel *channel = settings->get_channel(event.msg.guild_id, event.msg.channel_id)) {
|
||||
bot::message message;
|
||||
message.id = event.msg.id;
|
||||
|
||||
message.author = event.msg.member.get_nickname();
|
||||
if (message.author.empty())
|
||||
message.author = event.msg.author.global_name;
|
||||
|
||||
message.avatar = event.msg.member.get_avatar_url(settings->avatar_size());
|
||||
if (message.avatar.empty())
|
||||
message.avatar = event.msg.author.get_avatar_url(settings->avatar_size());
|
||||
|
||||
message.message = event.msg.content;
|
||||
message.source = channel->source;
|
||||
message.targets = channel->targets;
|
||||
|
||||
add(std::move(message));
|
||||
}
|
||||
}
|
||||
|
||||
void message_queue::run(bot::settings::settings *settings, submit_queue *submit_queue)
|
||||
|
@ -39,7 +79,7 @@ void message_queue::run(bot::settings::settings *settings, submit_queue *submit_
|
|||
m_queue.pop();
|
||||
m_mutex.unlock();
|
||||
|
||||
std::unique_ptr<bot::translator::translator> translator = settings->get_translator();
|
||||
auto translator = settings->get_translator();
|
||||
|
||||
for (auto target = message.targets.begin(); target != message.targets.end(); target++) {
|
||||
translated_message tr_message;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#ifndef MESSAGE_QUEUE_H
|
||||
#define MESSAGE_QUEUE_H
|
||||
#include <dpp/cluster.h>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <queue>
|
||||
|
@ -38,6 +39,8 @@ namespace bot {
|
|||
class message_queue {
|
||||
public:
|
||||
void add(const message &message);
|
||||
void add(message &&message);
|
||||
void process_message_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::message_create_t &event);
|
||||
void run(bot::settings::settings *settings, submit_queue *submit_queue);
|
||||
void terminate();
|
||||
|
||||
|
|
|
@ -19,6 +19,13 @@
|
|||
#include "slashcommands.h"
|
||||
using namespace std::string_literals;
|
||||
|
||||
void bot::slashcommands::process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
|
||||
{
|
||||
if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") {
|
||||
bot::slashcommands::process_translate_command(bot, settings, event);
|
||||
}
|
||||
}
|
||||
|
||||
void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
|
||||
{
|
||||
try {
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
namespace bot {
|
||||
namespace slashcommands {
|
||||
extern void process_command_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
||||
extern void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
||||
extern void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
|
||||
}
|
||||
|
|
|
@ -24,9 +24,14 @@ using namespace std::chrono_literals;
|
|||
|
||||
void submit_queue::add(const translated_message &message)
|
||||
{
|
||||
m_mutex.lock();
|
||||
const std::lock_guard<std::mutex> guard(m_mutex);
|
||||
m_queue.push(message);
|
||||
}
|
||||
|
||||
void submit_queue::add(translated_message &&message)
|
||||
{
|
||||
const std::lock_guard<std::mutex> guard(m_mutex);
|
||||
m_queue.push(message);
|
||||
m_mutex.unlock();
|
||||
}
|
||||
|
||||
void submit_queue::run(dpp::cluster *bot)
|
||||
|
|
|
@ -35,6 +35,7 @@ namespace bot {
|
|||
class submit_queue {
|
||||
public:
|
||||
void add(const translated_message &message);
|
||||
void add(translated_message &&message);
|
||||
void run(dpp::cluster *bot);
|
||||
void terminate();
|
||||
|
||||
|
|
Loading…
Reference in a new issue