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

Demonstrates a couple use cases of the Channel API

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h> /* PRIu64 */
#include <assert.h>
#include "discord.h"
#include "logmod.h"
void
print_usage(void)
{
printf(
"\n\n(USE WITH CAUTION) This bot demonstrates how easy it is to "
"create/delete channels\n"
"1. Type 'channel.create <channel_name>' anywhere to create a new "
"channel\n"
"2. Type 'channel.rename_this <channel_name>' to rename the current "
"channel\n"
"3. Type 'channel.delete_this' to delete the current channel\n"
"4. Type 'channel.get_invites' to check how many have been created\n"
"5. Type 'channel.create_invite' to create a new invite\n"
"6. Type 'channel.start_thread' to start a new thread (reply to a "
"message if you wish start a thread under it)\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
logmod_log(INFO, NULL,
"Channel-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
log_on_channel_create(struct discord *client,
const struct discord_channel *event)
{
logmod_log(INFO, NULL, "Channel %s (%" PRIu64 ") created", event->name,
event->id);
}
void
log_on_channel_update(struct discord *client,
const struct discord_channel *event)
{
logmod_log(INFO, NULL, "Channel %s (%" PRIu64 ") updated", event->name,
event->id);
}
void
log_on_channel_delete(struct discord *client,
const struct discord_channel *event)
{
logmod_log(INFO, NULL, "Channel %s (%" PRIu64 ") deleted", event->name,
event->id);
}
void
log_on_thread_create(struct discord *client,
const struct discord_channel *event)
{
logmod_log(INFO, NULL, "Thread %s (%" PRIu64 ") created", event->name,
event->id);
}
void
log_on_thread_update(struct discord *client,
const struct discord_channel *event)
{
logmod_log(INFO, NULL, "Thread %s (%" PRIu64 ") updated", event->name,
event->id);
}
void
log_on_thread_delete(struct discord *client,
const struct discord_channel *event)
{
logmod_log(INFO, NULL, "Thread %s (%" PRIu64 ") deleted", event->name,
event->id);
}
void
on_channel_create(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_create_guild_channel params = {
.name = event->content,
.reason = "Shiny new channel",
};
discord_create_guild_channel(client, event->guild_id, &params, NULL);
}
void
on_channel_rename_this(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_modify_channel params = {
.name = event->content,
.reason = "Clicks better",
};
discord_modify_channel(client, event->channel_id, &params, NULL);
}
void
on_channel_delete_this(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_delete_channel params = { .reason = "Stinky channel" };
discord_delete_channel(client, event->channel_id, &params, NULL);
}
void
done_get_channel_invites(struct discord *client,
struct discord_response *resp,
const struct discord_invites *invites)
{
if (!invites->size) {
logmod_log(INFO, NULL, "No invites found!");
return;
}
const struct discord_message *event = resp->keep;
snprintf(text, sizeof(text), "%d invite links created.", invites->size);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail_get_channel_invites(struct discord *client, struct discord_response *resp)
{
logmod_log(INFO, NULL, "Couldn't fetch invites: %s",
discord_strerror(resp->code, client));
}
void
on_channel_get_invites(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_invites ret = {
.done = &done_get_channel_invites,
.fail = &fail_get_channel_invites,
.keep = event,
};
discord_get_channel_invites(client, event->channel_id, &ret);
}
void
done_create_channel_invite(struct discord *client,
struct discord_response *resp,
const struct discord_invite *invite)
{
const struct discord_message *event = resp->keep;
char text[256];
snprintf(text, sizeof(text), "https://discord.gg/%s", invite->code);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail_create_channel_invite(struct discord *client,
struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
struct discord_create_message params = {
.content = "Couldn't create invite",
};
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_channel_create_invite(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_invite ret = {
.done = &done_create_channel_invite,
.fail = &fail_create_channel_invite,
.keep = event,
};
discord_create_channel_invite(client, event->channel_id, NULL, &ret);
}
void
done_start_thread(struct discord *client,
struct discord_response *resp,
const struct discord_channel *thread)
{
const struct discord_message *event = resp->keep;
char text[1024];
snprintf(text, sizeof(text), "Created thread <#%" PRIu64 ">", thread->id);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
fail_start_thread(struct discord *client, struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
char text[1024];
snprintf(text, sizeof(text), "Couldn't create thread: %s",
discord_strerror(resp->code, client));
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_channel_start_thread(struct discord *client,
const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_channel ret = {
.done = &done_start_thread,
.fail = &fail_start_thread,
.keep = event,
};
if (event->message_reference) {
.name = "new_thread",
};
discord_start_thread_with_message(client, event->channel_id,
event->message_reference->message_id,
&params, &ret);
}
else {
.name = "new_thread",
.type = DISCORD_CHANNEL_GUILD_PUBLIC_THREAD,
};
discord_start_thread_without_message(client, event->channel_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_from_json(config_file);
assert(NULL != client && "Could not initialize client");
discord_set_on_ready(client, &on_ready);
discord_set_on_channel_create(client, &log_on_channel_create);
discord_set_on_channel_update(client, &log_on_channel_update);
discord_set_on_channel_delete(client, &log_on_channel_delete);
discord_set_on_thread_create(client, &log_on_thread_create);
discord_set_on_thread_update(client, &log_on_thread_update);
discord_set_on_thread_delete(client, &log_on_thread_delete);
discord_set_prefix(client, "channel.");
discord_set_on_command(client, "create", &on_channel_create);
discord_set_on_command(client, "rename_this", &on_channel_rename_this);
discord_set_on_command(client, "delete_this", &on_channel_delete_this);
discord_set_on_command(client, "get_invites", &on_channel_get_invites);
discord_set_on_command(client, "create_invite", &on_channel_create_invite);
discord_set_on_command(client, "start_thread", &on_channel_start_thread);
print_usage();
fgetc(stdin); // wait for input
discord_run(client);
discord_cleanup(client);
}
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.
CCORDcode discord_start_thread_with_message(struct discord *client, u64snowflake channel_id, u64snowflake message_id, struct discord_start_thread_with_message *params, struct discord_ret_channel *ret)
Creates a new thread from an existing message.
CCORDcode discord_start_thread_without_message(struct discord *client, u64snowflake channel_id, struct discord_start_thread_without_message *params, struct discord_ret_channel *ret)
Creates a new thread that is not connected to an existing message.
CCORDcode discord_modify_channel(struct discord *client, u64snowflake channel_id, struct discord_modify_channel *params, struct discord_ret_channel *ret)
Update a channel's settings.
CCORDcode discord_get_channel_invites(struct discord *client, u64snowflake channel_id, struct discord_ret_invites *ret)
Get invites (with invite metadata) for the channel.
CCORDcode discord_delete_channel(struct discord *client, u64snowflake channel_id, struct discord_delete_channel *params, struct discord_ret_channel *ret)
Delete a channel, or close a private message.
CCORDcode discord_create_channel_invite(struct discord *client, u64snowflake channel_id, struct discord_create_channel_invite *params, struct discord_ret_invite *ret)
Create a new invite for the channel.
CCORDcode discord_create_guild_channel(struct discord *client, u64snowflake guild_id, struct discord_create_guild_channel *params, struct discord_ret_channel *ret)
Create a new guild 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_from_json(const char config_file[])
Creates a Discord Client handle from a config.json file.
#define DISCORD_MAX_MESSAGE_LEN
Definition: discord.h:71
const char * discord_strerror(CCORDcode code, struct discord *client)
Return the meaning of CCORDcode.
void discord_set_on_channel_update(struct discord *client, void(*callback)(struct discord *client, const struct discord_channel *event))
Triggers when a channel is updated.
void discord_set_on_channel_create(struct discord *client, void(*callback)(struct discord *client, const struct discord_channel *event))
Triggers when a channel is created.
void discord_set_prefix(struct discord *client, const char prefix[])
Set a mandatory prefix before commands.
void discord_set_on_channel_delete(struct discord *client, void(*callback)(struct discord *client, const struct discord_channel *event))
Triggers when a channel is deleted.
void discord_set_on_thread_create(struct discord *client, void(*callback)(struct discord *client, const struct discord_channel *event))
Triggers when a thread is created.
void discord_set_on_thread_delete(struct discord *client, void(*callback)(struct discord *client, const struct discord_channel *event))
Triggers when a thread is deleted.
void discord_set_on_thread_update(struct discord *client, void(*callback)(struct discord *client, const struct discord_channel *event))
Triggers when a thread is updated.
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.
void discord_set_on_command(struct discord *client, const char *command, void(*callback)(struct discord *client, const struct discord_message *event))
Set command/callback pair.
#define logmod_log
Alias to logmod_nlog for C89 compatibility.
Definition: logmod.h:487
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:89
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_channel *ret)
Definition: discord-response.h:89
Request's return context.
Definition: discord-response.h:137
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_invite *ret)
Definition: discord-response.h:137
Request's return context.
Definition: discord-response.h:138
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_invites *ret)
Definition: discord-response.h:138
The Discord client handler.
Definition: discord-internal.h:1206