some infra

This commit is contained in:
2026-08-27 21:47:03 +02:00
parent 7c2a2aecd1
commit 73809eaa7c
61 changed files with 253 additions and 17 deletions
+80
View File
@@ -0,0 +1,80 @@
#include <hammy/bot.h>
hammy_bot_t* hammy_bot_create() {
hammy_bot_t* bot = (hammy_bot_t*)malloc(sizeof(hammy_bot_t));
if (!bot) {
return NULL;
}
// Set defaults
bot->client = NULL;
bot->commands_registered = false;
bot->commands = vector_create(sizeof(hammy_command_t));
// Check if vector allocation errored
if (!bot->commands) {
free(bot);
return NULL;
}
if (vector_set_destructor(bot->commands, &hammy_command_destroy) < 0) {
vector_destroy(&bot->commands);
free(bot);
return NULL;
}
// Init the discord client - even though this is an owning reference, the main() and others may call functions on it (if we don't stay singlethread anymore then FIX THIS)
bot->client = discord_from_json("config.json");
if (!bot->client) {
vector_destroy(&bot->commands);
free(bot);
return NULL;
}
return bot;
}
bool hammy_bot_set_on_ready(hammy_bot_t* bot, void (*func)(struct discord* client, const struct discord_ready* event)) {
if (!bot || !func || !bot->client) { return false; }
discord_set_on_ready(bot->client, func);
return true;
}
bool hammy_bot_run(hammy_bot_t* bot) {
if (!bot || !bot->client) { return false; }
discord_run(bot->client);
return true;
}
bool hammy_bot_add_command(hammy_bot_t* bot, hammy_command_t* command) {
if (!bot || !command) { return false; }
// Push into commands vector
if (vector_push_back(bot->commands, command) < 0) {
return false;
}
return true;
}
bool hammy_bot_register_commands(hammy_bot_t* bot) {
if (!bot) { return false; }
// TODO: Figure out registration logic
}
bool hammy_bot_destroy(hammy_bot_t** bot) {
if (!bot || !(*bot)) { return false; }
if ((*bot)->client) {
discord_cleanup((*bot)->client);
}
// TODO: Potential other stuff here
vector_destroy(&(*bot)->commands); // commands is NULL form hereon, calls destructors on commands automatically
free(*bot);
*bot = NULL;
}
+24
View File
@@ -0,0 +1,24 @@
#include <hammy/command.h>
hammy_command_t* hammy_command_create() {
hammy_command_t* cmd = (hammy_command_t*)malloc(sizeof(hammy_command_t));
if (!cmd) {
return NULL;
}
// TODO: set defaults
return cmd;
}
void hammy_command_destroy(void* cmd) {
if (!cmd) {
return;
}
cmd = (hammy_command_t*)cmd;
// TODO: Other things here eventually
free(cmd);
}
+28 -6
View File
@@ -1,21 +1,43 @@
#include <string.h>
#include <concord/discord.h>
#include <signal.h>
#include <concord/log.h>
#include <hammy/bot.h>
static void on_signal(int sig) {
(void)sig;
discord_shutdown_all();
}
void on_ready(struct discord* client, const struct discord_ready* event) {
(void)client;
log_info("Logged in as %s", event->user->username);
}
int main(void) {
struct sigaction sa = { 0 };
sa.sa_handler = &on_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0; /* deliberately no SA_RESTART */
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
ccord_global_init();
struct discord* client = discord_config_init("config.json");
hammy_bot_t* bot = hammy_bot_create();
if (!bot) {
log_error("Hammy Bot creation returned NULL! Bailing!");
ccord_global_cleanup();
discord_set_on_ready(client, &on_ready);
return 1;
}
hammy_bot_set_on_ready(bot, &on_ready);
hammy_bot_run(bot); // This will block and exit afterwards
hammy_bot_destroy(&bot);
discord_run(client);
discord_cleanup(client);
ccord_global_cleanup();
return 0;