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

Demonstrates a couple use cases of the Guild Template API

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h> /* PRIu64, SCNu64 */
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf("\n\nThis bot demonstrates how easy it is to manipulate guild"
" template endpoints.\n"
"1. Type 'guild-template.get <code>' to get a guild template's "
"information\n"
"2. Type 'guild-template.create' to create a new guild template\n"
"3. Type 'guild-template.sync' to sync the guild template\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Guild-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_guild_template *template)
{
const struct discord_message *event = resp->keep;
snprintf(text, sizeof(text),
"Here is some information about your guild template:\nName: "
"'%s'\nDescription: '%s'\nCreator Id: %" PRIu64 "\n",
template->name, template->description, template->creator_id);
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;
snprintf(text, sizeof(text), "Couldn't perform operation: %s",
discord_strerror(resp->code, client));
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_get_guild_template(struct discord *client,
const struct discord_message *event)
{
.done = &done,
.fail = &fail,
.keep = event,
};
discord_get_guild_template(client, event->content, &ret);
}
void
on_create_guild_template(struct discord *client,
const struct discord_message *event)
{
.done = &done,
.fail = &fail,
.keep = event,
};
.name = "New server template!",
.description = "This is a new server template created with Concord!"
};
discord_create_guild_template(client, event->guild_id, &params, &ret);
}
void
on_sync_guild_template(struct discord *client,
const struct discord_message *event)
{
.done = &done,
.fail = &fail,
.keep = event,
};
discord_sync_guild_template(client, event->guild_id, event->content, &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 && "Couldn't initialize client");
discord_set_on_ready(client, &on_ready);
discord_set_prefix(client, "guild-template.");
discord_set_on_command(client, "get", on_get_guild_template);
discord_set_on_command(client, "create", on_create_guild_template);
discord_set_on_command(client, "sync", on_sync_guild_template);
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.
const char * discord_strerror(CCORDcode code, struct discord *client)
Return the meaning of CCORDcode.
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_sync_guild_template(struct discord *client, u64snowflake guild_id, const char template_code[], struct discord_ret_guild_template *ret)
Syncs the template to the guild's current state.
CCORDcode discord_get_guild_template(struct discord *client, const char template_code[], struct discord_ret_guild_template *ret)
Get a guild template for the given code.
CCORDcode discord_create_guild_template(struct discord *client, u64snowflake guild_id, struct discord_create_guild_template *params, struct discord_ret_guild_template *ret)
Creates a template for the guild.
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.
#define DISCORD_MAX_MESSAGE_LEN
Definition: discord.h:64
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: guild_template.h:64
char * name
Definition: guild_template.h:66
Definition: channel.h:683
char * content
Definition: channel.h:685
Definition: guild_template.h:23
Definition: channel.h:195
u64snowflake guild_id
Definition: channel.h:201
char * content
Definition: channel.h:207
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
CCORDcode code
Definition: discord-response.h:18
Request's return context.
Definition: discord-response.h:126
void(* fail)(struct discord *client, struct discord_response *resp)
Definition: discord-response.h:126
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_guild_template *ret)
Definition: discord-response.h:126
char * username
Definition: user.h:73
char * discriminator
Definition: user.h:75
The Discord client handler.
Definition: discord-internal.h:1182