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

Demonstrates cache usage

#include "discord.h"
static void
on_message(struct discord *client, const struct discord_message *message)
{
if (message->author->bot) return;
const struct discord_guild *guild =
discord_cache_get_guild(client, message->guild_id);
if (guild) {
printf("message received in guild '%s'\n", guild->name);
discord_unclaim(client, guild);
}
// check if this message is in the cache, NULL if it isn't
const struct discord_message *cached_message =
message->id);
if (cached_message) {
// discord_claim(cached_message); is implicit
discord_create_message(client, cached_message->channel_id,
.content = cached_message->content,
},
NULL);
// don't forget to clean up cached message when done
discord_unclaim(client, cached_message);
}
}
int
main(int argc, char *argv[])
{
const char *const config_file = argc > 1 ? argv[1] : "../config.json";
struct discord *client = discord_config_init(config_file);
// enable message caching
discord_set_on_message_create(client, on_message);
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.
const struct discord_message * discord_cache_get_channel_message(struct discord *client, u64snowflake channel_id, u64snowflake message_id)
Get a message from cache, only if locally available in RAM.
void discord_cache_enable(struct discord *client, enum discord_cache_options options)
const struct discord_guild * discord_cache_get_guild(struct discord *client, u64snowflake guild_id)
Get a guild from cache, only if locally available in RAM.
@ DISCORD_CACHE_GUILDS
Definition: discord-cache.h:17
@ DISCORD_CACHE_MESSAGES
Definition: discord-cache.h:16
void discord_cleanup(struct discord *client)
Free a Discord Client handle.
CCORDcode discord_run(struct discord *client)
Start a connection to the Discord Gateway.
void discord_unclaim(struct discord *client, const void *data)
Unclaim ownership of a resource provided by Concord.
struct discord * discord_config_init(const char config_file[])
Create a Discord Client handle by a config.json file.
void discord_add_intents(struct discord *client, uint64_t code)
Subscribe to Discord Events.
void discord_set_on_message_create(struct discord *client, void(*callback)(struct discord *client, const struct discord_message *event))
Triggers when a message is created.
#define DISCORD_GATEWAY_MESSAGE_CONTENT
Definition: gateway.h:41
Definition: channel.h:683
Definition: guild.h:88
char * name
Definition: guild.h:93
Definition: channel.h:195
u64snowflake guild_id
Definition: channel.h:201
u64snowflake id
Definition: channel.h:197
struct discord_user * author
Definition: channel.h:203
u64snowflake channel_id
Definition: channel.h:199
bool bot
Definition: user.h:79
The Discord client handler.
Definition: discord-internal.h:1182