diff --git a/.vscode/settings.json b/.vscode/settings.json index 9d8ea54..b3657ee 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -77,5 +77,9 @@ "future": "cpp", "ranges": "cpp", "stop_token": "cpp" - } + }, + "cSpell.words": [ + "premguild", + "resultset" + ] } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index fb40570..4c1d126 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ include(buildtools/cmake/FindPCRE.cmake) include_directories( "include" ) target_link_libraries(bot dl mysqlclient pcre dpp fmt spdlog) -set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -g -O2 -fPIC -rdynamic") +set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -g -O2 -fPIC -rdynamic -Wno-deprecated-declarations") set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2") target_link_libraries(bot) diff --git a/modules/trivia/cmd_reassignpremium.cpp b/modules/trivia/cmd_reassignpremium.cpp index da9b75a..6860752 100644 --- a/modules/trivia/cmd_reassignpremium.cpp +++ b/modules/trivia/cmd_reassignpremium.cpp @@ -48,8 +48,12 @@ void command_reassignpremium_t::call(const in_cmd &cmd, std::stringstream &token db::resultset access = db::query("SELECT * FROM trivia_access WHERE user_id = '?' AND enabled = 1", {cmd.author_id}); if (access.size() && guild_id && user_id) { - db::resultset user = db::query("SELECT * FROM premium_credits WHERE user_id = ?", {user_id}); + db::resultset user = db::query("SELECT * FROM premium_credits WHERE user_id = ? ORDER BY active DESC, since DESC", {user_id}); if (user.size()) { + std::string notice; + if (user.size() > 1) { + notice = "\n\n**WARNING**: __Multiple subscriptions for this user__. The most recent active subscription was updated."; + } if (user[0]["active"] == "0") { creator->SimpleEmbed(cmd.interaction_token, cmd.command_id, settings, ":warning:", "Premium subscription is not active!", cmd.channel_id); return; @@ -64,7 +68,7 @@ void command_reassignpremium_t::call(const in_cmd &cmd, std::stringstream &token cmd.interaction_token, cmd.command_id, settings, ":white_check_mark:", - fmt::format("Premium subscription {} reassigned to guild {}\n**Old** guild name: {}\n**New** guild name: {}", user[0]["subscription_id"], guild_id, oldname, newname), + fmt::format("Premium subscription {} reassigned to guild {}\n**Old** guild name: {}\n**New** guild name: {}{}", user[0]["subscription_id"], guild_id, oldname, newname, notice), cmd.channel_id ); } else { diff --git a/modules/trivia/cmd_subscription.cpp b/modules/trivia/cmd_subscription.cpp index 71bc9a2..3a74f80 100644 --- a/modules/trivia/cmd_subscription.cpp +++ b/modules/trivia/cmd_subscription.cpp @@ -48,7 +48,8 @@ void command_subscription_t::call(const in_cmd &cmd, std::stringstream &tokens, db::resultset access = db::query("SELECT * FROM trivia_access WHERE user_id = '?' AND enabled = 1", {cmd.author_id}); if (access.size() && user_id) { - db::resultset user = db::query("SELECT *, coalesce(date_format(since, '%d-%b-%Y %H:%i'), '') as hr_since, coalesce(date_format(cancel_date, '%d-%b-%Y %H:%i'), '') as hr_cancel_date, coalesce(date_format(payment_failed_date, '%d-%b-%Y %H:%i'), '') as hr_payment_failed_date, coalesce(date_format(manual_expiry_date, '%d-%b-%Y'), '') as hr_manual_expiry_date FROM premium_credits WHERE user_id = ?", {user_id}); + db::resultset user = db::query("SELECT *, coalesce(date_format(since, '%d-%b-%Y %H:%i'), '') as hr_since, coalesce(date_format(cancel_date, '%d-%b-%Y %H:%i'), '') as hr_cancel_date, coalesce(date_format(payment_failed_date, '%d-%b-%Y %H:%i'), '') as hr_payment_failed_date, coalesce(date_format(manual_expiry_date, '%d-%b-%Y'), '') as hr_manual_expiry_date FROM premium_credits WHERE user_id = ? ORDER BY since DESC", {user_id}); + db::resultset active_count = db::query("SELECT user_id FROM premium_credits WHERE user_id = ? AND active = 1", {user_id}); if (user.size()) { db::resultset premguild = db::query("SELECT * FROM trivia_guild_cache WHERE snowflake_id = ?", {user[0]["guild_id"]}); std::vector fields = { @@ -62,6 +63,9 @@ void command_subscription_t::call(const in_cmd &cmd, std::stringstream &tokens, { "Last Payment Failed Date", user[0]["hr_payment_failed_date"] + BLANK_EMOJI, true }, { "Manual Expiry Date", user[0]["hr_manual_expiry_date"] + BLANK_EMOJI, true }, }; + if (active_count.size() > 1) { + fields.push_back({ "WARNING", fmt::format("__{} active subscriptions for this user__. The most recent subscription is shown.{}", active_count.size(), BLANK_EMOJI), false }); + } creator->EmbedWithFields(cmd.interaction_token, cmd.command_id, settings, "Premium Subscription Details", fields, cmd.channel_id, "", "", "https://triviabot.co.uk/images/crown.png", "Subscription Information"); } else { creator->SimpleEmbed(cmd.interaction_token, cmd.command_id, settings, ":warning:", "This user does not have TriviaBot Premium!", cmd.channel_id); diff --git a/modules/trivia/numerics.cpp b/modules/trivia/numerics.cpp index e51701a..6910edf 100644 --- a/modules/trivia/numerics.cpp +++ b/modules/trivia/numerics.cpp @@ -35,7 +35,7 @@ int TriviaModule::random(int min, int max) return min + rand() % (( max + 1 ) - min); } -std::string TriviaModule::dec_to_roman(unsigned int decimal, const guild_settings_t &settings) +std::string TriviaModule::dec_to_roman(uint64_t decimal, const guild_settings_t &settings) { std::vector numbers = { 1, 4 ,5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 }; std::vector romans = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M" }; @@ -233,4 +233,4 @@ std::string TriviaModule::MakeFirstHint(const std::string &s, const guild_settin } else { return Q; } -} \ No newline at end of file +} diff --git a/modules/trivia/state.cpp b/modules/trivia/state.cpp index 771a797..5ac5a84 100644 --- a/modules/trivia/state.cpp +++ b/modules/trivia/state.cpp @@ -572,13 +572,13 @@ void state_t::do_normal_round(bool silent, const guild_settings_t& settings) question.customhint2 = currency + question.customhint2; uint32_t r = creator->random(1, 13); if ((r < 3 && from_string(question.customhint2, std::dec) <= 10000)) { - question.customhint2 = creator->dec_to_roman(from_string(question.customhint2, std::dec), settings); - } else if ((r >= 3 && r < 6) || from_string(question.customhint2, std::dec) > 10000) { - question.customhint2 = fmt::format(_("HEX", settings), from_string(question.customhint2, std::dec)); + question.customhint2 = creator->dec_to_roman(from_string(question.customhint2, std::dec), settings); + } else if ((r >= 3 && r < 6) || from_string(question.customhint2, std::dec) > 10000) { + question.customhint2 = fmt::format(_("HEX", settings), from_string(question.customhint2, std::dec)); } else if (r >= 6 && r <= 10) { - question.customhint2 = fmt::format(_("OCT", settings), from_string(question.customhint2, std::dec)); + question.customhint2 = fmt::format(_("OCT", settings), from_string(question.customhint2, std::dec)); } else { - question.customhint2 = fmt::format(_("BIN", settings), from_string(question.customhint2, std::dec)); + question.customhint2 = fmt::format(_("BIN", settings), from_string(question.customhint2, std::dec)); } } else { uint32_t r = creator->random(1, 12); diff --git a/modules/trivia/trivia.h b/modules/trivia/trivia.h index 65103e1..1476ba9 100644 --- a/modules/trivia/trivia.h +++ b/modules/trivia/trivia.h @@ -169,7 +169,7 @@ class TriviaModule : public Module virtual std::string GetVersion(); virtual std::string GetDescription(); int random(int min, int max); - std::string dec_to_roman(unsigned int decimal, const guild_settings_t &settings); + std::string dec_to_roman(uint64_t decimal, const guild_settings_t &settings); std::string tidy_num(std::string num); void UpdatePresenceLine(); std::string conv_num(std::string datain, const guild_settings_t &settings); diff --git a/modules/trivia/webrequest.cpp b/modules/trivia/webrequest.cpp index dd6d92f..330e7b8 100644 --- a/modules/trivia/webrequest.cpp +++ b/modules/trivia/webrequest.cpp @@ -726,9 +726,9 @@ void leave_team(uint64_t snowflake_id) /* Make a player join a team */ bool join_team(uint64_t snowflake_id, const std::string &team, uint64_t channel_id) { - if (check_team_exists(team)) { - auto teaminfo = db::query("SELECT * FROM teams WHERE name = '?'", {team}); - if (teaminfo.size() && teaminfo[0]["qualifying_score"].length() && from_string(teaminfo[0]["qualifying_score"], std::dec) > 0) { + auto teaminfo = db::query("SELECT * FROM teams WHERE name = '?'", {team}); + if (teaminfo.size()) { + if (teaminfo[0]["qualifying_score"].length() && from_string(teaminfo[0]["qualifying_score"], std::dec) > 0) { auto rs_score = db::query("SELECT * FROM vw_scorechart WHERE name = '?'", {team}); uint64_t score = (rs_score.size() ? from_string(rs_score[0]["score"], std::dec) : 0); if (score < from_string(teaminfo[0]["qualifying_score"], std::dec)) { @@ -743,7 +743,7 @@ bool join_team(uint64_t snowflake_id, const std::string &team, uint64_t channel_ } } db::query("DELETE FROM team_membership WHERE nick='?'", {snowflake_id}); - db::query("INSERT INTO team_membership (nick, team, joined, points_contributed) VALUES('?','?',now(),0)", {snowflake_id, team}); + db::query("INSERT INTO team_membership (nick, team, joined, points_contributed) VALUES('?','?',unix_timestamp(),0)", {snowflake_id, team}); db::query("UPDATE teams SET owner_id = '?' WHERE name = '?' AND owner_id IS NULL", {snowflake_id, team}); return true; } else { @@ -804,14 +804,6 @@ streak_t get_streak(uint64_t snowflake_id) return s; } -/* Returns true if a team name exists */ -bool check_team_exists(const std::string &team) -{ - // Replaced with direct db query for perforamance increase - 27Dec20 - db::resultset r = db::query("SELECT name FROM teams WHERE name = '?'", {team}); - return (r.size()); -} - /* Add points to a team */ void add_team_points(const std::string &team, int points, uint64_t snowflake_id) { diff --git a/modules/trivia/webrequest.h b/modules/trivia/webrequest.h index 7b69f67..694322e 100644 --- a/modules/trivia/webrequest.h +++ b/modules/trivia/webrequest.h @@ -30,7 +30,7 @@ #define BACKEND_PATH_LIVE "/api/{0}" /* Development API endpoint URL */ -#define BACKEND_HOST_DEV "beta.brainbox.cc" +#define BACKEND_HOST_DEV "https://beta.brainbox.cc" #define BACKEND_PATH_DEV "/api/{0}" using json = nlohmann::json; @@ -79,7 +79,6 @@ std::string get_current_team(uint64_t snowflake_id); void leave_team(uint64_t snowflake_id); streak_t get_streak(uint64_t snowflake_id, uint64_t guild_id); streak_t get_streak(uint64_t snowflake_id); -bool check_team_exists(const std::string &team); void add_team_points(const std::string &team, int points, uint64_t snowflake_id); uint32_t get_team_points(const std::string &team); void cache_user(const class dpp::user *_user, const class dpp::guild *_guild, const class dpp::guild_member* gi); diff --git a/src/modules.cpp b/src/modules.cpp index ab8184e..3259782 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -145,8 +145,7 @@ bool ModuleLoader::Load(const std::string &filename) if (Modules.find(filename) == Modules.end()) { char buffer[PATH_MAX + 1]; - getcwd(buffer, PATH_MAX); - std::string full_module_spec = std::string(buffer) + "/" + filename; + std::string full_module_spec = std::string(getcwd(buffer, PATH_MAX)) + "/" + filename; m.dlopen_handle = dlopen(full_module_spec.c_str(), RTLD_NOW | RTLD_LOCAL); if (!m.dlopen_handle) {