Compare commits
2 Commits
fa8c2f8401
...
d2c215df36
Author | SHA1 | Date | |
---|---|---|---|
d2c215df36 | |||
5b9cbd5b03 |
@@ -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_RESPONSE(balance) \
|
||||
"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,21 +1,67 @@
|
||||
#include "Commands.hpp"
|
||||
#include "../languages/locale_en.hpp"
|
||||
#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() + ");");
|
||||
}
|
||||
|
@@ -1,15 +1,24 @@
|
||||
#include "../languages/locale_en.hpp"
|
||||
#include "../token.h"
|
||||
#include "Commands.hpp"
|
||||
#include <dpp/cluster.h>
|
||||
#include <dpp/dispatcher.h>
|
||||
#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);
|
||||
}
|
||||
|
@@ -1,11 +1,29 @@
|
||||
#include "../languages/locale_en.hpp"
|
||||
#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},
|
||||
};
|
||||
|
Reference in New Issue
Block a user