mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-21 21:30:24 +01:00
add /list languages command, minor fixes
This commit is contained in:
parent
7dcb64f7cf
commit
ea2c6c460f
5 changed files with 55 additions and 15 deletions
|
@ -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
|
||||
|
|
|
@ -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<bot::settings::settings> guard(*settings);
|
||||
|
||||
|
|
|
@ -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)));
|
||||
|
|
|
@ -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<bot::translator::language> 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);
|
||||
|
|
|
@ -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<dpp::http_request_completion_t> &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<dpp::http_request_completion_t> _p;
|
||||
std::future<dpp::http_request_completion_t> _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<dpp::http_request_completion_t> promise;
|
||||
std::future<dpp::http_request_completion_t> 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();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue