From ea2c6c460f15a6460625d4e1fdf7b6aba29460b3 Mon Sep 17 00:00:00 2001 From: Syping Date: Fri, 23 Feb 2024 07:14:08 +0100 Subject: [PATCH] add /list languages command, minor fixes --- CMakeLists.txt | 2 +- src/message_queue.cpp | 4 ++++ src/settings.cpp | 3 ++- src/slashcommands.cpp | 35 ++++++++++++++++++++++++++++++++--- src/webhook_push.cpp | 26 ++++++++++++++++---------- 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59e34f7..b26ac46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ ****************************************************************************]] cmake_minimum_required(VERSION 3.16) -project(dtranslatebot VERSION 0.1 LANGUAGES CXX) +project(dtranslatebot VERSION 0.2 LANGUAGES CXX) include(GNUInstallDirs) # dtranslatebot Source files diff --git a/src/message_queue.cpp b/src/message_queue.cpp index 7276de3..007f207 100644 --- a/src/message_queue.cpp +++ b/src/message_queue.cpp @@ -36,6 +36,10 @@ void message_queue::add(message &&message) void message_queue::process_message_event(dpp::cluster *bot, bot::settings::settings *settings, const dpp::message_create_t &event) { + // We check for conditions we want to skip translation for + if (event.msg.content.empty() || event.msg.has_thread()) + return; + if (event.msg.webhook_id) { const std::lock_guard guard(*settings); diff --git a/src/settings.cpp b/src/settings.cpp index cbce34a..a110e95 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -203,7 +203,8 @@ void process_url(const std::string &url, translator *translator) translator->url = "/"; url_v = url_v.substr(0, slash_pos); } - auto colon_pos = url_v.find_first_of(':'); + // We don't have IPv6 support here yet + auto colon_pos = url_v.find_last_of(':'); if (colon_pos != std::string_view::npos) { translator->hostname = url_v.substr(0, colon_pos); const int port = std::stoi(std::string(url_v.substr(colon_pos + 1))); diff --git a/src/slashcommands.cpp b/src/slashcommands.cpp index 2dede96..8ad0dd9 100644 --- a/src/slashcommands.cpp +++ b/src/slashcommands.cpp @@ -117,7 +117,7 @@ void bot::slashcommands::process_edit_command(dpp::cluster *bot, bot::settings:: source_valid = true; break; } - language_codes << " " << language.code; + language_codes << ' ' << language.code; } if (source_valid) { @@ -217,11 +217,38 @@ void bot::slashcommands::process_list_command(dpp::cluster *bot, bot::settings:: event.reply(dpp::message("The current guild have no translated channel!").set_flags(dpp::m_ephemeral)); } } + else if (interaction.options[0].name == "languages") { + dpp::permission user_permissions = event.command.get_resolved_permission(event.command.usr.id); + if (user_permissions.has(dpp::p_manage_webhooks)) { + const std::vector languages = settings->get_translator()->get_languages(); + std::ostringstream reply_languages; + reply_languages << "**Available Languages**\n"; + for (auto language = languages.begin(); language != languages.end();) { + reply_languages << language->name << ": " << language->code; + if (++language != languages.end()) + reply_languages << '\n'; + } + if (reply_languages.str().length() <= 2000) { + event.reply(dpp::message(reply_languages.str()).set_flags(dpp::m_ephemeral)); + } + else { + reply_languages.str({}); + reply_languages << "Available Languages:"; + for (auto language = languages.begin(); language != languages.end(); language++) { + reply_languages << ' ' << language->code; + } + event.reply(dpp::message(reply_languages.str()).set_flags(dpp::m_ephemeral)); + } + } + else { + event.reply(dpp::message("Unauthorized to list available languages!")); + } + } else { throw std::invalid_argument("Option " + interaction.options[0].name + " is not known"); } } - catch (const std::exception& exception) { + catch (const std::exception &exception) { std::cerr << "[Exception] " << exception.what() << std::endl; event.reply(dpp::message("Exception while processing command:\n"s + exception.what()).set_flags(dpp::m_ephemeral)); } @@ -258,7 +285,7 @@ void bot::slashcommands::process_translate_command(dpp::cluster *bot, bot::setti target_valid = true; if (source_valid && target_valid) break; - language_codes << " " << language.code; + language_codes << ' ' << language.code; } if (source_valid && target_valid) { @@ -404,8 +431,10 @@ void bot::slashcommands::register_commands(dpp::cluster *bot, bot::settings::set dpp::slashcommand command_list("list", "List translation settings", bot->me.id); dpp::command_option channel_list_subcommand(dpp::co_sub_command, "channel", "List current channel translation settings"); dpp::command_option guild_list_subcommand(dpp::co_sub_command, "guild", "List current guild translation settings"); + dpp::command_option languages_list_subcommand(dpp::co_sub_command, "languages", "List available languages to translate"); command_list.add_option(channel_list_subcommand); command_list.add_option(guild_list_subcommand); + command_list.add_option(languages_list_subcommand); commands.push_back(command_list); dpp::slashcommand command_translate("translate", "Translate current channel", bot->me.id); diff --git a/src/webhook_push.cpp b/src/webhook_push.cpp index 1e4a0f7..457ec3e 100644 --- a/src/webhook_push.cpp +++ b/src/webhook_push.cpp @@ -25,10 +25,12 @@ using namespace std::string_view_literals; void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster *bot) { dpp::json json_body = { - {"username"s, message.author}, - {"avatar_url"s, message.avatar} + {"username"s, message.author} }; + if (!message.avatar.empty()) + json_body["avatar_url"] = message.avatar; + // We will split too long messages into multiple messages if (message.message.length() > 2000) { std::string_view message_v = message.message; @@ -73,14 +75,18 @@ void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster } } +void webhook_request_completed(std::promise &promise, dpp::json &json, const dpp::http_request_completion_t &event) +{ + if (event.status != 204) + std::cerr << "[Warning] Webhook push returned unexpected code " << event.status << std::endl; + promise.set_value(event); +} + void bot::webhook_push::push_request(dpp::snowflake webhook_id, const std::string &webhook_token, const std::string &json, dpp::cluster *bot) { - std::promise _p; - std::future _f = _p.get_future(); - bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, [&bot, &_p](dpp::json &json, const dpp::http_request_completion_t &event) { - if (event.status != 204) - std::cerr << "[Warning] Webhook push returned unexpected code " << event.status << std::endl; - _p.set_value(event); - }); - _f.wait(); + std::promise promise; + std::future future = promise.get_future(); + bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, + std::bind(&webhook_request_completed, std::ref(promise), std::placeholders::_1, std::placeholders::_2)); + future.wait(); }