Major reordering of the bot
This commit is contained in:
9
src/Commands/InlineDefinitions.hpp
Normal file
9
src/Commands/InlineDefinitions.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <dpp/snowflake.h>
|
||||
|
||||
// Helper methods
|
||||
inline void increaseFromUsersBalance(const dpp::snowflake userid,
|
||||
std::uint64_t amount);
|
||||
inline bool deductFromUsersBalance(const dpp::snowflake userid,
|
||||
std::uint64_t amount);
|
||||
inline std::string getUserBalance(const dpp::snowflake userid);
|
||||
inline void addUserToDatabase(const dpp::snowflake userid);
|
154
src/Commands/MoneyCommands.cpp
Normal file
154
src/Commands/MoneyCommands.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "../../settings.hpp" // This is where language is imported
|
||||
#include "../Base/SQL.hpp"
|
||||
#include "InlineDefinitions.hpp"
|
||||
|
||||
#include <dpp/appcommand.h>
|
||||
#include <dpp/cluster.h>
|
||||
#include <dpp/dispatcher.h>
|
||||
#include <dpp/snowflake.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
// Actual commands
|
||||
void commandBalance(const dpp::slashcommand_t &event) {
|
||||
std::string person, balance;
|
||||
dpp::command_value id = event.get_parameter("user");
|
||||
|
||||
// Weirdest thing ever
|
||||
if (std::holds_alternative<std::monostate>(id)) {
|
||||
balance = getUserBalance(event.command.get_issuing_user().id);
|
||||
event.reply(COMMAND_BALANCE_SELF_RESPONSE(balance));
|
||||
return;
|
||||
}
|
||||
|
||||
person = std::get<dpp::snowflake>(id).str();
|
||||
balance = getUserBalance(person);
|
||||
event.reply(COMMAND_BALANCE_SOMEONE_ELSE_RESPONSE(person, balance));
|
||||
}
|
||||
|
||||
void commandPay(const dpp::slashcommand_t &event) {
|
||||
std::string recipient =
|
||||
std::get<dpp::snowflake>(event.get_parameter("recipient")).str();
|
||||
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
||||
|
||||
// See if we can deduct the payment first
|
||||
if (!deductFromUsersBalance(event.command.get_issuing_user().id, amount)) {
|
||||
event.reply(COMMAND_PAY_FAIL_INSUFFICIENT_AMOUNT(recipient,
|
||||
std::to_string(amount)));
|
||||
return;
|
||||
}
|
||||
|
||||
// Lets pay them
|
||||
increaseFromUsersBalance(recipient, amount);
|
||||
event.reply(COMMAND_PAY_SUCCESS(recipient, std::to_string(amount)));
|
||||
}
|
||||
|
||||
void commandPrintMoney(const dpp::slashcommand_t &event) {
|
||||
std::int64_t userid = event.command.get_issuing_user().id;
|
||||
|
||||
if (ADMIN_ID != userid) {
|
||||
event.reply(COMMAND_PRINT_FAIL_NO_PRIVILIEGE(std::to_string(ADMIN_ID)));
|
||||
return;
|
||||
}
|
||||
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
||||
increaseFromUsersBalance(ADMIN_ID, amount);
|
||||
event.reply(
|
||||
COMMAND_PRINT_SUCCESS(std::to_string(ADMIN_ID), std::to_string(amount)));
|
||||
}
|
||||
|
||||
void commandBurnMoney(const dpp::slashcommand_t &event) {
|
||||
std::int64_t userid = event.command.get_issuing_user().id;
|
||||
|
||||
if (ADMIN_ID != userid) {
|
||||
event.reply(COMMAND_BURN_FAIL_NO_PRIVILIEGE(std::to_string(ADMIN_ID)));
|
||||
return;
|
||||
}
|
||||
std::uint64_t amount = std::get<std::int64_t>(event.get_parameter("amount"));
|
||||
|
||||
// Insufficient in balance
|
||||
if (!deductFromUsersBalance(ADMIN_ID, amount)) {
|
||||
event.reply(COMMAND_BURN_FAIL_INSUFFICIENT_AMOUNT(std::to_string(amount)));
|
||||
return;
|
||||
}
|
||||
|
||||
event.reply(
|
||||
COMMAND_BURN_SUCCESS(std::to_string(ADMIN_ID), std::to_string(amount)));
|
||||
}
|
||||
|
||||
void commandMoneyLeaderboard(const dpp::slashcommand_t &event) {
|
||||
std::string result;
|
||||
std::stringstream replyStream;
|
||||
execSQL("SELECT * FROM MONEY ORDER BY CASH DESC LIMIT 15;", &result);
|
||||
|
||||
replyStream << COMMAND_MONEY_LEADERBOARD_TEXT;
|
||||
|
||||
// AI code starts here
|
||||
std::stringstream ss(result);
|
||||
std::string token;
|
||||
uint8_t rank = 1;
|
||||
|
||||
while (std::getline(ss, token, ';')) {
|
||||
if (token.find("UID:") == 0) {
|
||||
std::string uid = token.substr(4); // Remove "UID:"
|
||||
|
||||
// Get the next token (CASH)
|
||||
if (std::getline(ss, token, ';')) {
|
||||
std::string cash = token.substr(5); // Remove "CASH:"
|
||||
replyStream << std::to_string(rank) << ". <@!" << uid << "> - " << cash
|
||||
<< " " << CURRENCY_NAME << "(s)"
|
||||
<< "\n";
|
||||
rank++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// AI code ends here
|
||||
// I hate parsing strings, thank you AI
|
||||
|
||||
event.reply(replyStream.str());
|
||||
}
|
||||
|
||||
///
|
||||
/// HELPER INLINE METHODS
|
||||
///
|
||||
|
||||
inline void increaseFromUsersBalance(const dpp::snowflake userid,
|
||||
std::uint64_t amount) {
|
||||
std::uint64_t balance = std::stoi(getUserBalance(userid));
|
||||
execSQL("UPDATE MONEY SET CASH=" + std::to_string(balance + amount) +
|
||||
" WHERE UID=" + userid.str());
|
||||
}
|
||||
|
||||
inline bool deductFromUsersBalance(const dpp::snowflake userid,
|
||||
std::uint64_t amount) {
|
||||
std::uint64_t balance = std::stoi(getUserBalance(userid));
|
||||
if (balance < amount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
execSQL("UPDATE MONEY SET CASH=" + std::to_string(balance - amount) +
|
||||
" WHERE UID=" + userid.str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline std::string getUserBalance(const dpp::snowflake userid) {
|
||||
std::string balance;
|
||||
execSQL("SELECT CASH FROM MONEY WHERE UID=" + userid.str(), &balance);
|
||||
|
||||
if (balance.empty()) {
|
||||
addUserToDatabase(userid);
|
||||
execSQL("SELECT CASH FROM MONEY WHERE UID=" + userid.str(),
|
||||
&balance); // We are rerunning it cus if the default starting cash
|
||||
// changes I might forget to change it here
|
||||
}
|
||||
std::uint8_t begining = balance.find(':') + 1;
|
||||
|
||||
return balance.substr(begining, balance.find(';') - begining);
|
||||
}
|
||||
|
||||
inline void addUserToDatabase(const dpp::snowflake userid) {
|
||||
execSQL("INSERT INTO MONEY (UID) VALUES (" + userid.str() + ");");
|
||||
}
|
4
src/Commands/OtherCommands.cpp
Normal file
4
src/Commands/OtherCommands.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
#include <dpp/dispatcher.h>
|
||||
|
||||
void commandPing(const dpp::slashcommand_t &event) { event.reply("Pong"); }
|
||||
void commandAbout(const dpp::slashcommand_t &event) { event.reply("Pong"); }
|
Reference in New Issue
Block a user