SQL, Language, Organising commands

This commit is contained in:
2025-07-18 23:01:18 +03:00
parent b81b6580a9
commit e293909ffa
6 changed files with 71 additions and 6 deletions

View File

@@ -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

3
src/Commands.cpp Normal file
View File

@@ -0,0 +1,3 @@
#include "Commands.hpp"
void commandPing(const dpp::slashcommand_t &event) { event.reply("Pong"); }

6
src/Commands.hpp Normal file
View File

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

27
src/SQL.hpp Normal file
View File

@@ -0,0 +1,27 @@
#include <iostream>
#include <sqlite3.h>
#include <string>
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);
}
}

2
src/local_en.hpp Normal file
View File

@@ -0,0 +1,2 @@
#define CURRENCY_NAME "The Night Coin"
#define COMMAND_BALANCE_DESCRIPTION "See your balance of " CURRENCY_NAME

View File

@@ -1,25 +1,49 @@
#include "../token.h"
#include "Commands.hpp"
#include "SQL.hpp"
#include "local_en.hpp"
#include <dpp/cluster.h>
#include <dpp/dispatcher.h>
#include <dpp/dpp.h>
#include <dpp/once.h>
#include <string>
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<struct register_bot_commands>()) {
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<struct clear_bot_commands>()) {
// bot.global_command_delete(1395839332220408051);
}
});