Skip to content

Commit

Permalink
fix(client.c): set_on_command should match exact command and overwrit…
Browse files Browse the repository at this point in the history
…e previous values
  • Loading branch information
Anotra committed Feb 23, 2022
1 parent 577be52 commit 1e7f303
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
15 changes: 11 additions & 4 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,25 @@ discord_set_on_command(struct discord *client,
client->gw.cmds.on_default.cb = callback;
return; /* EARLY RETURN */
}
size_t index = 0;
const size_t command_len = strlen(command);
for (; index < client->gw.cmds.amt; index++)
if (command_len == client->gw.cmds.pool[index].size)
if (strcmp(command, client->gw.cmds.pool[index].start) == 0)
goto modify;

++client->gw.cmds.amt;
client->gw.cmds.pool =
realloc(client->gw.cmds.pool,
client->gw.cmds.amt * sizeof(*client->gw.cmds.pool));

client->gw.cmds.pool[client->gw.cmds.amt - 1].start = command;
client->gw.cmds.pool[client->gw.cmds.amt - 1].size = strlen(command);
client->gw.cmds.pool[client->gw.cmds.amt - 1].cb = callback;
client->gw.cmds.pool[index].start = strdup(command);
client->gw.cmds.pool[index].size = command_len;
modify:
client->gw.cmds.pool[index].cb = callback;

discord_add_intents(client, DISCORD_GATEWAY_GUILD_MESSAGES
| DISCORD_GATEWAY_DIRECT_MESSAGES);
| DISCORD_GATEWAY_DIRECT_MESSAGES);
}

void
Expand Down
30 changes: 20 additions & 10 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,16 +693,27 @@ on_message_create(struct discord_gateway *gw, struct sized_buffer *data)
if (gw->cmds.pool
&& !strncmp(gw->cmds.prefix.start, msg.content, gw->cmds.prefix.size))
{
char *command_start = msg.content + gw->cmds.prefix.size;
char *command_end = command_start;
while (*command_end && !isspace(*command_end))
++command_end;
size_t command_len = command_end - command_start;

struct discord_gateway_cmd_cbs *cmd = NULL;
size_t i;

for (i = 0; i < gw->cmds.amt; ++i) {
/* check if command from channel matches set command */
if (!strncmp(gw->cmds.pool[i].start,
msg.content + gw->cmds.prefix.size,
gw->cmds.pool[i].size))
{
cmd = &gw->cmds.pool[i];
if (command_len == gw->cmds.pool[i].size) {
/* check if command from channel matches set command */
if (!strncmp(gw->cmds.pool[i].start,
command_start,
command_len))
{
cmd = &gw->cmds.pool[i];
if (!cmd->cb)
cmd = NULL;
break;
}
}
}
if (!cmd && gw->cmds.prefix.size) {
Expand All @@ -714,11 +725,10 @@ on_message_create(struct discord_gateway *gw, struct sized_buffer *data)
char *tmp = msg.content; /* hold original ptr */

/* skip blank characters */
msg.content += (ptrdiff_t)(gw->cmds.prefix.size + cmd->size);
while (isspace((int)msg.content[0])) {
msg.content = command_end;
while (*msg.content && isspace((int)msg.content[0]))
++msg.content;
}


cmd->cb(client, &msg);

msg.content = tmp; /* retrieve original ptr */
Expand Down

0 comments on commit 1e7f303

Please sign in to comment.