diff --git a/languages/locale_en.hpp b/languages/locale_en.hpp index 50d8c15..31d94e4 100644 --- a/languages/locale_en.hpp +++ b/languages/locale_en.hpp @@ -32,12 +32,13 @@ "Failed to download the background image! Aborting." RESPONSE_NO_CHARGE #define COMMAND_GENERATE_REPORT_FAIL_IMAGE_LOAD \ "Failed to load the background image! Aborting." RESPONSE_NO_CHARGE - +#define COMMAND_GENERATE_REPORT_FAIL_INSUFFICIENT_BALANCE(amount) \ + "You do not have " + amount + " " CURRENCY_NAME "(s) to afford this." #define COMMAND_GENERATE_REPORT_CONFIRMATION_QUESTION(price, imageURL, \ imageMIME, headline) \ "This image generation will cost you " + price + \ " " CURRENCY_NAME \ - "(s).\nAre you sure you want to continue?\nHeadline: " + \ + "(s). Are you sure you want to continue?\nHeadline: " + \ headline + "\n[" + imageMIME + "](" + imageURL + ")" // Command get_pfp diff --git a/src/Commands/GenerativeCommands.cpp b/src/Commands/GenerativeCommands.cpp index 14c8f4c..9ce454c 100644 --- a/src/Commands/GenerativeCommands.cpp +++ b/src/Commands/GenerativeCommands.cpp @@ -1,5 +1,6 @@ #include "../Common.hpp" #include "../Utility/CairoTools.hpp" +#include "TransactionMethods.hpp" #include #include @@ -11,6 +12,16 @@ void commandGenerateReport(const dpp::slashcommand_t &event, dpp::cluster &bot) { event.thinking(); + auto issuer = event.command.get_issuing_user().id; + // Insufficient balance + if (!checkFromUsersBalance(issuer, COMMAND_GENERATE_REPORT_COST)) { + event.edit_response(COMMAND_GENERATE_REPORT_FAIL_INSUFFICIENT_BALANCE( + std::to_string(COMMAND_GENERATE_REPORT_COST))); + return; + } + deductFromUsersBalance(issuer, COMMAND_GENERATE_REPORT_COST); + + // Start processing dpp::snowflake fileId = std::get(event.get_parameter("image")); dpp::attachment file = event.command.get_resolved_attachment(fileId); diff --git a/src/Commands/MoneyCommands.cpp b/src/Commands/MoneyCommands.cpp index 7d56e18..6b784d6 100644 --- a/src/Commands/MoneyCommands.cpp +++ b/src/Commands/MoneyCommands.cpp @@ -34,12 +34,13 @@ void commandPay(const dpp::slashcommand_t &event, dpp::cluster &bot) { std::get(event.get_parameter("recipient")).str(); std::uint64_t amount = std::get(event.get_parameter("amount")); - // See if we can deduct the payment first - if (!deductFromUsersBalance(event.command.get_issuing_user().id, amount)) { + // Insufficient balance + if (!checkFromUsersBalance(ADMIN_ID, amount)) { event.reply(COMMAND_PAY_FAIL_INSUFFICIENT_AMOUNT(recipient, std::to_string(amount))); return; } + deductFromUsersBalance(ADMIN_ID, amount); // Lets pay them increaseFromUsersBalance(recipient, amount); @@ -68,12 +69,13 @@ void commandBurnMoney(const dpp::slashcommand_t &event, dpp::cluster &bot) { } std::uint64_t amount = std::get(event.get_parameter("amount")); - // Insufficient in balance - if (!deductFromUsersBalance(ADMIN_ID, amount)) { + // Insufficient balance + if (!checkFromUsersBalance(ADMIN_ID, amount)) { event.reply(COMMAND_BURN_FAIL_INSUFFICIENT_AMOUNT(std::to_string(amount))); return; } + deductFromUsersBalance(ADMIN_ID, amount); event.reply( COMMAND_BURN_SUCCESS(std::to_string(ADMIN_ID), std::to_string(amount))); } @@ -122,16 +124,15 @@ void increaseFromUsersBalance(const dpp::snowflake userid, " WHERE UID=" + userid.str()); } -bool deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount) { +void deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount) { std::uint64_t balance = std::stoll(getUserBalance(userid)); - if (balance < amount) { - return false; - } - execSQL("UPDATE MONEY SET CASH=" + std::to_string(balance - amount) + " WHERE UID=" + userid.str()); +} - return true; +bool checkFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount) { + std::uint64_t balance = std::stoll(getUserBalance(userid)); + return balance >= amount; } std::string getUserBalance(const dpp::snowflake userid) { diff --git a/src/Commands/TransactionMethods.hpp b/src/Commands/TransactionMethods.hpp index 9e4a8d8..1014b38 100644 --- a/src/Commands/TransactionMethods.hpp +++ b/src/Commands/TransactionMethods.hpp @@ -3,6 +3,8 @@ // Helper methods void increaseFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount); -bool deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount); +void deductFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount); +bool checkFromUsersBalance(const dpp::snowflake userid, std::uint64_t amount); std::string getUserBalance(const dpp::snowflake userid); + void addUserToDatabase(const dpp::snowflake userid);