discord_async_next - execute immediate request function asynchronously

struct discord_async_ret

Async done callback return context.

Public Members

const void *ret

the request’s response object (NULL if missing)

Note

can be safely cast to the request’s return type

void *data

user arbitrary data (NULL if missing)

typedef void (*discord_on_done)(struct discord *client, struct discord_async_ret *ret)

Triggers on a successful async request.

struct discord_async_err

Async fail callback return context.

Public Members

ORCAcode code

request error code

See

discord_strerror()

void *data

user arbitrary data (NULL if missing)

typedef void (*discord_on_fail)(struct discord *client, struct discord_async_err *err)

Triggers on a failed async request.

struct discord_async_attr

The async attributes for next request.

Public Members

discord_on_done done

optional callback to be executed on a succesful request

discord_on_fail fail

optional callback to be executed on a failed request

bool high_p

whether the next request is high priority (enqueued first)

void *data

optional user data to be sent over

void (*cleanup)(void *data)

optional user data cleanup function

void discord_async_next(struct discord *client, struct discord_async_attr *attr)

Set next request to run asynchronously.

Parameters
  • client – the client created with discord_init()

  • attr – optional async attributes for next request, can be NULL if not needed

Example

void on_msg_done(struct discord *client, struct discord_async_ret *ret)
{
  const struct discord_message *msg = ret->ret;

  log_info("Sent: %s", msg->content);
}

void on_msg_fail(struct discord *client, struct discord_async_err *err)
{
  log_error("%s", discord_strerror(err->code, client));
}

void on_start(struct discord *client, const struct discord_message *msg)
{
  struct discord_async_attr attr = { .done = &on_msg_done,
                                     .fail = &on_msg_fail };
  struct discord_create_message_params params = { 0 };

  // next request is low priority (enqueued last)
  attr.high_p = false;
  discord_async_next(client, &attr);
  params.content = "Request A";
  discord_create_message(client, msg->channel_id, &params, NULL);

  // next request is high priority (enqueued first)
  attr.high_p = true;
  discord_async_next(client, &attr);
  params.content = "Request B";
  discord_create_message(client, msg->channel_id, &params, NULL);

  // next request is low priority (enqueued last)
  attr.high_p = false;
  discord_async_next(client, &attr);
  params.content = "Request C";
  discord_create_message(client, msg->channel_id, &params, NULL);

  // next request defaults to the default blocking behavior,
  // because its missing a discord_async_next() match
  params.content = "Request D";
  discord_create_message(client, msg->channel_id, &params, NULL);
}

int main(void)
{
  struct discord *client = discord_init(BOT_TOKEN);
  discord_set_on_command(client, "!start", &on_start);
  discord_run(client);
  discord_cleanup(client);
}