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

Demonstrates pinning messages

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h> /* PRIu64, SCNu64 */
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf(
"\n\nThis bot demonstrates how easy it is to have a"
" message be pinned.\n"
"1. Reply to a message with '!pin' or type '!pin <message_id> to pin "
"it\n"
"2. Reply to a message with '!unpin' or type '!unpin <message_id> to "
"unpin it\n"
"3. Type '!get_pins' to get a id list of pinned messages\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("Pin-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
on_pin(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
u64snowflake msg_id = 0;
sscanf(event->content, "%" SCNu64, &msg_id);
if (!msg_id) {
if (!event->referenced_message) return;
msg_id = event->referenced_message->id;
}
struct discord_pin_message params = { .reason = "Important message" };
discord_pin_message(client, event->channel_id, msg_id, &params, NULL);
}
void
on_unpin(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
u64snowflake msg_id = 0;
sscanf(event->content, "%" SCNu64, &msg_id);
if (!msg_id) {
if (!event->referenced_message) return;
msg_id = event->referenced_message->id;
}
struct discord_unpin_message params = { .reason = "No longer relevant" };
discord_unpin_message(client, event->channel_id, msg_id, &params, NULL);
}
void
done_get_pins(struct discord *client,
struct discord_response *resp,
const struct discord_messages *msgs)
{
const struct discord_message *event = resp->keep;
char text[2000] = "No pins on channel";
char *cur = text;
char *end = &text[sizeof(text) - 1];
for (int i = 0; i < msgs->size; ++i) {
cur += snprintf(cur, end - cur,
"https://discord.com/channels/%" PRIu64 "/%" PRIu64
"/%" PRIu64 "\n",
event->guild_id, event->channel_id, msgs->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_pins(struct discord *client, struct discord_response *resp)
{
const struct discord_message *event = resp->keep;
char text[2000] = "";
snprintf(text, sizeof(text),
"Failed fetching pinned messages at <#%" PRIu64 ">",
event->channel_id);
struct discord_create_message params = { .content = text };
discord_create_message(client, event->channel_id, &params, NULL);
}
void
on_get_pins(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
struct discord_ret_messages ret = {
.done = &done_get_pins,
.fail = &fail_get_pins,
.keep = event,
};
discord_get_pinned_messages(client, event->channel_id, &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_set_on_ready(client, &on_ready);
discord_set_prefix(client, "!");
discord_set_on_command(client, "pin", &on_pin);
discord_set_on_command(client, "unpin", &on_unpin);
discord_set_on_command(client, "get_pins", &on_get_pins);
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_pin_message(struct discord *client, u64snowflake channel_id, u64snowflake message_id, struct discord_pin_message *params, struct discord_ret *ret)
Pin a message to a channel.
CCORDcode discord_get_pinned_messages(struct discord *client, u64snowflake channel_id, struct discord_ret_messages *ret)
Get all pinned messages in the channel.
CCORDcode discord_unpin_message(struct discord *client, u64snowflake channel_id, u64snowflake message_id, struct discord_unpin_message *params, struct discord_ret *ret)
Unpin a message from a 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_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:195
u64snowflake guild_id
Definition: channel.h:201
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: channel.h:265
int size
Definition: channel.h:265
struct discord_message * array
Definition: channel.h:266
Definition: channel.h:826
char * reason
Definition: channel.h:828
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
Request's return context.
Definition: discord-response.h:87
void(* done)(struct discord *client, struct discord_response *resp, const struct discord_messages *ret)
Definition: discord-response.h:87
Definition: channel.h:830
char * reason
Definition: channel.h:832
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