From e293909ffa0fe0c972e50aaa84f76a26633093ec Mon Sep 17 00:00:00 2001 From: cat Date: Fri, 18 Jul 2025 23:01:18 +0300 Subject: [PATCH] SQL, Language, Organising commands --- CMakeLists.txt | 9 ++++++--- src/Commands.cpp | 3 +++ src/Commands.hpp | 6 ++++++ src/SQL.hpp | 27 +++++++++++++++++++++++++++ src/local_en.hpp | 2 ++ src/main.cpp | 30 +++++++++++++++++++++++++++--- 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/Commands.cpp create mode 100644 src/Commands.hpp create mode 100644 src/SQL.hpp create mode 100644 src/local_en.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f8b09f2..5abd355 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,16 +6,19 @@ project(TheBartender VERSION 1.0) list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) # Create an executable -add_executable(${PROJECT_NAME} src/main.cpp) +add_executable(${PROJECT_NAME} src/main.cpp src/Commands.cpp) # Find our pre-installed DPP package (using FindDPP.cmake). find_package(DPP REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(SQLITE3 REQUIRED sqlite3) # Link the pre-installed DPP package. -target_link_libraries(${PROJECT_NAME} ${DPP_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} ${DPP_LIBRARIES} ${SQLITE3_LIBRARIES}) # Include the DPP directories. -target_include_directories(${PROJECT_NAME} PRIVATE ${DPP_INCLUDE_DIR}) +target_include_directories(${PROJECT_NAME} PRIVATE ${DPP_INCLUDE_DIR} + ${SQLITE3_INCLUDE_DIR}) # Set C++ version set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20 diff --git a/src/Commands.cpp b/src/Commands.cpp new file mode 100644 index 0000000..f02145e --- /dev/null +++ b/src/Commands.cpp @@ -0,0 +1,3 @@ +#include "Commands.hpp" + +void commandPing(const dpp::slashcommand_t &event) { event.reply("Pong"); } diff --git a/src/Commands.hpp b/src/Commands.hpp new file mode 100644 index 0000000..3fbd6ab --- /dev/null +++ b/src/Commands.hpp @@ -0,0 +1,6 @@ +#include +void commandPing(const dpp::slashcommand_t &event); + +inline std::unordered_map> + Commands{{"ping", commandPing}}; diff --git a/src/SQL.hpp b/src/SQL.hpp new file mode 100644 index 0000000..d7aff38 --- /dev/null +++ b/src/SQL.hpp @@ -0,0 +1,27 @@ +#include +#include +#include + +inline sqlite3 *database; + +// Its invoked per returned row/record +// typeCount is each column +static int callback(void *deadWeight, int typeCount, char **value, char **key) { + for (int x = 0; x < typeCount; x++) { + std::cout << key[x] << " " << value[x] << "\n"; + } + return 0; +} + +static void execSQL(std::string sql) { + int errorCode = 0; + errorCode = sqlite3_exec(database, sql.c_str(), callback, 0, NULL); + if (errorCode == 19) { + return; + } + + if (errorCode) { + std::cerr << sqlite3_errmsg(database) << " exiting!\n"; + exit(1); + } +} diff --git a/src/local_en.hpp b/src/local_en.hpp new file mode 100644 index 0000000..97c16c8 --- /dev/null +++ b/src/local_en.hpp @@ -0,0 +1,2 @@ +#define CURRENCY_NAME "The Night Coin" +#define COMMAND_BALANCE_DESCRIPTION "See your balance of " CURRENCY_NAME diff --git a/src/main.cpp b/src/main.cpp index 83f2748..c3cf217 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,25 +1,49 @@ #include "../token.h" +#include "Commands.hpp" +#include "SQL.hpp" +#include "local_en.hpp" + #include #include #include #include +#include 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)"); + + // Bot setup bullshit dpp::cluster bot(TOKEN); // Neat utility bot.on_log(dpp::utility::cout_logger()); bot.on_slashcommand([](const dpp::slashcommand_t &event) { - if (event.command.get_command_name() == "test") { - event.reply("Recieved"); + auto command = Commands.find(event.command.get_command_name()); + if (command != Commands.end()) { + command->second(event); + return; } + event.reply("Could not find that command :("); }); bot.on_ready([&bot](const dpp::ready_t &event) { if (dpp::run_once()) { bot.global_command_create( - dpp::slashcommand("test", "quick test", bot.me.id)); + dpp::slashcommand("ping", "Ping-pong test", bot.me.id)); + bot.guild_command_create( + dpp::slashcommand("balance", COMMAND_BALANCE_DESCRIPTION, bot.me.id), + GUILD); + } + + if (dpp::run_once()) { + // bot.global_command_delete(1395839332220408051); } });