commands
This commit is contained in:
+43
-16
@@ -1,24 +1,51 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
// Handlers live in src/hammy/commands/. Declared here rather than in a header
|
||||
// so adding a command touches exactly two places: its own .c file and this
|
||||
// table.
|
||||
void hammy_cmd_ping(const hammy_job_t* job, struct discord* client);
|
||||
|
||||
// TODO: set defaults
|
||||
|
||||
return cmd;
|
||||
// The command table. Pure data - no allocation, no lifetime, no destructor.
|
||||
// Copying an entry into the bot's vector is a plain struct copy, and the vector
|
||||
// may be created with a NULL destructor hook.
|
||||
//
|
||||
// Field order: name, description, options, handler, instant.
|
||||
static const hammy_command_t hammy_builtin_commands[] = {
|
||||
{ "ping", "Check whether Hammy is alive and how fast it is responding",
|
||||
NULL, &hammy_cmd_ping, true },
|
||||
|
||||
// Tier 0 commands go here as they land. All pure computation, so instant:
|
||||
// { "grid", "Maidenhead locator conversions", &grid_opts, &hammy_cmd_grid, true },
|
||||
// { "morse", "Text to and from Morse code", &morse_opts, &hammy_cmd_morse, true },
|
||||
// { "band", "What band is a frequency in", &band_opts, &hammy_cmd_band, true },
|
||||
//
|
||||
// Anything touching the backend API, a database or the network is NOT
|
||||
// instant:
|
||||
// { "call", "Look up a callsign", &call_opts, &hammy_cmd_call, false },
|
||||
};
|
||||
|
||||
const hammy_command_t* hammy_command_builtins(size_t* outCount) {
|
||||
if (outCount)
|
||||
*outCount = sizeof(hammy_builtin_commands) / sizeof(hammy_builtin_commands[0]);
|
||||
|
||||
return hammy_builtin_commands;
|
||||
}
|
||||
|
||||
void hammy_command_destroy(void* cmd) {
|
||||
if (!cmd) {
|
||||
return;
|
||||
const hammy_command_t* hammy_command_find(const hammy_command_t* commands, size_t count,
|
||||
const char* name) {
|
||||
if (!commands || !name) return NULL;
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (commands[i].name && strcmp(commands[i].name, name) == 0)
|
||||
return &commands[i];
|
||||
}
|
||||
|
||||
cmd = (hammy_command_t*)cmd;
|
||||
|
||||
// TODO: Other things here eventually
|
||||
|
||||
free(cmd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool hammy_command_is_instant(const hammy_command_t* command) {
|
||||
return command && command->instant;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user