Reorganised databases

This commit is contained in:
2025-07-19 01:16:09 +03:00
parent b8b3ccfbcb
commit a0468758ad
5 changed files with 39 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
#include "../../languages/locale_en.hpp"
#include "../../token.h"
#include "../Commands.hpp"
#include "SQL.hpp"
#include "../Databases.hpp"
#include <dpp/cluster.h>
#include <dpp/dispatcher.h>
@@ -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);

View File

@@ -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<std::string *>(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;
}

View File

@@ -1,3 +1,21 @@
#include "Commands.hpp"
#include "../languages/locale_en.hpp"
#include "Base/SQL.hpp"
#include <dpp/snowflake.h>
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)");
}

View File

@@ -1,6 +1,7 @@
#include <dpp/dispatcher.h>
void commandPing(const dpp::slashcommand_t &event);
void commandBalance(const dpp::slashcommand_t &event);
inline std::unordered_map<std::string,
std::function<void(const dpp::slashcommand_t &event)>>
Commands{{"ping", commandPing}};
Commands{{"ping", commandPing}, {"balance", commandBalance}};

10
src/Databases.hpp Normal file
View File

@@ -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);
}