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

Demonstrates listening to audit-log events and fetching a specific audit-log

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf("\n\nThis bot demonstrates how easy it is to log"
" for certain events.\n"
"1. Type '!last_channel' to check the most recent channel created "
"by you\n"
"\tsee: "
"https://discord.com/developers/docs/resources/"
"audit-log#audit-log-entry-object-audit-log-events\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Audit-Log-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
log_on_guild_member_add(struct discord *client,
const struct discord_guild_member *event)
{
log_info("%s#%s joined guild %" PRIu64, event->user->username,
event->user->discriminator, event->guild_id);
}
void
log_on_guild_member_update(struct discord *client,
const struct discord_guild_member_update *event)
{
char nick[128] = "";
if (event->nick && *event->nick)
snprintf(nick, sizeof(nick), " (%s)", event->nick);
log_info("%s#%s%s updated (guild %" PRIu64 ")", event->user->username,
event->user->discriminator, nick, event->guild_id);
}
void
log_on_guild_member_remove(struct discord *client,
const struct discord_guild_member_remove *event)
{
log_info("%s#%s left guild %" PRIu64, event->user->username,
event->user->discriminator, event->guild_id);
}
void
done(struct discord *client,
struct discord_response *resp,
const struct discord_audit_log *audit_log)
{
const struct discord_message *event = resp->keep;
if (!audit_log->audit_log_entries || !audit_log->audit_log_entries->size) {
log_warn("No audit log entries found!");
return;
}
struct discord_audit_log_entry *entry =
&audit_log->audit_log_entries->array[0];
char text[1028];
snprintf(text, sizeof(text), "<@!%" PRIu64 "> has created <#%" PRIu64 ">!",
entry->user_id, entry->target_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)
{
(void)resp;
log_error("Couldn't retrieve audit log: %s",
discord_strerror(resp->code, client));
}
void
on_audit_channel_create(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_audit_log ret = {
.done = &done,
.fail = &fail,
.keep = event,
};
struct discord_get_guild_audit_log params = {
.user_id = event->author->id,
};
discord_get_guild_audit_log(client, event->guild_id, &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 && "Couldn't initialize client");
discord_add_intents(client, 32767); // subscribe to all events
discord_set_on_ready(client, &on_ready);
discord_set_on_guild_member_add(client, &log_on_guild_member_add);
discord_set_on_guild_member_update(client, &log_on_guild_member_update);
discord_set_on_guild_member_remove(client, &log_on_guild_member_remove);
discord_set_on_command(client, "!last_channel", &on_audit_channel_create);
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.
@ DISCORD_AUDIT_LOG_CHANNEL_CREATE
Definition: audit_log.h:24
const char * discord_strerror(CCORDcode code, struct discord *client)
Return the meaning of CCORDcode.
CCORDcode discord_get_guild_audit_log(struct discord *client, u64snowflake guild_id, struct discord_get_guild_audit_log *params, struct discord_ret_audit_log *ret)
Get audit log for a given guild.
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.
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_add_intents(struct discord *client, uint64_t code)
Subscribe to Discord Events.
void discord_set_on_guild_member_update(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_member_update *event))
Triggers when a guild member is updated.
void discord_set_on_guild_member_remove(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_member_remove *event))
Triggers when a guild member is removed.
void discord_set_on_guild_member_add(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_member *event))
Triggers when a guild member is added.
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_warn(...)
Definition: log.h:53
#define log_error(...)
Definition: log.h:54
#define log_info(...)
Definition: log.h:52
struct discord_audit_log_entry * array
Definition: audit_log.h:126
int size
Definition: audit_log.h:125
Definition: audit_log.h:103
u64snowflake target_id
Definition: audit_log.h:105
u64snowflake user_id
Definition: audit_log.h:111
Definition: audit_log.h:77
struct discord_audit_log_entries * audit_log_entries
Definition: audit_log.h:80
Definition: channel.h:683
char * content
Definition: channel.h:685
Definition: audit_log.h:163
u64snowflake user_id
Definition: audit_log.h:165
Definition: gateway.h:449
u64snowflake guild_id
Definition: gateway.h:451
struct discord_user * user
Definition: gateway.h:453
Definition: gateway.h:456
u64snowflake guild_id
Definition: gateway.h:458
struct discord_user * user
Definition: gateway.h:462
char * nick
Definition: gateway.h:464
Definition: guild.h:290
struct discord_user * user
Definition: guild.h:293
u64snowflake guild_id
Definition: guild.h:328
Definition: channel.h:195
u64snowflake guild_id
Definition: channel.h:201
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
CCORDcode code
Definition: discord-response.h:18
Request's return context.
Definition: discord-response.h:73
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_audit_log *ret)
Definition: discord-response.h:73
void(* fail)(struct discord *client, struct discord_response *resp)
Definition: discord-response.h:73
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