mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 13:50:22 +01:00
make code more efficient and added storage setting
This commit is contained in:
parent
3800da9b00
commit
25d83b243d
10 changed files with 264 additions and 186 deletions
|
@ -24,6 +24,7 @@ include(GNUInstallDirs)
|
||||||
set(DTRANSLATEBOT_HEADERS
|
set(DTRANSLATEBOT_HEADERS
|
||||||
src/message_queue.h
|
src/message_queue.h
|
||||||
src/settings.h
|
src/settings.h
|
||||||
|
src/slashcommands.h
|
||||||
src/submit_queue.h
|
src/submit_queue.h
|
||||||
src/translate_core.h
|
src/translate_core.h
|
||||||
src/translate_libretranslate.h
|
src/translate_libretranslate.h
|
||||||
|
@ -33,6 +34,7 @@ set(DTRANSLATEBOT_SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/message_queue.cpp
|
src/message_queue.cpp
|
||||||
src/settings.cpp
|
src/settings.cpp
|
||||||
|
src/slashcommands.cpp
|
||||||
src/submit_queue.cpp
|
src/submit_queue.cpp
|
||||||
src/translate_core.cpp
|
src/translate_core.cpp
|
||||||
src/translate_libretranslate.cpp
|
src/translate_libretranslate.cpp
|
||||||
|
|
168
src/main.cpp
168
src/main.cpp
|
@ -16,12 +16,14 @@
|
||||||
* responsible for anything with use of the software, you are self responsible.
|
* responsible for anything with use of the software, you are self responsible.
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include <dpp/dpp.h>
|
#include <dpp/cluster.h>
|
||||||
|
#include <dpp/once.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include "message_queue.h"
|
#include "message_queue.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "slashcommands.h"
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
|
@ -33,14 +35,14 @@ int main(int argc, char* argv[]) {
|
||||||
if (!settings.parse(argv[1]))
|
if (!settings.parse(argv[1]))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
{
|
if (settings.get_translator()->get_languages().empty()) {
|
||||||
std::vector<bot::translate::language> languages;
|
|
||||||
languages = settings.get_translator()->get_languages();
|
|
||||||
|
|
||||||
if (languages.empty()) {
|
|
||||||
std::cerr << "Failed to initialise translateable languages" << std::endl;
|
std::cerr << "Failed to initialise translateable languages" << std::endl;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!std::filesystem::exists(settings.get_storage_path())) {
|
||||||
|
std::cerr << "Storage directory " << settings.get_storage_path() << " can not be found" << std::endl;
|
||||||
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
dpp::cluster bot(settings.get_token(), dpp::i_default_intents | dpp::i_message_content);
|
dpp::cluster bot(settings.get_token(), dpp::i_default_intents | dpp::i_message_content);
|
||||||
|
@ -83,167 +85,19 @@ int main(int argc, char* argv[]) {
|
||||||
message.message = event.msg.content;
|
message.message = event.msg.content;
|
||||||
message.source = channel->source;
|
message.source = channel->source;
|
||||||
message.targets = channel->targets;
|
message.targets = channel->targets;
|
||||||
message_queue.add(message);
|
message_queue.add(std::move(message));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on_slashcommand([&bot, &settings](const dpp::slashcommand_t &event) {
|
bot.on_slashcommand([&bot, &settings](const dpp::slashcommand_t &event) {
|
||||||
if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") {
|
if (event.command.get_command_name() == "translate" || event.command.get_command_name() == "translate_pref") {
|
||||||
try {
|
bot::slashcommands::process_translate_command(&bot, &settings, event);
|
||||||
std::variant<dpp::channel,dpp::webhook> v_target;
|
|
||||||
const std::string source = std::get<std::string>(event.get_parameter("source"));
|
|
||||||
const std::string target = std::get<std::string>(event.get_parameter("target"));
|
|
||||||
|
|
||||||
dpp::command_interaction interaction = event.command.get_command_interaction();
|
|
||||||
if (interaction.options[0].name == "channel") {
|
|
||||||
v_target = event.command.get_resolved_channel(
|
|
||||||
std::get<dpp::snowflake>(event.get_parameter("channel")));
|
|
||||||
}
|
|
||||||
else if (interaction.options[0].name == "webhook") {
|
|
||||||
v_target = dpp::webhook(std::get<std::string>(event.get_parameter("webhook")));
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<bot::translate::language> languages = settings.get_translator()->get_languages();
|
|
||||||
|
|
||||||
std::ostringstream language_codes;
|
|
||||||
bool source_valid = false, target_valid = false;
|
|
||||||
for (const bot::translate::language &language : languages) {
|
|
||||||
if (language.code == source)
|
|
||||||
source_valid = true;
|
|
||||||
if (language.code == target)
|
|
||||||
target_valid = true;
|
|
||||||
if (source_valid && target_valid)
|
|
||||||
break;
|
|
||||||
language_codes << " " << language.code;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (source_valid && target_valid) {
|
|
||||||
const std::lock_guard<bot::settings::settings> guard(settings);
|
|
||||||
if (!settings.get_channel(event.command.guild_id, event.command.channel_id)) {
|
|
||||||
if (dpp::channel *channel = std::get_if<dpp::channel>(&v_target)) {
|
|
||||||
dpp::webhook webhook;
|
|
||||||
webhook.channel_id = channel->id;
|
|
||||||
webhook.guild_id = channel->guild_id;
|
|
||||||
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
|
|
||||||
|
|
||||||
bot.create_webhook(webhook, [&bot, &settings, event, source, target](const dpp::confirmation_callback_t &callback) {
|
|
||||||
if (callback.is_error()) {
|
|
||||||
event.reply(dpp::message("Failed to generate webhook!\n" + callback.http_info.body).set_flags(dpp::m_ephemeral));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const dpp::webhook webhook = callback.get<dpp::webhook>();
|
|
||||||
|
|
||||||
bot::settings::channel s_channel;
|
|
||||||
s_channel.id = event.command.channel_id;
|
|
||||||
s_channel.source = source;
|
|
||||||
|
|
||||||
bot::settings::target s_target;
|
|
||||||
s_target.target = target;
|
|
||||||
s_target.webhook = webhook;
|
|
||||||
s_channel.targets.emplace_back(s_target);
|
|
||||||
|
|
||||||
settings.lock();
|
|
||||||
settings.add_channel(s_channel, event.command.guild_id);
|
|
||||||
settings.add_translatebot_webhook(webhook);
|
|
||||||
settings.unlock();
|
|
||||||
|
|
||||||
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
|
|
||||||
bot::settings::channel s_channel;
|
|
||||||
s_channel.id = event.command.channel_id;
|
|
||||||
s_channel.source = source;
|
|
||||||
|
|
||||||
bot::settings::target s_target;
|
|
||||||
s_target.target = target;
|
|
||||||
s_target.webhook = *webhook;
|
|
||||||
s_channel.targets.emplace_back(s_target);
|
|
||||||
|
|
||||||
settings.lock();
|
|
||||||
settings.add_channel(s_channel, event.command.guild_id);
|
|
||||||
settings.add_translatebot_webhook(*webhook);
|
|
||||||
settings.unlock();
|
|
||||||
|
|
||||||
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
event.reply(dpp::message("The current channel is already being translated!").set_flags(dpp::m_ephemeral));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!source_valid && !target_valid) {
|
|
||||||
event.reply(dpp::message("Source and target languages are not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
|
|
||||||
}
|
|
||||||
else if (!source_valid) {
|
|
||||||
event.reply(dpp::message("Source language is not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
|
|
||||||
}
|
|
||||||
else if (!target_valid) {
|
|
||||||
event.reply(dpp::message("Target language is not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (const std::exception &exception) {
|
|
||||||
std::cerr << "Failed to process command /" << event.command.get_command_name() << ": " << exception.what() << std::endl;
|
|
||||||
event.reply(dpp::message("Failed to process command /" + event.command.get_command_name() + "\n" + exception.what()).set_flags(dpp::m_ephemeral));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.on_ready([&bot, &settings](const dpp::ready_t &event) {
|
bot.on_ready([&bot, &settings](const dpp::ready_t &event) {
|
||||||
if (dpp::run_once<struct register_bot_commands>()) {
|
if (dpp::run_once<struct register_bot_commands>()) {
|
||||||
settings.lock();
|
bot::slashcommands::register_commands(&bot, &settings);
|
||||||
const std::vector<bot::translate::language> languages = settings.get_translator()->get_languages();
|
|
||||||
const std::vector<std::string> preferred_languages = settings.get_preferred_languages();
|
|
||||||
settings.unlock();
|
|
||||||
|
|
||||||
std::vector<dpp::slashcommand> commands;
|
|
||||||
|
|
||||||
dpp::slashcommand command_translate("translate", "Translate current channel (ISO 639-1)", bot.me.id);
|
|
||||||
command_translate.set_default_permissions(dpp::p_manage_webhooks);
|
|
||||||
dpp::command_option channel_subcommand(dpp::co_sub_command, "channel", "Translate current channel to a channel (ISO 639-1)");
|
|
||||||
dpp::command_option webhook_subcommand(dpp::co_sub_command, "webhook", "Translate current channel to a webhook (ISO 639-1)");
|
|
||||||
dpp::command_option source_option(dpp::co_string, "source", "Source language", true);
|
|
||||||
source_option.set_max_length(2).set_min_length(2);
|
|
||||||
dpp::command_option target_option(dpp::co_string, "target", "Target language", true);
|
|
||||||
target_option.set_max_length(2).set_min_length(2);
|
|
||||||
dpp::command_option channel_option(dpp::co_channel, "channel", "Target channel", true);
|
|
||||||
channel_option.add_channel_type(dpp::CHANNEL_TEXT);
|
|
||||||
dpp::command_option webhook_option(dpp::co_string, "webhook", "Target webhook", true);
|
|
||||||
channel_subcommand.add_option(source_option);
|
|
||||||
channel_subcommand.add_option(target_option);
|
|
||||||
channel_subcommand.add_option(channel_option);
|
|
||||||
webhook_subcommand.add_option(source_option);
|
|
||||||
webhook_subcommand.add_option(target_option);
|
|
||||||
webhook_subcommand.add_option(webhook_option);
|
|
||||||
command_translate.add_option(channel_subcommand);
|
|
||||||
command_translate.add_option(webhook_subcommand);
|
|
||||||
commands.emplace_back(command_translate);
|
|
||||||
|
|
||||||
if (preferred_languages.size() > 1) {
|
|
||||||
dpp::slashcommand command_translate_pref("translate_pref", "Translate current channel (Preferred languages)", bot.me.id);
|
|
||||||
command_translate_pref.set_default_permissions(dpp::p_manage_webhooks);
|
|
||||||
dpp::command_option channel_pref_subcommand(dpp::co_sub_command, "channel", "Translate current channel to a channel (Preferred languages)");
|
|
||||||
dpp::command_option webhook_pref_subcommand(dpp::co_sub_command, "webhook", "Translate current channel to a webhook (Preferred languages)");
|
|
||||||
dpp::command_option source_pref_option(dpp::co_string, "source", "Source language", true);
|
|
||||||
dpp::command_option target_pref_option(dpp::co_string, "target", "Target language", true);
|
|
||||||
for (const bot::translate::language &language : languages) {
|
|
||||||
if (std::find(preferred_languages.begin(), preferred_languages.end(), language.code) != preferred_languages.end()) {
|
|
||||||
source_pref_option.add_choice(dpp::command_option_choice(language.name, language.code));
|
|
||||||
target_pref_option.add_choice(dpp::command_option_choice(language.name, language.code));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
channel_pref_subcommand.add_option(source_pref_option);
|
|
||||||
channel_pref_subcommand.add_option(target_pref_option);
|
|
||||||
channel_pref_subcommand.add_option(channel_option);
|
|
||||||
webhook_pref_subcommand.add_option(source_pref_option);
|
|
||||||
webhook_pref_subcommand.add_option(target_pref_option);
|
|
||||||
webhook_pref_subcommand.add_option(webhook_option);
|
|
||||||
command_translate_pref.add_option(channel_pref_subcommand);
|
|
||||||
command_translate_pref.add_option(webhook_pref_subcommand);
|
|
||||||
commands.emplace_back(command_translate_pref);
|
|
||||||
}
|
|
||||||
|
|
||||||
bot.global_bulk_command_create(commands);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -21,16 +21,6 @@
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
inline bot::translated_message make_translated_message(const bot::message &message, const std::string &translated_message, const dpp::webhook &webhook)
|
|
||||||
{
|
|
||||||
bot::translated_message tr_message;
|
|
||||||
tr_message.author = message.author;
|
|
||||||
tr_message.avatar = message.avatar;
|
|
||||||
tr_message.message = translated_message;
|
|
||||||
tr_message.webhook = webhook;
|
|
||||||
return tr_message;
|
|
||||||
}
|
|
||||||
|
|
||||||
void bot::message_queue::add(const bot::message &message)
|
void bot::message_queue::add(const bot::message &message)
|
||||||
{
|
{
|
||||||
m_mutex.lock();
|
m_mutex.lock();
|
||||||
|
@ -51,8 +41,12 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
|
||||||
std::unique_ptr<bot::translate::translator> translator = settings->get_translator();
|
std::unique_ptr<bot::translate::translator> translator = settings->get_translator();
|
||||||
|
|
||||||
for (auto target = message.targets.begin(); target != message.targets.end(); target++) {
|
for (auto target = message.targets.begin(); target != message.targets.end(); target++) {
|
||||||
const std::string tr_message = translator->translate(message.message, message.source, target->target);
|
bot::translated_message tr_message;
|
||||||
submit_queue->add(make_translated_message(message, tr_message, target->webhook));
|
tr_message.author = message.author;
|
||||||
|
tr_message.avatar = message.avatar;
|
||||||
|
tr_message.message = translator->translate(message.message, message.source, target->target);
|
||||||
|
tr_message.webhook = target->webhook;
|
||||||
|
submit_queue->add(std::move(tr_message));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#ifndef MESSAGE_QUEUE_H
|
#ifndef MESSAGE_QUEUE_H
|
||||||
#define MESSAGE_QUEUE_H
|
#define MESSAGE_QUEUE_H
|
||||||
#include <dpp/dpp.h>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
#include <dpp/json.h>
|
#include <dpp/json.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
@ -35,13 +36,13 @@ void bot::settings::settings::add_channel(const bot::settings::channel &channel,
|
||||||
// We will create the guild structure when it is not in memory
|
// We will create the guild structure when it is not in memory
|
||||||
bot::settings::guild guild;
|
bot::settings::guild guild;
|
||||||
guild.id = guild_id;
|
guild.id = guild_id;
|
||||||
guild.channel.emplace_back(channel);
|
guild.channel.push_back(std::move(channel));
|
||||||
m_guilds.emplace_back(guild);
|
m_guilds.push_back(std::move(guild));
|
||||||
}
|
}
|
||||||
|
|
||||||
void bot::settings::settings::add_translatebot_webhook(const dpp::webhook &webhook)
|
void bot::settings::settings::add_translatebot_webhook(dpp::snowflake webhook_id)
|
||||||
{
|
{
|
||||||
m_webhookIds.push_back(webhook.id);
|
m_webhookIds.push_back(webhook_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t bot::settings::settings::get_avatar_size()
|
uint16_t bot::settings::settings::get_avatar_size()
|
||||||
|
@ -86,6 +87,11 @@ const std::vector<std::string> bot::settings::settings::get_preferred_languages(
|
||||||
return m_preflangs;
|
return m_preflangs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::filesystem::path bot::settings::settings::get_storage_path()
|
||||||
|
{
|
||||||
|
return m_storagepath;
|
||||||
|
}
|
||||||
|
|
||||||
const bot::settings::translate* bot::settings::settings::get_translate()
|
const bot::settings::translate* bot::settings::settings::get_translate()
|
||||||
{
|
{
|
||||||
return &m_translate;
|
return &m_translate;
|
||||||
|
@ -145,6 +151,16 @@ 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);
|
||||||
m_token = json_token.value();
|
m_token = json_token.value();
|
||||||
|
|
||||||
|
auto json_storage = json.find("storage");
|
||||||
|
if (json_storage != json.end())
|
||||||
|
m_storagepath = std::string(json_storage.value());
|
||||||
|
else if (char *storagepath = getenv("DTRANSLATEBOT_STORAGE"))
|
||||||
|
m_storagepath = storagepath;
|
||||||
|
|
||||||
|
if (m_storagepath.empty())
|
||||||
|
m_storagepath = std::filesystem::current_path();
|
||||||
|
|
||||||
|
// In future we should allow more services here, for now it's only LibreTranslate
|
||||||
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;
|
||||||
|
@ -248,25 +264,25 @@ bool bot::settings::settings::parse(const std::string &filename)
|
||||||
bot::settings::target target;
|
bot::settings::target target;
|
||||||
target.target = json_channel_target.value();
|
target.target = json_channel_target.value();
|
||||||
target.webhook = dpp::webhook(json_channel->at("webhook"));
|
target.webhook = dpp::webhook(json_channel->at("webhook"));
|
||||||
channel.targets.emplace_back(target);
|
|
||||||
m_webhookIds.push_back(target.webhook.id);
|
m_webhookIds.push_back(target.webhook.id);
|
||||||
|
channel.targets.push_back(std::move(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++) {
|
||||||
bot::settings::target target;
|
bot::settings::target target;
|
||||||
target.target = json_target.key();
|
target.target = json_target.key();
|
||||||
target.webhook = dpp::webhook(json_target.value());
|
target.webhook = dpp::webhook(json_target.value());
|
||||||
channel.targets.emplace_back(target);
|
|
||||||
m_webhookIds.push_back(target.webhook.id);
|
m_webhookIds.push_back(target.webhook.id);
|
||||||
|
channel.targets.push_back(std::move(target));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!channel.source.empty() && !channel.targets.empty())
|
if (!channel.source.empty() && !channel.targets.empty())
|
||||||
guild.channel.emplace_back(channel);
|
guild.channel.push_back(std::move(channel));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_guilds.emplace_back(guild);
|
m_guilds.push_back(std::move(guild));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,7 +295,7 @@ bool bot::settings::settings::parse(const std::string &filename)
|
||||||
std::cerr << "\"preferred_lang\" is limited to 25 languages" << std::endl;
|
std::cerr << "\"preferred_lang\" is limited to 25 languages" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
m_preflangs.push_back(json_preflang);
|
m_preflangs.push_back(std::move(json_preflang));
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <dpp/snowflake.h>
|
#include <dpp/snowflake.h>
|
||||||
#include <dpp/webhook.h>
|
#include <dpp/webhook.h>
|
||||||
|
#include <filesystem>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -52,12 +53,13 @@ namespace bot {
|
||||||
class settings {
|
class settings {
|
||||||
public:
|
public:
|
||||||
void add_channel(const bot::settings::channel &channel, dpp::snowflake guild_id);
|
void add_channel(const bot::settings::channel &channel, dpp::snowflake guild_id);
|
||||||
void add_translatebot_webhook(const dpp::webhook &webhook);
|
void add_translatebot_webhook(dpp::snowflake webhook_id);
|
||||||
uint16_t get_avatar_size();
|
uint16_t get_avatar_size();
|
||||||
const bot::settings::channel* get_channel(const bot::settings::guild *guild, dpp::snowflake channel_id);
|
const bot::settings::channel* get_channel(const bot::settings::guild *guild, dpp::snowflake channel_id);
|
||||||
const bot::settings::channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
|
const bot::settings::channel* get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id);
|
||||||
const bot::settings::guild* get_guild(dpp::snowflake guild_id);
|
const bot::settings::guild* get_guild(dpp::snowflake guild_id);
|
||||||
const std::vector<std::string> get_preferred_languages();
|
const std::vector<std::string> get_preferred_languages();
|
||||||
|
const std::filesystem::path get_storage_path();
|
||||||
const bot::settings::translate* get_translate();
|
const bot::settings::translate* get_translate();
|
||||||
std::unique_ptr<bot::translate::translator> get_translator();
|
std::unique_ptr<bot::translate::translator> get_translator();
|
||||||
const std::string get_token();
|
const std::string get_token();
|
||||||
|
@ -71,6 +73,7 @@ namespace bot {
|
||||||
std::recursive_mutex m_mutex;
|
std::recursive_mutex m_mutex;
|
||||||
std::vector<bot::settings::guild> m_guilds;
|
std::vector<bot::settings::guild> m_guilds;
|
||||||
std::vector<std::string> m_preflangs;
|
std::vector<std::string> m_preflangs;
|
||||||
|
std::filesystem::path m_storagepath;
|
||||||
bot::settings::translate m_translate;
|
bot::settings::translate m_translate;
|
||||||
std::string m_token;
|
std::string m_token;
|
||||||
std::vector<dpp::snowflake> m_webhookIds;
|
std::vector<dpp::snowflake> m_webhookIds;
|
||||||
|
|
177
src/slashcommands.cpp
Normal file
177
src/slashcommands.cpp
Normal file
|
@ -0,0 +1,177 @@
|
||||||
|
/*****************************************************************************
|
||||||
|
* dtranslatebot Discord Translate Bot
|
||||||
|
* Copyright (C) 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 "slashcommands.h"
|
||||||
|
|
||||||
|
void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
std::variant<dpp::channel,dpp::webhook> v_target;
|
||||||
|
const std::string source = std::get<std::string>(event.get_parameter("source"));
|
||||||
|
const std::string target = std::get<std::string>(event.get_parameter("target"));
|
||||||
|
|
||||||
|
dpp::command_interaction interaction = event.command.get_command_interaction();
|
||||||
|
if (interaction.options[0].name == "channel") {
|
||||||
|
v_target = event.command.get_resolved_channel(
|
||||||
|
std::get<dpp::snowflake>(event.get_parameter("channel")));
|
||||||
|
}
|
||||||
|
else if (interaction.options[0].name == "webhook") {
|
||||||
|
v_target = dpp::webhook(std::get<std::string>(event.get_parameter("webhook")));
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<bot::translate::language> languages = settings->get_translator()->get_languages();
|
||||||
|
|
||||||
|
std::ostringstream language_codes;
|
||||||
|
bool source_valid = false, target_valid = false;
|
||||||
|
for (const bot::translate::language &language : languages) {
|
||||||
|
if (language.code == source)
|
||||||
|
source_valid = true;
|
||||||
|
if (language.code == target)
|
||||||
|
target_valid = true;
|
||||||
|
if (source_valid && target_valid)
|
||||||
|
break;
|
||||||
|
language_codes << " " << language.code;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source_valid && target_valid) {
|
||||||
|
const std::lock_guard<bot::settings::settings> guard(*settings);
|
||||||
|
if (!settings->get_channel(event.command.guild_id, event.command.channel_id)) {
|
||||||
|
if (dpp::channel *channel = std::get_if<dpp::channel>(&v_target)) {
|
||||||
|
dpp::webhook webhook;
|
||||||
|
webhook.channel_id = channel->id;
|
||||||
|
webhook.guild_id = channel->guild_id;
|
||||||
|
webhook.name = "Translate Bot Webhook <" + std::to_string(event.command.channel_id) + ":" + source + ":" + target + ">";
|
||||||
|
|
||||||
|
bot->create_webhook(webhook, [&bot, &settings, event, source, target](const dpp::confirmation_callback_t &callback) {
|
||||||
|
if (callback.is_error()) {
|
||||||
|
event.reply(dpp::message("Failed to generate webhook!\n" + callback.http_info.body).set_flags(dpp::m_ephemeral));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const dpp::webhook webhook = callback.get<dpp::webhook>();
|
||||||
|
|
||||||
|
bot::settings::channel s_channel;
|
||||||
|
s_channel.id = event.command.channel_id;
|
||||||
|
s_channel.source = source;
|
||||||
|
|
||||||
|
bot::settings::target s_target;
|
||||||
|
s_target.target = target;
|
||||||
|
s_target.webhook = webhook;
|
||||||
|
s_channel.targets.push_back(std::move(s_target));
|
||||||
|
|
||||||
|
settings->lock();
|
||||||
|
settings->add_channel(s_channel, event.command.guild_id);
|
||||||
|
settings->add_translatebot_webhook(webhook.id);
|
||||||
|
settings->unlock();
|
||||||
|
|
||||||
|
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (dpp::webhook *webhook = std::get_if<dpp::webhook>(&v_target)) {
|
||||||
|
bot::settings::channel s_channel;
|
||||||
|
s_channel.id = event.command.channel_id;
|
||||||
|
s_channel.source = source;
|
||||||
|
|
||||||
|
bot::settings::target s_target;
|
||||||
|
s_target.target = target;
|
||||||
|
s_target.webhook = *webhook;
|
||||||
|
s_channel.targets.push_back(std::move(s_target));
|
||||||
|
|
||||||
|
settings->lock();
|
||||||
|
settings->add_channel(s_channel, event.command.guild_id);
|
||||||
|
settings->add_translatebot_webhook(webhook->id);
|
||||||
|
settings->unlock();
|
||||||
|
|
||||||
|
event.reply(dpp::message("Channel will be now translated!").set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
event.reply(dpp::message("The current channel is already being translated!").set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (!source_valid && !target_valid) {
|
||||||
|
event.reply(dpp::message("Source and target languages are not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
else if (!source_valid) {
|
||||||
|
event.reply(dpp::message("Source language is not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
else if (!target_valid) {
|
||||||
|
event.reply(dpp::message("Target language is not valid!\nAvailable languages are:" + language_codes.str()).set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::exception &exception) {
|
||||||
|
std::cerr << "Failed to process command /" << event.command.get_command_name() << ": " << exception.what() << std::endl;
|
||||||
|
event.reply(dpp::message("Failed to process command /" + event.command.get_command_name() + "\n" + exception.what()).set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::settings *settings)
|
||||||
|
{
|
||||||
|
settings->lock();
|
||||||
|
const std::vector<bot::translate::language> languages = settings->get_translator()->get_languages();
|
||||||
|
const std::vector<std::string> preferred_languages = settings->get_preferred_languages();
|
||||||
|
settings->unlock();
|
||||||
|
|
||||||
|
std::vector<dpp::slashcommand> commands;
|
||||||
|
|
||||||
|
dpp::slashcommand command_translate("translate", "Translate current channel (ISO 639-1)", bot->me.id);
|
||||||
|
command_translate.set_default_permissions(dpp::p_manage_webhooks);
|
||||||
|
dpp::command_option channel_subcommand(dpp::co_sub_command, "channel", "Translate current channel to a channel (ISO 639-1)");
|
||||||
|
dpp::command_option webhook_subcommand(dpp::co_sub_command, "webhook", "Translate current channel to a webhook (ISO 639-1)");
|
||||||
|
dpp::command_option source_option(dpp::co_string, "source", "Source language", true);
|
||||||
|
source_option.set_max_length(2).set_min_length(2);
|
||||||
|
dpp::command_option target_option(dpp::co_string, "target", "Target language", true);
|
||||||
|
target_option.set_max_length(2).set_min_length(2);
|
||||||
|
dpp::command_option channel_option(dpp::co_channel, "channel", "Target channel", true);
|
||||||
|
channel_option.add_channel_type(dpp::CHANNEL_TEXT);
|
||||||
|
dpp::command_option webhook_option(dpp::co_string, "webhook", "Target webhook", true);
|
||||||
|
channel_subcommand.add_option(source_option);
|
||||||
|
channel_subcommand.add_option(target_option);
|
||||||
|
channel_subcommand.add_option(channel_option);
|
||||||
|
webhook_subcommand.add_option(source_option);
|
||||||
|
webhook_subcommand.add_option(target_option);
|
||||||
|
webhook_subcommand.add_option(webhook_option);
|
||||||
|
command_translate.add_option(channel_subcommand);
|
||||||
|
command_translate.add_option(webhook_subcommand);
|
||||||
|
commands.push_back(std::move(command_translate));
|
||||||
|
|
||||||
|
if (preferred_languages.size() > 1) {
|
||||||
|
dpp::slashcommand command_translate_pref("translate_pref", "Translate current channel (Preferred languages)", bot->me.id);
|
||||||
|
command_translate_pref.set_default_permissions(dpp::p_manage_webhooks);
|
||||||
|
dpp::command_option channel_pref_subcommand(dpp::co_sub_command, "channel", "Translate current channel to a channel (Preferred languages)");
|
||||||
|
dpp::command_option webhook_pref_subcommand(dpp::co_sub_command, "webhook", "Translate current channel to a webhook (Preferred languages)");
|
||||||
|
dpp::command_option source_pref_option(dpp::co_string, "source", "Source language", true);
|
||||||
|
dpp::command_option target_pref_option(dpp::co_string, "target", "Target language", true);
|
||||||
|
for (const bot::translate::language &language : languages) {
|
||||||
|
if (std::find(preferred_languages.begin(), preferred_languages.end(), language.code) != preferred_languages.end()) {
|
||||||
|
source_pref_option.add_choice(dpp::command_option_choice(language.name, language.code));
|
||||||
|
target_pref_option.add_choice(dpp::command_option_choice(language.name, language.code));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
channel_pref_subcommand.add_option(source_pref_option);
|
||||||
|
channel_pref_subcommand.add_option(target_pref_option);
|
||||||
|
channel_pref_subcommand.add_option(channel_option);
|
||||||
|
webhook_pref_subcommand.add_option(source_pref_option);
|
||||||
|
webhook_pref_subcommand.add_option(target_pref_option);
|
||||||
|
webhook_pref_subcommand.add_option(webhook_option);
|
||||||
|
command_translate_pref.add_option(channel_pref_subcommand);
|
||||||
|
command_translate_pref.add_option(webhook_pref_subcommand);
|
||||||
|
commands.push_back(std::move(command_translate_pref));
|
||||||
|
}
|
||||||
|
|
||||||
|
bot->global_bulk_command_create(commands);
|
||||||
|
}
|
33
src/slashcommands.h
Normal file
33
src/slashcommands.h
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
/*****************************************************************************
|
||||||
|
* dtranslatebot Discord Translate Bot
|
||||||
|
* Copyright (C) 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.
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef SLASHCOMMANDS_H
|
||||||
|
#define SLASHCOMMANDS_H
|
||||||
|
|
||||||
|
#include <dpp/cluster.h>
|
||||||
|
#include "settings.h"
|
||||||
|
|
||||||
|
namespace bot {
|
||||||
|
class slashcommands {
|
||||||
|
public:
|
||||||
|
static void process_translate_command(dpp::cluster *bot, bot::settings::settings *settings, const dpp::slashcommand_t &event);
|
||||||
|
static void register_commands(dpp::cluster *bot, bot::settings::settings *settings);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SLASHCOMMANDS_H
|
|
@ -18,11 +18,11 @@
|
||||||
|
|
||||||
#ifndef SUBMIT_QUEUE_H
|
#ifndef SUBMIT_QUEUE_H
|
||||||
#define SUBMIT_QUEUE_H
|
#define SUBMIT_QUEUE_H
|
||||||
#include <dpp/dpp.h>
|
#include <dpp/cluster.h>
|
||||||
|
#include <dpp/webhook.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace bot {
|
namespace bot {
|
||||||
struct translated_message {
|
struct translated_message {
|
||||||
|
|
|
@ -52,7 +52,7 @@ const std::vector<bot::translate::language> bot::translate::libretranslate::get_
|
||||||
language.name = json_lang_name.value();
|
language.name = json_lang_name.value();
|
||||||
|
|
||||||
if (!language.code.empty() && !language.name.empty())
|
if (!language.code.empty() && !language.name.empty())
|
||||||
languages.emplace_back(language);
|
languages.push_back(std::move(language));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue