dtranslatebot/src/main.cpp

116 lines
4.2 KiB
C++
Raw Normal View History

2024-01-02 03:45:06 +01:00
/*****************************************************************************
* dtranslatebot Discord Translate Bot
* Copyright (C) 2023-2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <dpp/cluster.h>
#include <dpp/once.h>
2024-01-02 03:45:06 +01:00
#include <iostream>
#include <vector>
2024-01-02 03:45:06 +01:00
#include <thread>
#include "message_queue.h"
2024-01-02 03:45:06 +01:00
#include "settings.h"
#include "slashcommands.h"
2024-01-02 03:45:06 +01:00
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " [json]" << std::endl;
return 0;
}
bot::settings::settings settings;
2024-02-04 07:10:02 +01:00
if (!settings.parse_file(argv[1]))
return 1;
if (settings.get_translator()->get_languages().empty()) {
2024-02-04 10:12:19 +01:00
std::cerr << "[Error] Failed to initialise translateable languages" << std::endl;
return 2;
}
2024-02-04 09:24:32 +01:00
if (!std::filesystem::exists(settings.storage_path())) {
2024-02-04 10:12:19 +01:00
std::cerr << "[Error] Storage directory " << settings.storage_path() << " can not be found" << std::endl;
return 2;
}
2024-01-02 03:45:06 +01:00
2024-02-04 09:24:32 +01:00
dpp::cluster bot(settings.token(), dpp::i_default_intents | dpp::i_message_content);
2024-01-02 03:45:06 +01:00
2024-02-04 10:03:18 +01:00
bot.on_log([&bot](const dpp::log_t &event){
2024-02-04 10:12:19 +01:00
std::cerr << "[Log] " << event.message << std::endl;
2024-02-04 10:03:18 +01:00
});
2024-01-02 03:45:06 +01:00
bot::submit_queue submit_queue;
std::thread submit_queue_loop(&bot::submit_queue::run, &submit_queue, &bot);
bot::message_queue message_queue;
std::thread message_queue_loop(&bot::message_queue::run, &message_queue, &settings, &submit_queue);
2024-01-02 03:45:06 +01:00
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;
}
2024-01-02 03:45:06 +01:00
// 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);
2024-02-03 08:10:28 +01:00
if (const bot::settings::channel *channel = settings.get_channel(event.msg.guild_id, event.msg.channel_id)) {
2024-01-12 11:18:15 +01:00
bot::message message;
message.id = event.msg.id;
2024-01-17 13:18:53 +01:00
2024-01-17 13:13:51 +01:00
message.author = event.msg.member.get_nickname();
2024-01-17 13:18:53 +01:00
if (message.author.empty())
message.author = event.msg.author.global_name;
2024-01-17 13:18:53 +01:00
2024-02-04 09:24:32 +01:00
message.avatar = event.msg.member.get_avatar_url(settings.avatar_size());
2024-01-17 13:13:51 +01:00
if (message.avatar.empty())
2024-02-04 09:24:32 +01:00
message.avatar = event.msg.author.get_avatar_url(settings.avatar_size());
2024-01-17 13:18:53 +01:00
2024-01-12 11:18:15 +01:00
message.message = event.msg.content;
message.source = channel->source;
message.targets = channel->targets;
message_queue.add(std::move(message));
2024-01-02 03:45:06 +01:00
}
});
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_ready([&bot, &settings](const dpp::ready_t &event) {
if (dpp::run_once<struct register_bot_commands>()) {
bot::slashcommands::register_commands(&bot, &settings);
}
});
2024-01-02 03:45:06 +01:00
bot.start(dpp::st_wait);
// It's unneccessary, but we choose to exit clean anyway
message_queue.terminate();
message_queue_loop.join();
submit_queue.terminate();
submit_queue_loop.join();
2024-01-02 03:45:06 +01:00
return 0;
}