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

Demonstrates a couple use cases of the Channel reactions API

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h> /* SCNu64 */
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf(
"\n\nThis bot demonstrates how easy it is to create/delete"
" reactions from a message.\n"
"1. Reply to a message with 'reaction.get_users <emoji>' to get all "
"the users who reacted with that particular emoji\n"
"2. Reply to a message with 'reaction.create <emoji>' and the bot "
"will react with that emoji\n"
"3. Reply to a message with 'reaction.delete <emoji>' to delete all "
"reactions with a particular emoji\n"
"4. Reply to a message with 'reaction.delete_all' to delete all "
"reactions\n"
"5. Reply to a message with 'reaction.delete_self <emoji>' to delete "
"your reaction with a particular emoji\n"
"6. Reply to a message with 'reaction.delete_user <user_id> <emoji>' "
"to delete the user reaction with a particular emoji\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Reaction-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
done_get_users(struct discord *client,
struct discord_response *resp,
const struct discord_users *users)
{
const struct discord_message *event = resp->keep;
char text[2000];
if (!users->size) {
snprintf(text, sizeof(text), "Nobody reacted with that emoji!");
}
else {
char *cur = text;
char *end = &text[sizeof(text) - 1];
for (int i = 0; i < users->size; ++i) {
cur += snprintf(cur, end - cur, "%s (%" PRIu64 ")\n",
users->array[i].username, users->array[i].id);
if (cur >= end) break;
}
}
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail_get_users(struct discord *client, struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
char text[256];
snprintf(text, sizeof(text), "Couldn't fetch reactions: %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_users(struct discord *client, const struct discord_message *event)
{
if (event->author->bot || !event->referenced_message) return;
struct discord_ret_users ret = {
.done = &done_get_users,
.fail = &fail_get_users,
.keep = event,
};
struct discord_get_reactions params = { .limit = 25 };
event->referenced_message->id, 0, event->content,
&params, &ret);
}
void
on_create(struct discord *client, const struct discord_message *event)
{
if (event->author->bot || !event->referenced_message) return;
event->referenced_message->id, 0, event->content,
NULL);
}
void
on_delete(struct discord *client, const struct discord_message *event)
{
if (event->author->bot || !event->referenced_message) return;
client, event->referenced_message->channel_id,
event->referenced_message->id, 0, event->content, NULL);
}
void
on_delete_all(struct discord *client, const struct discord_message *event)
{
if (event->author->bot || !event->referenced_message) return;
event->referenced_message->id, NULL);
}
void
on_delete_self(struct discord *client, const struct discord_message *event)
{
if (event->author->bot || !event->referenced_message) return;
event->referenced_message->id, 0,
event->content, NULL);
}
void
on_delete_user(struct discord *client, const struct discord_message *event)
{
if (event->author->bot || !event->referenced_message) return;
u64snowflake user_id = 0;
char emoji_name[256] = "";
sscanf(event->content, "%" SCNu64 " %s", &user_id, emoji_name);
event->referenced_message->id, user_id, 0,
emoji_name, NULL);
}
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, "reaction.");
discord_set_on_command(client, "get_users", &on_get_users);
discord_set_on_command(client, "create", &on_create);
discord_set_on_command(client, "delete", &on_delete);
discord_set_on_command(client, "delete_all", &on_delete_all);
discord_set_on_command(client, "delete_self", &on_delete_self);
discord_set_on_command(client, "delete_user", &on_delete_user);
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_delete_own_reaction(struct discord *client, u64snowflake channel_id, u64snowflake message_id, u64snowflake emoji_id, const char emoji_name[], struct discord_ret *ret)
Delete a reaction the current user has made for the message.
CCORDcode discord_delete_user_reaction(struct discord *client, u64snowflake channel_id, u64snowflake message_id, u64snowflake user_id, u64snowflake emoji_id, const char emoji_name[], struct discord_ret *ret)
Deletes another user's reaction.
CCORDcode discord_get_reactions(struct discord *client, u64snowflake channel_id, u64snowflake message_id, u64snowflake emoji_id, const char emoji_name[], struct discord_get_reactions *params, struct discord_ret_users *ret)
Get a list of users that reacted with given emoji.
CCORDcode discord_delete_all_reactions(struct discord *client, u64snowflake channel_id, u64snowflake message_id, struct discord_ret *ret)
Deletes all reactions from message.
CCORDcode discord_create_reaction(struct discord *client, u64snowflake channel_id, u64snowflake message_id, u64snowflake emoji_id, const char emoji_name[], struct discord_ret *ret)
Create a reaction for the message.
CCORDcode discord_delete_all_reactions_for_emoji(struct discord *client, u64snowflake channel_id, u64snowflake message_id, u64snowflake emoji_id, const char emoji_name[], struct discord_ret *ret)
Deletes all the reactions for a given emoji on message.
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: channel.h:716
int limit
Definition: channel.h:723
Definition: channel.h:195
u64snowflake id
Definition: channel.h:197
struct discord_message * referenced_message
Definition: channel.h:252
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:151
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_users *ret)
Definition: discord-response.h:151
bool bot
Definition: user.h:79
u64snowflake id
Definition: user.h:71
char * username
Definition: user.h:73
char * discriminator
Definition: user.h:75
Definition: user.h:104
int size
Definition: user.h:104
struct discord_user * array
Definition: user.h:105
The Discord client handler.
Definition: discord-internal.h:1182