Added payment and basic deduction and increase of cash
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
#define CURRENCY_NAME "The Night Coin"
|
#define CURRENCY_NAME "the night coin"
|
||||||
|
|
||||||
#define COMMAND_BALANCE_DESCRIPTION "See your balance of " CURRENCY_NAME
|
#define COMMAND_BALANCE_DESCRIPTION "See your balance of " CURRENCY_NAME
|
||||||
#define COMMAND_BALANCE_RESPONSE(balance) \
|
#define COMMAND_BALANCE_RESPONSE(balance) \
|
||||||
"You currently have " + balance + " " CURRENCY_NAME "(s)"
|
"You currently have " + balance + " " CURRENCY_NAME "(s)"
|
||||||
|
|
||||||
|
#define COMMAND_PAY_DESCRIPTION "Send someone some amount of " CURRENCY_NAME "s"
|
||||||
|
#define COMMAND_PAY_ARGS_USER_DESCRIPTION "Who do you want to pay"
|
||||||
|
#define COMMAND_PAY_ARGS_AMOUNT_DESCRIPTION "How much do you want to pay"
|
||||||
|
#define COMMAND_PAY_FAIL_INSUFFICIENT_AMOUNT(recipient, amount) \
|
||||||
|
"You do not have the balance to send <@!" + recipient + "> " + amount + \
|
||||||
|
" " CURRENCY_NAME "(s)!"
|
||||||
|
#define COMMAND_PAY_SUCCESS(recipient, amount) \
|
||||||
|
"Successfully sent <@!" + recipient + "> " + amount + " " CURRENCY_NAME "(s)!"
|
||||||
|
@@ -1,20 +1,67 @@
|
|||||||
#include "Base/SQL.hpp"
|
#include "Base/SQL.hpp"
|
||||||
#include "Commands.hpp"
|
#include "Commands.hpp"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <dpp/dispatcher.h>
|
||||||
#include <dpp/snowflake.h>
|
#include <dpp/snowflake.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
void commandPing(const dpp::slashcommand_t &event) { event.reply("Pong"); }
|
void commandPing(const dpp::slashcommand_t &event) { event.reply("Pong"); }
|
||||||
void commandBalance(const dpp::slashcommand_t &event) {
|
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;
|
std::string balance;
|
||||||
execSQL("SELECT CASH FROM MONEY WHERE UID=" + userid.str(), &balance);
|
execSQL("SELECT CASH FROM MONEY WHERE UID=" + userid.str(), &balance);
|
||||||
|
|
||||||
if (balance.empty()) {
|
if (balance.empty()) {
|
||||||
execSQL("INSERT INTO MONEY (UID) VALUES (" + userid.str() + ");");
|
addUserToDatabase(userid);
|
||||||
balance = "0";
|
balance = "0";
|
||||||
} else {
|
} else {
|
||||||
std::uint8_t begining = balance.find(':') + 1;
|
std::uint8_t begining = balance.find(':') + 1;
|
||||||
balance = balance.substr(begining, balance.find(';') - begining);
|
balance = balance.substr(begining, balance.find(';') - begining);
|
||||||
}
|
}
|
||||||
|
return balance;
|
||||||
event.reply(COMMAND_BALANCE_RESPONSE(balance));
|
}
|
||||||
|
|
||||||
|
inline void addUserToDatabase(const dpp::snowflake userid) {
|
||||||
|
execSQL("INSERT INTO MONEY (UID) VALUES (" + userid.str() + ");");
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,24 @@
|
|||||||
#include "Commands.hpp"
|
#include "Commands.hpp"
|
||||||
|
#include <dpp/appcommand.h>
|
||||||
|
|
||||||
void createCommands(const dpp::ready_t &event, dpp::cluster &bot) {
|
void createCommands(const dpp::ready_t &event, dpp::cluster &bot) {
|
||||||
bot.global_command_create(
|
bot.global_command_create(
|
||||||
dpp::slashcommand("ping", "Ping-pong test", bot.me.id));
|
dpp::slashcommand("ping", "Ping-pong test", bot.me.id));
|
||||||
|
|
||||||
|
// Money related stuff
|
||||||
bot.guild_command_create(
|
bot.guild_command_create(
|
||||||
dpp::slashcommand("balance", COMMAND_BALANCE_DESCRIPTION, bot.me.id),
|
dpp::slashcommand("balance", COMMAND_BALANCE_DESCRIPTION, bot.me.id),
|
||||||
GUILD);
|
GUILD);
|
||||||
bot.guild_command_create(
|
bot.guild_command_create(
|
||||||
dpp::slashcommand("bal", COMMAND_BALANCE_DESCRIPTION, bot.me.id), GUILD);
|
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);
|
||||||
}
|
}
|
||||||
|
@@ -2,13 +2,28 @@
|
|||||||
#include "../token.h"
|
#include "../token.h"
|
||||||
#include <dpp/cluster.h>
|
#include <dpp/cluster.h>
|
||||||
#include <dpp/dispatcher.h>
|
#include <dpp/dispatcher.h>
|
||||||
|
#include <dpp/snowflake.h>
|
||||||
|
|
||||||
void createCommands(const dpp::ready_t &event, dpp::cluster &bot);
|
void createCommands(const dpp::ready_t &event, dpp::cluster &bot);
|
||||||
void commandPing(const dpp::slashcommand_t &event);
|
void commandPing(const dpp::slashcommand_t &event);
|
||||||
void commandBalance(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,
|
inline std::unordered_map<std::string,
|
||||||
std::function<void(const dpp::slashcommand_t &event)>>
|
std::function<void(const dpp::slashcommand_t &event)>>
|
||||||
Commands{{"ping", commandPing},
|
Commands{
|
||||||
|
{"ping", commandPing},
|
||||||
{"balance", commandBalance},
|
{"balance", commandBalance},
|
||||||
{"bal", commandBalance}};
|
{"bal", commandBalance},
|
||||||
|
{"pay", commandPay},
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user