From a0468758adf4cc0d5cfb4994dc9634016efe85b2 Mon Sep 17 00:00:00 2001 From: cat Date: Sat, 19 Jul 2025 01:16:09 +0300 Subject: [PATCH] Reorganised databases --- src/Base/Entry.cpp | 9 ++------- src/Base/SQL.hpp | 11 +++++++---- src/Commands.cpp | 18 ++++++++++++++++++ src/Commands.hpp | 3 ++- src/Databases.hpp | 10 ++++++++++ 5 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/Databases.hpp diff --git a/src/Base/Entry.cpp b/src/Base/Entry.cpp index 7b94e82..ae9b6d6 100644 --- a/src/Base/Entry.cpp +++ b/src/Base/Entry.cpp @@ -1,7 +1,7 @@ #include "../../languages/locale_en.hpp" #include "../../token.h" #include "../Commands.hpp" -#include "SQL.hpp" +#include "../Databases.hpp" #include #include @@ -11,12 +11,7 @@ int main(int argc, char **argv) { // SQL database set up - sqlite3_open("discordServer.db", &database); - - // Fuck dude capitalism 😔😔😔 - execSQL("CREATE TABLE IF NOT EXISTS MONEY(" - "UID INT PRIMARY KEY NOT NULL," - "Cash INT NOT NULL DEFAULT 0)"); + makeDatabases(); // Bot setup bullshit dpp::cluster bot(TOKEN); diff --git a/src/Base/SQL.hpp b/src/Base/SQL.hpp index f80f06b..b8ed6e3 100644 --- a/src/Base/SQL.hpp +++ b/src/Base/SQL.hpp @@ -6,16 +6,19 @@ inline sqlite3 *database; // Its invoked per returned row/record // typeCount is each column -static int callback(void *deadWeight, int typeCount, char **value, char **key) { +// +// Results come out as KEY:VALUE; +static int callback(void *output, int typeCount, char **value, char **key) { + std::string *out = static_cast(output); for (int x = 0; x < typeCount; x++) { - std::cout << key[x] << " " << value[x] << "\n"; + *out += std::string(key[x]) + ":" + std::string(value[x]) + ";"; } return 0; } -static void execSQL(std::string sql) { +static void execSQL(std::string sql, std::string *result = nullptr) { int errorCode = 0; - errorCode = sqlite3_exec(database, sql.c_str(), callback, 0, NULL); + errorCode = sqlite3_exec(database, sql.c_str(), callback, result, NULL); if (errorCode == 19) { return; } diff --git a/src/Commands.cpp b/src/Commands.cpp index f02145e..258aee9 100644 --- a/src/Commands.cpp +++ b/src/Commands.cpp @@ -1,3 +1,21 @@ #include "Commands.hpp" +#include "../languages/locale_en.hpp" +#include "Base/SQL.hpp" +#include 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; + execSQL("SELECT CASH FROM MONEY WHERE UID=" + userid.str(), &balance); + + if (balance.empty()) { + execSQL("INSERT INTO MONEY (UID) VALUES (" + userid.str() + ");"); + balance = "0"; + } else { + std::uint8_t begining = balance.find(':') + 1; + balance = balance.substr(begining, balance.find(';') - begining); + } + + event.reply("You have " + balance + " " + CURRENCY_NAME + "(s)"); +} diff --git a/src/Commands.hpp b/src/Commands.hpp index 3fbd6ab..2c6364c 100644 --- a/src/Commands.hpp +++ b/src/Commands.hpp @@ -1,6 +1,7 @@ #include void commandPing(const dpp::slashcommand_t &event); +void commandBalance(const dpp::slashcommand_t &event); inline std::unordered_map> - Commands{{"ping", commandPing}}; + Commands{{"ping", commandPing}, {"balance", commandBalance}}; diff --git a/src/Databases.hpp b/src/Databases.hpp new file mode 100644 index 0000000..4650399 --- /dev/null +++ b/src/Databases.hpp @@ -0,0 +1,10 @@ +#include "Base/SQL.hpp" + +#define MAKE_DATABASE "CREATE TABLE IF NOT EXISTS " +#define DATABASE_MONEY \ + "MONEY(UID INT PRIMARY KEY NOT NULL, CASH INT NOT NULL DEFAULT 0)" + +inline void makeDatabases() { + sqlite3_open("bot.db", &database); + execSQL(MAKE_DATABASE DATABASE_MONEY); +}