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

Demonstrates a couple use cases of the Emoji API

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h> /* PRIu64, SCNu64 */
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf(
"\n\n This bot demonstrates how easy it is to create/delete emojis\n"
"1. Type 'emoji.list' to get a list of server emojis (in no "
"particular order)\n"
"2. Type 'emoji.get <emoji_id>' to get the selected emoji\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Emoji-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
done_list_guild_emojis(struct discord *client,
struct discord_response *resp,
const struct discord_emojis *emojis)
{
const struct discord_message *event = resp->keep;
char text[2000] = "";
if (!emojis->size) {
log_info("No emojis in guild");
return;
}
char *cur = text;
char *end = &text[sizeof(text) - 1];
char *prev;
for (int i = 0; i < emojis->size; ++i) {
prev = cur;
cur += snprintf(cur, end - cur, "<%s:%s:%" PRIu64 ">(%" PRIu64 ")\n",
emojis->array[i].animated ? "a" : "",
emojis->array[i].name, emojis->array[i].id,
emojis->array[i].id);
if (cur >= end) { // to make sure no emoji is skipped
*prev = '\0'; // end string before truncation
// reset for retry
cur = text;
--i;
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
continue;
}
}
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail_list_guild_emojis(struct discord *client, struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
char text[256];
snprintf(text, sizeof(text), "Couldn't fetch guild emojis: %s",
discord_strerror(resp->code, client));
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_list_guild_emojis(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_emojis ret = {
.done = &done_list_guild_emojis,
.fail = &fail_list_guild_emojis,
.keep = event,
};
discord_list_guild_emojis(client, event->guild_id, &ret);
}
void
done_get_guild_emoji(struct discord *client,
struct discord_response *resp,
const struct discord_emoji *emoji)
{
const struct discord_message *event = resp->keep;
snprintf(text, sizeof(text), "Here you go: <%s:%s:%" PRIu64 ">",
emoji->animated ? "a" : "", emoji->name, emoji->id);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail_get_guild_emoji(struct discord *client, struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
char text[256];
snprintf(text, sizeof(text), "Unknown emoji: %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_emoji(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
u64snowflake emoji_id = 0ULL;
sscanf(event->content, "%" SCNu64, &emoji_id);
if (!emoji_id) return;
struct discord_ret_emoji ret = {
.done = &done_get_guild_emoji,
.fail = &fail_get_guild_emoji,
.keep = event,
};
discord_get_guild_emoji(client, event->guild_id, emoji_id, &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, "emoji.");
discord_set_on_command(client, "list", &on_list_guild_emojis);
discord_set_on_command(client, "get", &on_get_guild_emoji);
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.
uint64_t u64snowflake
Snowflake datatype.
Definition: types.h:28
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_list_guild_emojis(struct discord *client, u64snowflake guild_id, struct discord_ret_emojis *ret)
Get emojis of a given guild.
CCORDcode discord_get_guild_emoji(struct discord *client, u64snowflake guild_id, u64snowflake emoji_id, struct discord_ret_emoji *ret)
Get a specific emoji from a 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: channel.h:683
char * content
Definition: channel.h:685
Definition: emoji.h:23
u64snowflake id
Definition: emoji.h:25
char * name
Definition: emoji.h:27
bool animated
Definition: emoji.h:41
Definition: emoji.h:47
struct discord_emoji * array
Definition: emoji.h:48
int size
Definition: emoji.h:47
Definition: channel.h:195
u64snowflake guild_id
Definition: channel.h:201
char * content
Definition: channel.h:207
struct discord_user * author
Definition: channel.h:203
u64snowflake channel_id
Definition: channel.h:199
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:95
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_emoji *ret)
Definition: discord-response.h:95
Request's return context.
Definition: discord-response.h:96
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_emojis *ret)
Definition: discord-response.h:96
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