Concord - C Discord API library
A Discord API wrapper library written in C
invite.c

Demonstrates a couple use cases of the Invite API

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h> /* PRIu64 */
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf(
"\n\nThis bot demonstrates how easy it is to fetch/delete invites\n"
"1. Type 'invite.get <invite_code>' to get a invite object from its "
"particular code\n"
"2. Type 'invite.delete <invite_code>' to delete a invite object by "
"its particular code\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Invite-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
done(struct discord *client,
struct discord_response *resp,
const struct discord_invite *invite)
{
const struct discord_message *event = resp->keep;
char text[256];
snprintf(text, sizeof(text), "Success: https://discord.gg/%s",
invite->code);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail(struct discord *client, struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
struct discord_create_message params = {
.content = "Couldn't perform operation."
};
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_invite_get(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_invite ret = {
.done = &done,
.fail = &fail,
.keep = event,
};
struct discord_get_invite params = {
.with_counts = true,
.with_expiration = true,
};
discord_get_invite(client, event->content, &params, &ret);
}
void
on_invite_delete(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_invite ret = {
.done = &done,
.fail = &fail,
.keep = event,
};
struct discord_delete_invite params = { .reason = "Stale invite" };
discord_delete_invite(client, event->content, &params, &ret);
}
int
main(int argc, char *argv[])
{
const char *config_file;
if (argc > 1)
config_file = argv[1];
else
config_file = "../config.json";
struct discord *client = discord_config_init(config_file);
assert(NULL != client && "Could not initialize client");
discord_set_on_ready(client, &on_ready);
discord_set_prefix(client, "invite.");
discord_set_on_command(client, "get", &on_invite_get);
discord_set_on_command(client, "delete", &on_invite_delete);
print_usage();
fgetc(stdin); // wait for input
discord_run(client);
discord_cleanup(client);
}
CCORDcode ccord_global_init()
Initialize global shared-resources not API-specific.
void ccord_global_cleanup()
Cleanup global shared-resources.
Public functions and datatypes.
CCORDcode discord_create_message(struct discord *client, u64snowflake channel_id, struct discord_create_message *params, struct discord_ret_message *ret)
Post a message to a guild text or DM channel.
CCORDcode discord_delete_invite(struct discord *client, char *invite_code, struct discord_delete_invite *params, struct discord_ret_invite *ret)
Delete an invite.
CCORDcode discord_get_invite(struct discord *client, char *invite_code, struct discord_get_invite *params, struct discord_ret_invite *ret)
Get an invite for the given code.
void discord_cleanup(struct discord *client)
Free a Discord Client handle.
CCORDcode discord_run(struct discord *client)
Start a connection to the Discord Gateway.
struct discord * discord_config_init(const char config_file[])
Create a Discord Client handle by a config.json file.
void discord_set_prefix(struct discord *client, const char prefix[])
Set a mandatory prefix before commands.
void discord_set_on_command(struct discord *client, char *command, void(*callback)(struct discord *client, const struct discord_message *event))
Set command/callback pair.
void discord_set_on_ready(struct discord *client, void(*callback)(struct discord *client, const struct discord_ready *event))
Triggers when the client session is ready.
#define log_info(...)
Definition: log.h:52
Definition: channel.h:683
char * content
Definition: channel.h:685
Definition: invite.h:117
char * reason
Definition: invite.h:119
Definition: invite.h:107
bool with_counts
Definition: invite.h:109
Definition: invite.h:27
char * code
Definition: invite.h:29
Definition: channel.h:195
char * content
Definition: channel.h:207
struct discord_user * author
Definition: channel.h:203
Definition: gateway.h:332
struct discord_user * user
Definition: gateway.h:336
The response for the completed request.
Definition: discord-response.h:12
const void * keep
Definition: discord-response.h:16
Request's return context.
Definition: discord-response.h:132
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_invite *ret)
Definition: discord-response.h:132
void(* fail)(struct discord *client, struct discord_response *resp)
Definition: discord-response.h:132
bool bot
Definition: user.h:79
char * username
Definition: user.h:73
char * discriminator
Definition: user.h:75
The Discord client handler.
Definition: discord-internal.h:1190