Add registration (needs leak fix), remove something from ping

This commit is contained in:
2026-08-30 17:11:36 +02:00
parent 9b7ce082c2
commit 35591b0701
5 changed files with 1166 additions and 5 deletions
+4 -1
View File
@@ -41,7 +41,10 @@ bool hammy_bot_add_command(hammy_bot_t* bot, const hammy_command_t* command);
// Looks a command up by name. Returns a pointer into the vector's storage, or // Looks a command up by name. Returns a pointer into the vector's storage, or
// NULL. Valid until the vector is next modified, which after startup is never. // NULL. Valid until the vector is next modified, which after startup is never.
const hammy_command_t* hammy_bot_find_command(const hammy_bot_t* bot, const char* name); const hammy_command_t* hammy_bot_find_command(const hammy_bot_t* bot, const char* name);
// Wipes all commands from the Discord registry. Return true on success, false on failure.
bool hammy_bot_deregister_all_commands(hammy_bot_t* bot);
// Registers all commands with Discord. Return true if succeeded or already // Registers all commands with Discord. Return true if succeeded or already
// registered; false if failure. Requires bot->application_id to be set, so call // registered; false if failure. Requires bot->application_id to be set, so call
// this from on_ready, not before. // this from on_ready, not before.
+1 -1
View File
@@ -23,7 +23,7 @@ void hammy_cmd_ping(const hammy_job_t* job, struct discord* client) {
char body[256]; char body[256];
snprintf(body, sizeof(body), snprintf(body, sizeof(body),
"Pong!\nGateway: `%d ms`\nHandled in: `%" PRId64 " ms`", "Gateway: `%d ms`\nHandled in: `%" PRId64 " ms`",
gatewayMs, handledMs); gatewayMs, handledMs);
hammy_job_respond(job, client, "Pong!", body, false); hammy_job_respond(job, client, "Pong!", body, false);
+37 -1
View File
@@ -133,6 +133,8 @@ bool hammy_bot_add_command(hammy_bot_t* bot, const hammy_command_t* command) {
return false; return false;
} }
log_info("[bot] Loaded command '%s'.", command->name);
return true; return true;
} }
@@ -181,6 +183,41 @@ bool hammy_bot_start_pool(hammy_bot_t* bot, size_t nWorkers, size_t queueCap) {
return true; return true;
} }
bool hammy_bot_deregister_all_commands(hammy_bot_t* bot) {
// TODO: This leaks and I don't know why. Figure it out.
if (!bot || !bot->client) { return false; }
struct discord_application_commands cmds = {0};
struct discord_ret_application_commands ret = { .sync = &cmds };
if (discord_get_global_application_commands(bot->client, bot->appId, &ret) == CCORD_OK) {
for (int i = 0; i < cmds.size; i++) {
discord_delete_global_application_command(bot->client, bot->appId, cmds.array[i].id, NULL);
}
} else {
log_error("[bot] Failed to deregister global commands!");
return false;
}
const char* devGuild = getenv("HAMMY_DEV_GUILD");
u64snowflake guildId = devGuild ? (u64snowflake)strtoull(devGuild, NULL, 10) : 0;
if (guildId) {
if (discord_get_guild_application_commands(bot->client, bot->appId, guildId, &ret) == CCORD_OK) {
for (int i = 0; i < cmds.size; i++) {
discord_delete_guild_application_command(bot->client, bot->appId, guildId, cmds.array[i].id, NULL);
}
} else {
log_error("[bot] Failed to deregister dev guild commands!");
return false;
}
}
bot->commandsRegistered = false;
return true;
}
bool hammy_bot_register_commands(hammy_bot_t* bot) { bool hammy_bot_register_commands(hammy_bot_t* bot) {
if (!bot || !bot->client) { return false; } if (!bot || !bot->client) { return false; }
if (bot->commandsRegistered) { return true; } // Already registered if (bot->commandsRegistered) { return true; } // Already registered
@@ -190,7 +227,6 @@ bool hammy_bot_register_commands(hammy_bot_t* bot) {
return false; return false;
} }
// TODO: Temporary - dev guild ID for testing.
const char* devGuild = getenv("HAMMY_DEV_GUILD"); const char* devGuild = getenv("HAMMY_DEV_GUILD");
u64snowflake guildId = devGuild ? (u64snowflake)strtoull(devGuild, NULL, 10) : 0; u64snowflake guildId = devGuild ? (u64snowflake)strtoull(devGuild, NULL, 10) : 0;
+2
View File
@@ -29,6 +29,8 @@ static void hammy_on_ready(struct discord* client, const struct discord_ready* e
// response editing need it. // response editing need it.
bot->appId = event->application->id; bot->appId = event->application->id;
//hammy_bot_deregister_all_commands(bot);
// discord_clone() deep-copies the gateway's *current* payload, so it only // discord_clone() deep-copies the gateway's *current* payload, so it only
// succeeds from inside a dispatch callback. READY is the first one we get. // succeeds from inside a dispatch callback. READY is the first one we get.
if (!hammy_bot_start_pool(bot, 0, 0)) { // 0, 0 = defaults if (!hammy_bot_start_pool(bot, 0, 0)) { // 0, 0 = defaults
+1122 -2
View File
File diff suppressed because it is too large Load Diff