mirror of
https://github.com/Syping/dtranslatebot.git
synced 2026-04-01 13:20:40 +02:00
add user support to database
This commit is contained in:
parent
3546eed2dd
commit
bb5adf587c
10 changed files with 205 additions and 12 deletions
|
|
@ -102,6 +102,22 @@ std::vector<dpp::snowflake> database::get_guilds()
|
|||
return {};
|
||||
}
|
||||
|
||||
bot::settings::user database::get_user(dpp::snowflake user_id)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cerr << "[Debug] database::get_user(dpp::snowflake) have being called." << std::endl;
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<dpp::snowflake> database::get_users()
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cerr << "[Debug] database::get_users() have being called." << std::endl;
|
||||
#endif
|
||||
return {};
|
||||
}
|
||||
|
||||
void database::set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &source)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
|
|
@ -109,6 +125,13 @@ void database::set_channel_source(dpp::snowflake guild_id, dpp::snowflake channe
|
|||
#endif
|
||||
}
|
||||
|
||||
void database::set_user_target(dpp::snowflake user_id, const std::string &target)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
std::cerr << "[Debug] database::set_user_target(dpp::snowflake, const std::string&) have being called." << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool database::sync()
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
|
|
|
|||
|
|
@ -48,7 +48,10 @@ namespace bot {
|
|||
virtual std::vector<bot::settings::target> get_channel_targets(dpp::snowflake guild_id, dpp::snowflake channel_id);
|
||||
*/
|
||||
virtual std::vector<dpp::snowflake> get_guilds();
|
||||
virtual bot::settings::user get_user(dpp::snowflake user_id);
|
||||
virtual std::vector<dpp::snowflake> get_users();
|
||||
virtual void set_channel_source(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &source);
|
||||
virtual void set_user_target(dpp::snowflake user_id, const std::string &target);
|
||||
virtual bool sync();
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@
|
|||
*****************************************************************************/
|
||||
|
||||
#include "http_request.h"
|
||||
using namespace std::string_literals;
|
||||
|
||||
http_request::http_request() {
|
||||
instance = curl_easy_init();
|
||||
|
|
@ -56,7 +55,7 @@ const http_response http_request::post(const std::string &url, const std::string
|
|||
curl_easy_setopt(instance, CURLOPT_URL, url.c_str());
|
||||
curl_slist *header_slist = nullptr;
|
||||
if (!content_type.empty()) {
|
||||
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string("Content-Type: "s + content_type).c_str());
|
||||
curl_slist *new_header_slist = curl_slist_append(header_slist, std::string("Content-Type: " + content_type).c_str());
|
||||
if (!new_header_slist) {
|
||||
curl_slist_free_all(header_slist);
|
||||
throw std::bad_alloc();
|
||||
|
|
@ -88,7 +87,7 @@ const http_response http_request::post(const std::string &url, const std::string
|
|||
}
|
||||
|
||||
std::string http_request::legacy_url(const std::string &hostname, uint16_t port, const std::string &url, bool tls) {
|
||||
return (tls ? "https://"s : "http://"s) + hostname + ":"s + std::to_string(port) + (url.empty() ? "/"s : (url.front() != '/' ? "/"s + url : url));
|
||||
return (tls ? "https://" : "http://") + hostname + ":" + std::to_string(port) + (url.empty() ? "/" : (url.front() != '/' ? "/" + url : url));
|
||||
}
|
||||
|
||||
http_request::~http_request() {
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ void process_database_channels(std::shared_ptr<bot::database::database> database
|
|||
}
|
||||
}
|
||||
|
||||
void process_database(std::shared_ptr<bot::database::database> database, std::vector<guild> &guilds, std::vector<dpp::snowflake> &webhookIds)
|
||||
void process_database(std::shared_ptr<bot::database::database> database, std::vector<guild> &guilds, std::vector<user> &users, std::vector<dpp::snowflake> &webhookIds)
|
||||
{
|
||||
std::cout << "[Launch] Loading database..." << std::endl;
|
||||
const std::vector<dpp::snowflake> db_guilds = database->get_guilds();
|
||||
|
|
@ -86,6 +86,13 @@ void process_database(std::shared_ptr<bot::database::database> database, std::ve
|
|||
guilds.push_back(std::move(guild));
|
||||
}
|
||||
}
|
||||
const std::vector<dpp::snowflake> db_users = database->get_users();
|
||||
for (auto db_user_id = db_users.begin(); db_user_id != db_users.end(); db_user_id++) {
|
||||
bot::settings::user user = database->get_user(*db_user_id);
|
||||
if (user.target.empty())
|
||||
user.target = "en";
|
||||
users.push_back(std::move(user));
|
||||
}
|
||||
}
|
||||
|
||||
void process_guild_settings(const dpp::json &json, std::vector<guild> &guilds, std::vector<dpp::snowflake> &webhookIds)
|
||||
|
|
@ -485,6 +492,20 @@ const target* settings::get_target(const channel *channel, const std::string &ta
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
user* settings::get_user(dpp::snowflake user_id) {
|
||||
if (!m_externallyLockedCount) {
|
||||
#ifndef NDEBUG
|
||||
std::cerr << "[Debug] settings::get_user(dpp::snowflake) have being called without settings being locked." << std::endl;
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
for (auto user = m_users.begin(); user != m_users.end(); user++) {
|
||||
if (user->id == user_id)
|
||||
return &*user;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::vector<std::string> settings::preferred_languages() const
|
||||
{
|
||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||
|
|
@ -509,6 +530,19 @@ const std::string settings::token() const
|
|||
return m_token;
|
||||
}
|
||||
|
||||
void settings::set_user_target(dpp::snowflake user_id, std::string target) {
|
||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||
for (auto user = m_users.begin(); user != m_users.end(); user++) {
|
||||
if (user->id == user_id) {
|
||||
user->target = target;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// We will create the user structure when it is not in memory
|
||||
m_users.push_back({ user_id, target });
|
||||
}
|
||||
|
||||
bool settings::is_translatebot(dpp::snowflake webhook_id) const
|
||||
{
|
||||
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
|
||||
|
|
@ -582,7 +616,7 @@ bool settings::parse(const std::string &data, bool initialize)
|
|||
if (json_user != json.end() && json_user->is_object())
|
||||
process_user_settings(*json_user, m_avatarSize);
|
||||
|
||||
process_database(m_database, m_guilds, m_webhookIds);
|
||||
process_database(m_database, m_guilds, m_users, m_webhookIds);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,11 +45,15 @@ namespace bot {
|
|||
target* get_target(dpp::snowflake guild_id, dpp::snowflake channel_id, const std::string &target);
|
||||
static target* get_target(channel *channel, const std::string &target);
|
||||
static const target* get_target(const channel *channel, const std::string &target);
|
||||
user* get_user(dpp::snowflake user_id);
|
||||
const std::vector<std::string> preferred_languages() const;
|
||||
std::shared_ptr<bot::database::database> get_database() const;
|
||||
std::shared_ptr<bot::translator::translator> get_translator() const;
|
||||
const std::string token() const;
|
||||
|
||||
/* set functions */
|
||||
void set_user_target(dpp::snowflake user_id, std::string target);
|
||||
|
||||
/* is functions */
|
||||
bool is_translatebot(dpp::snowflake webhook_id) const;
|
||||
|
||||
|
|
@ -75,6 +79,7 @@ namespace bot {
|
|||
std::vector<std::string> m_prefLangs;
|
||||
std::shared_ptr<bot::translator::translator> m_translator;
|
||||
std::string m_token;
|
||||
std::vector<user> m_users;
|
||||
std::vector<dpp::snowflake> m_webhookIds;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,10 @@ namespace bot {
|
|||
dpp::snowflake id;
|
||||
std::vector<bot::settings::channel> channel;
|
||||
};
|
||||
struct user {
|
||||
dpp::snowflake id;
|
||||
std::string target;
|
||||
};
|
||||
struct translator {
|
||||
std::string type;
|
||||
std::string hostname;
|
||||
|
|
|
|||
|
|
@ -43,15 +43,15 @@ void bot::webhook_push::run(const bot::translated_guild_message &message, dpp::c
|
|||
}
|
||||
else {
|
||||
bot::svmatch match;
|
||||
if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*(\\.|\\?|\\!|\\。)\\s.*$"s))) {
|
||||
if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*(\\.|\\?|\\!|\\。)\\s.*$"))) {
|
||||
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
||||
message_v = message_v.substr(1334 + match.position(1));
|
||||
}
|
||||
else if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*(\\,)\\s.*$"s))) {
|
||||
else if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*(\\,)\\s.*$"))) {
|
||||
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
||||
message_v = message_v.substr(1334 + match.position(1));
|
||||
}
|
||||
else if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*()\\s.*$"s))) {
|
||||
else if (bot::regex_match(message_eov.begin(), message_eov.end(), match, bot::regex("^.*()\\s.*$"))) {
|
||||
json_body["content"] = message_v.substr(0, 1334 + match.position(1));
|
||||
message_v = message_v.substr(1334 + match.position(1));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue