summaryrefslogtreecommitdiff
path: root/bot/src
diff options
context:
space:
mode:
Diffstat (limited to 'bot/src')
-rw-r--r--bot/src/actions/delete.cpp31
-rw-r--r--bot/src/handle_actions.cpp4
-rw-r--r--bot/src/main.cpp2
-rw-r--r--bot/src/utils.cpp43
4 files changed, 63 insertions, 17 deletions
diff --git a/bot/src/actions/delete.cpp b/bot/src/actions/delete.cpp
index 27855cc..22dbc80 100644
--- a/bot/src/actions/delete.cpp
+++ b/bot/src/actions/delete.cpp
@@ -1,9 +1,27 @@
-#include <dpp/dpp.h>
+#include "../../include/utils.hpp"
+
+
+const std::map<Lang, std::map<std::string, std::string>> error_messages_map = {
+ {Lang::en, {
+ {"error_no_messages", "No message to delete."},
+ {"error", "You need to wait a bit before deleting messages."},
+ {"error_amount", "The amount of messages to delete must be between 1 and 100."},
+ {"error_perm_channel", "You do not have permission to delete messages in this channel."}
+ }},
+ {Lang::fr, {
+ {"error_no_messages", "Aucun message à supprimer."},
+ {"error", "Vous devez attendre un peu avant de supprimer des messages."},
+ {"error_amount", "Le nombre de messages à supprimer doit être compris entre 1 et 100."},
+ {"error_perm_channel", "Vous n'avez pas la permission de supprimer des messages dans ce canal."}
+ }}
+};
dpp::task<bool> delete_action(const dpp::slashcommand_t &event, const nlohmann::json &action,
const std::unordered_map<std::string, std::string> &key_values,
dpp::user &user_ptr, dpp::cluster *cluster)
{
+ // setup locale for gettext
+ std::string locale = app::get_available_locale(event.command.locale);
const dpp::channel *channel_ptr = &event.command.get_channel();
const auto &guild_ptr = event.command.get_guild();
@@ -13,13 +31,14 @@ dpp::task<bool> delete_action(const dpp::slashcommand_t &event, const nlohmann::
const auto bot_member_it = guild_ptr.members.find(cluster->me.id);
const auto *bot_member_ptr = (bot_member_it != guild_ptr.members.end()) ? &bot_member_it->second : nullptr;
- const std::unordered_map<std::string, std::string> error_messages = [&action]()
+ const std::unordered_map<std::string, std::string> error_messages = [&action,&locale]()
{
std::unordered_map<std::string, std::string> defaults = {
- {"error_no_messages", "No messages to delete."},
- {"error", "You need to wait a bit before deleting messages."},
- {"error_amount", "The amount of messages to delete must be between 1 and 100."},
- {"error_perm_channel", "You do not have permission to delete messages in this channel."}};
+ {"error_no_messages", app::translate("error_no_messages", locale, error_messages_map)},
+ {"error", app::translate("error", locale, error_messages_map)},
+ {"error_amount", app::translate("error_amount", locale, error_messages_map)},
+ {"error_perm_channel", app::translate("error_perm_channel", locale, error_messages_map)},
+ };
if (action.contains("error_amount"))
defaults["error_amount"] = action["error_amount"];
if (action.contains("error_perm_channel"))
diff --git a/bot/src/handle_actions.cpp b/bot/src/handle_actions.cpp
index e469ec7..0b3f2ff 100644
--- a/bot/src/handle_actions.cpp
+++ b/bot/src/handle_actions.cpp
@@ -1,8 +1,6 @@
-#include <dpp/dpp.h>
#include "../include/actions/delete.hpp"
dpp::task<bool> handle_actions(const dpp::slashcommand_t &event, const nlohmann::json &actions, const std::unordered_map<std::string, std::string> &key_values)
{
-
dpp::cluster *cluster = event.owner;
dpp::user user_ptr = event.command.get_issuing_user();
dpp::async thinking = event.co_thinking(false);
@@ -36,4 +34,6 @@ dpp::task<bool> handle_actions(const dpp::slashcommand_t &event, const nlohmann:
}
}
}
+ co_await thinking;
+ co_return true;
}
diff --git a/bot/src/main.cpp b/bot/src/main.cpp
index 42e84bd..8007998 100644
--- a/bot/src/main.cpp
+++ b/bot/src/main.cpp
@@ -1,5 +1,3 @@
-#include <dpp/dpp.h>
-#include <string>
#include "../include/utils.hpp"
#include "../include/http_webhook_server.hpp"
#include "../include/handle_actions.hpp"
diff --git a/bot/src/utils.cpp b/bot/src/utils.cpp
index 349f584..18c43b5 100644
--- a/bot/src/utils.cpp
+++ b/bot/src/utils.cpp
@@ -1,10 +1,4 @@
-#include <dpp/dpp.h>
-#include <dpp/nlohmann/json.hpp>
-#include <map>
-#include <string>
-#include <regex>
-#include <sstream>
-#include <algorithm>
+#include "../include/utils.hpp"
using namespace dpp;
namespace app
@@ -212,4 +206,39 @@ namespace app
}
return str;
}
+
+ std::string get_available_locale(std::string locale)
+ {
+ std::vector<std::string> available_locales = {"en", "fr"};
+ std::string lang = locale.substr(0, 2);
+ std::transform(lang.begin(), lang.end(), lang.begin(), ::tolower);
+ if (std::find(available_locales.begin(), available_locales.end(), lang) != available_locales.end())
+ {
+ return lang;
+ }
+ else
+ {
+ return "en"; // Default to English if not found
+ }
+ }
+
+ std::string translate(const std::string &str, const std::string &locale, const std::map<Lang, std::map<std::string, std::string>> &array_translations, const std::unordered_map<std::string, std::string> &args)
+ {
+ std::string lang = get_available_locale(locale);
+ auto it = array_translations.find(Lang::en);
+ if (it != array_translations.end())
+ {
+ auto it2 = it->second.find(str);
+ if (it2 != it->second.end())
+ {
+ std::string translation = it2->second;
+ for (const auto &arg : args)
+ {
+ translation = update_string(translation, args);
+ }
+ return translation;
+ }
+ }
+ return str; // Return the original string if no translation is found
+ }
}