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

Demonstrates banning and unbanning members

#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 ban/unban members\n"
"1. Type '!ban <user_id>' to ban user\n"
"2. Type '!unban <user_id>' to unban user\n"
"3. Type '!prune_count <days>' to check how many members would be "
"removed by a prune operation (1-30 days)\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Ban-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
log_on_guild_ban_add(struct discord *client,
const struct discord_guild_ban_add *event)
{
log_info("User `%s#%s` has been banned.", event->user->username,
event->user->discriminator);
}
void
log_on_guild_ban_remove(struct discord *client,
const struct discord_guild_ban_remove *event)
{
log_info("User `%s#%s` has been unbanned.", event->user->username,
event->user->discriminator);
}
void
on_ban(struct discord *client, const struct discord_message *event)
{
u64snowflake target_id = 0ULL;
sscanf(event->content, "%" SCNu64, &target_id);
struct discord_create_guild_ban params = {
.reason = "Someone really dislikes you!",
};
discord_create_guild_ban(client, event->guild_id, target_id, &params,
NULL);
}
void
on_unban(struct discord *client, const struct discord_message *event)
{
u64snowflake target_id = 0ULL;
sscanf(event->content, "%" SCNu64, &target_id);
struct discord_remove_guild_ban params = {
.reason = "Someone really likes you!"
};
discord_remove_guild_ban(client, event->guild_id, target_id, &params,
NULL);
}
void
done_get_prune_count(struct discord *client,
struct discord_response *resp,
const struct discord_prune_count *ret)
{
const struct discord_message *event = resp->keep;
const long days = (long)resp->data;
char text[128];
snprintf(text, sizeof(text),
"%d members will be removed by that prune operation! (%ld days)",
ret->pruned, days);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_prune_count(struct discord *client, const struct discord_message *event)
{
char *endp = NULL;
const long days = strtol(event->content, &endp, 10);
if (endp != event->content) {
struct discord_get_guild_prune_count params = { .days = days };
discord_get_guild_prune_count(client, event->guild_id, &params,
.done = done_get_prune_count,
.keep = event,
.data = (void *)days,
});
}
else {
struct discord_create_message params = {
.content = "Bad command formatting, expect: `!prune_count <days>`"
};
discord_create_message(client, event->channel_id, &params, 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_on_guild_ban_add(client, &log_on_guild_ban_add);
discord_set_on_guild_ban_remove(client, &log_on_guild_ban_remove);
discord_set_on_command(client, "!ban", &on_ban);
discord_set_on_command(client, "!unban", &on_unban);
discord_set_on_command(client, "!prune_count", &on_prune_count);
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.
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_remove_guild_ban(struct discord *client, u64snowflake guild_id, u64snowflake user_id, struct discord_remove_guild_ban *params, struct discord_ret *ret)
Remove the ban for a user.
CCORDcode discord_get_guild_prune_count(struct discord *client, u64snowflake guild_id, struct discord_get_guild_prune_count *params, struct discord_ret_prune_count *ret)
Returns the number of members that would be removed in a prune operation.
CCORDcode discord_create_guild_ban(struct discord *client, u64snowflake guild_id, u64snowflake user_id, struct discord_create_guild_ban *params, struct discord_ret *ret)
Bans user from a given 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.
void discord_set_on_guild_ban_add(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_ban_add *event))
Triggers when a user is banned from a guild.
void discord_set_on_guild_ban_remove(struct discord *client, void(*callback)(struct discord *client, const struct discord_guild_ban_remove *event))
Triggers when a user is unbanned from a guild.
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: guild.h:685
int delete_message_days
Definition: guild.h:690
Definition: channel.h:683
char * content
Definition: channel.h:685
Definition: guild.h:754
int days
Definition: guild.h:757
Definition: gateway.h:416
struct discord_user * user
Definition: gateway.h:420
Definition: gateway.h:423
struct discord_user * user
Definition: gateway.h:427
Definition: channel.h:195
u64snowflake guild_id
Definition: channel.h:201
char * content
Definition: channel.h:207
u64snowflake channel_id
Definition: channel.h:199
Definition: guild.h:431
int pruned
Definition: guild.h:432
Definition: gateway.h:332
struct discord_user * user
Definition: gateway.h:336
Definition: guild.h:693
char * reason
Definition: guild.h:695
The response for the completed request.
Definition: discord-response.h:12
const void * keep
Definition: discord-response.h:16
void * data
Definition: discord-response.h:14
Request's return context.
Definition: discord-response.h:114
char * username
Definition: user.h:73
char * discriminator
Definition: user.h:75
The Discord client handler.
Definition: discord-internal.h:1182