Added payment and basic deduction and increase of cash

This commit is contained in:
2025-07-19 02:24:15 +03:00
parent 5b9cbd5b03
commit d2c215df36
4 changed files with 93 additions and 8 deletions

View File

@@ -1,20 +1,67 @@
#include "Base/SQL.hpp"
#include "Commands.hpp"
#include <cstdint>
#include <dpp/dispatcher.h>
#include <dpp/snowflake.h>
#include <string>
void commandPing(const dpp::slashcommand_t &event) { event.reply("Pong"); }
void commandBalance(const dpp::slashcommand_t &event) {
dpp::snowflake userid = event.command.get_issuing_user().id;
std::string balance = getUserBalance(event.command.get_issuing_user().id);
event.reply(COMMAND_BALANCE_RESPONSE(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)));
}
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()) {
execSQL("INSERT INTO MONEY (UID) VALUES (" + userid.str() + ");");
addUserToDatabase(userid);
balance = "0";
} else {
std::uint8_t begining = balance.find(':') + 1;
balance = balance.substr(begining, balance.find(';') - begining);
}
event.reply(COMMAND_BALANCE_RESPONSE(balance));
return balance;
}
inline void addUserToDatabase(const dpp::snowflake userid) {
execSQL("INSERT INTO MONEY (UID) VALUES (" + userid.str() + ");");
}

View File

@@ -1,11 +1,24 @@
#include "Commands.hpp"
#include <dpp/appcommand.h>
void createCommands(const dpp::ready_t &event, dpp::cluster &bot) {
bot.global_command_create(
dpp::slashcommand("ping", "Ping-pong test", bot.me.id));
// Money related stuff
bot.guild_command_create(
dpp::slashcommand("balance", COMMAND_BALANCE_DESCRIPTION, bot.me.id),
GUILD);
bot.guild_command_create(
dpp::slashcommand("bal", COMMAND_BALANCE_DESCRIPTION, bot.me.id), GUILD);
bot.guild_command_create(
dpp::slashcommand("pay", COMMAND_PAY_DESCRIPTION, bot.me.id)
.add_option(dpp::command_option(
dpp::command_option_type::co_user, "recipient",
COMMAND_PAY_ARGS_USER_DESCRIPTION, true))
.add_option(dpp::command_option(
dpp::command_option_type::co_integer, "amount",
COMMAND_PAY_ARGS_AMOUNT_DESCRIPTION, true)
.set_min_value(1)),
GUILD);
}

View File

@@ -2,13 +2,28 @@
#include "../token.h"
#include <dpp/cluster.h>
#include <dpp/dispatcher.h>
#include <dpp/snowflake.h>
void createCommands(const dpp::ready_t &event, dpp::cluster &bot);
void commandPing(const dpp::slashcommand_t &event);
void commandBalance(const dpp::slashcommand_t &event);
void commandPay(const dpp::slashcommand_t &event);
// Inline helpers
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);
inline std::unordered_map<std::string,
std::function<void(const dpp::slashcommand_t &event)>>
Commands{{"ping", commandPing},
{"balance", commandBalance},
{"bal", commandBalance}};
Commands{
{"ping", commandPing},
{"balance", commandBalance},
{"bal", commandBalance},
{"pay", commandPay},
};