#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
void
print_usage(void)
{
printf(
"\n\nThis bot demonstrates how easy it is to manipulate guild"
" endpoints.\n"
"1. Type 'guild.role_create <name>' to create a new role\n"
"2. Type 'guild.role_delete <role_id>' to delete\n"
"3. Type 'guild.role_member_add <user_id> <role_id>' to assign role "
"to user\n"
"4. Type 'guild.role_member_remove <user_id> <role_id>' to remove "
"role from user\n"
"5. Type 'guild.role_list' to get a list of this guild roles\n"
"6. Type 'guild.member_get <id>' to fetch a member by their ID\n"
"7. Type 'guild.channels_get' to list channels in a guild\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
{
log_info(
"Guild-Bot succesfully connected to Discord as %s#%s!",
}
void
log_on_role_create(
struct discord *client,
{
}
void
log_on_role_update(
struct discord *client,
{
}
void
log_on_role_delete(
struct discord *client,
{
}
void
{
char name[128] = "";
sscanf(event->
content,
"%s", name);
if (!*name) {
log_error(
"Couldn't create role `%s`", name);
return;
}
}
void
{
sscanf(event->
content,
"%" SCNu64, &role_id);
if (!role_id) {
log_error(
"Invalid format for `guild.role_delete <role_id>`");
return;
}
}
void
{
sscanf(event->
content,
"%" SCNu64
" %" SCNu64, &user_id, &role_id);
if (!user_id || !role_id) {
"Invalid format for `guild.role_member_add <user_id> <role_id>`");
return;
}
.
reason =
"Special role for a special member",
};
¶ms, NULL);
}
void
on_role_member_remove(
struct discord *client,
{
sscanf(event->
content,
"%" SCNu64
" %" SCNu64, &user_id, &role_id);
if (!user_id || !role_id) {
log_error(
"Invalid format for `guild.role_member_remove <user_id> "
"<role_id>`");
return;
}
.
reason =
"Didn't deserve it",
};
¶ms, NULL);
}
void
done_get_guild_roles(
struct discord *client,
{
char *cur = text;
char *end = &text[sizeof(text) - 1];
char *prev;
for (
int i = 0; i < roles->
size; ++i) {
prev = cur;
cur += snprintf(cur, end - cur, "<@&%" PRIu64 ">(%" PRIu64 ")\n",
if (cur >= end) {
*prev = '\0';
cur = text;
--i;
continue;
}
}
}
void
{
}
void
{
.
done = &done_get_guild_roles,
.fail = &fail_get_guild_roles,
};
}
void
done_get_guild_member(
struct discord *client,
{
(void)resp;
}
void
{
}
void
{
sscanf(event->
content,
"%" SCNu64, &user_id);
if (!user_id) {
log_error(
"Invalid format for `guild.member_get <user_id>`");
return;
}
.
done = &done_get_guild_member,
.fail = &fail_get_guild_member,
};
}
void
done_get_guild_channels(
struct discord *client,
{
char *cur = text;
char *end = &text[sizeof(text) - 1];
char *prev;
for (
int i = 0; i < channels->
size; ++i) {
prev = cur;
cur += snprintf(cur, end - cur, "<#%" PRIu64 ">\n",
if (cur >= end) {
*prev = '\0';
cur = text;
--i;
continue;
}
}
}
void
{
char text[256];
snprintf(text, sizeof(text), "Couldn't fetch guild channels: %s",
}
void
{
.
done = &done_get_guild_channels,
.fail = &fail_get_guild_channels,
.keep = event,
};
}
int
main(int argc, char *argv[])
{
const char *config_file;
if (argc > 1)
config_file = argv[1];
else
config_file = "../config.json";
assert(NULL != client && "Couldn't initialize client");
&on_role_member_remove);
print_usage();
fgetc(stdin);
}
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_guild_role(struct discord *client, u64snowflake guild_id, u64snowflake role_id, struct discord_delete_guild_role *params, struct discord_ret *ret)
Delete a guild role.
CCORDcode discord_get_guild_member(struct discord *client, u64snowflake guild_id, u64snowflake user_id, struct discord_ret_guild_member *ret)
Get guild member of a guild from given user id.
CCORDcode discord_create_guild_role(struct discord *client, u64snowflake guild_id, struct discord_create_guild_role *params, struct discord_ret_role *ret)
Create a new guild role.
CCORDcode discord_get_guild_channels(struct discord *client, u64snowflake guild_id, struct discord_ret_channels *ret)
Fetch channels from given guild. Does not include threads.
CCORDcode discord_get_guild_roles(struct discord *client, u64snowflake guild_id, struct discord_ret_roles *ret)
Get guild roles.
CCORDcode discord_add_guild_member_role(struct discord *client, u64snowflake guild_id, u64snowflake user_id, u64snowflake role_id, struct discord_add_guild_member_role *params, struct discord_ret *ret)
Adds a role to a guild member.
CCORDcode discord_remove_guild_member_role(struct discord *client, u64snowflake guild_id, u64snowflake user_id, u64snowflake role_id, struct discord_remove_guild_member_role *params, struct discord_ret *ret)
Removes a role from a guild member.
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_guild_role_update(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_role_update *event))
Triggers when a guild role is updated.
void discord_set_on_guild_role_delete(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_role_delete *event))
Triggers when a guild role is deleted.
void discord_set_on_guild_role_create(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_role_create *event))
Triggers when a guild role is created.
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
char * reason
Definition: guild.h:674
u64snowflake id
Definition: channel.h:124
Definition: channel.h:191
struct discord_channel * array
Definition: channel.h:192
int size
Definition: channel.h:191
char * name
Definition: guild.h:702
Definition: channel.h:683
char * content
Definition: channel.h:685
char * reason
Definition: guild.h:752
struct discord_user * user
Definition: guild.h:293
Definition: gateway.h:505
struct discord_role * role
Definition: gateway.h:509
Definition: gateway.h:519
u64snowflake role_id
Definition: gateway.h:523
Definition: gateway.h:512
struct discord_role * role
Definition: gateway.h:516
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
char * reason
Definition: guild.h:678
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:85
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_channels *ret)
Definition: discord-response.h:85
Request's return context.
Definition: discord-response.h:104
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_guild_member *ret)
Definition: discord-response.h:104
Request's return context.
Definition: discord-response.h:111
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_roles *ret)
Definition: discord-response.h:111
u64snowflake id
Definition: permissions.h:120
Definition: permissions.h:149
struct discord_role * array
Definition: permissions.h:150
int size
Definition: permissions.h:149
bool bot
Definition: user.h:79
u64snowflake id
Definition: user.h:71
char * username
Definition: user.h:73
char * discriminator
Definition: user.h:75
The Discord client handler.
Definition: discord-internal.h:1190