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

Demonstrates sending DMs with your client

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <pthread.h>
#include <assert.h>
#include "discord.h"
#include "log.h"
void
print_usage(void)
{
printf("\n\nThis bot demonstrates how easy it is to start a DM"
" with someone and talk without leaving the terminal\n"
"1. Type at the terminal <recipient_id>:<message> to start your "
"conversation\n"
"\tex: 1232232312321232123:Hello there friend!\n"
"2. For successive messages to the same person, you can just type "
"the message"
" without the need of specifying the recipient_id everytime\n"
"3. If you wish to start a new conversation, repeat the #1 format\n"
"\nTYPE ANY KEY TO START BOT\n");
}
void
on_ready(struct discord *client, const struct discord_ready *event)
{
log_info("ManualDM-Bot succesfully connected to Discord as %s#%s!",
event->user->username, event->user->discriminator);
}
void
on_dm_receive(struct discord *client, const struct discord_message *event)
{
if (event->author->bot) return;
printf("%s:%s\n", event->author->username, event->content);
}
void *
read_input(void *p_client)
{
struct discord *client = p_client;
char buf[32 + DISCORD_MAX_MESSAGE_LEN];
u64snowflake recipient_id;
u64snowflake dm_channel_id;
pthread_detach(pthread_self());
while (1) {
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
if (!*buf) continue; // is empty
memset(msg, 0, sizeof(msg));
recipient_id = 0;
sscanf(buf, "%" PRIu64 ":%[^\n]", &recipient_id, msg);
if (!recipient_id || !*msg) {
sscanf(buf, "%[^\n]", msg);
if (!*msg) {
printf("Expected format: <*recipient_id>:<message>");
continue;
}
}
else { /* reset active chat */
struct discord_channel ret_channel = { 0 };
struct discord_ret_channel ret = { .sync = &ret_channel };
if (CCORD_OK == discord_create_dm(client, &params, &ret)) {
dm_channel_id = ret_channel.id;
discord_channel_cleanup(&ret_channel);
}
}
struct discord_create_message params = { .content = msg };
discord_create_message(client, dm_channel_id, &params, &ret);
}
pthread_exit(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_message_create(client, &on_dm_receive);
/* Keep just DISCORD_GATEWAY_DIRECT_MESSAGES */
print_usage();
fgetc(stdin); // wait for input
pthread_t tid;
pthread_create(&tid, NULL, &read_input, client);
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.
#define DISCORD_SYNC_FLAG
flag for enabling sync mode without expecting a datatype return
Definition: discord-response.h:63
Public functions and datatypes.
void discord_channel_cleanup(struct discord_channel *self)
@ CCORD_OK
Definition: error.h:42
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_create_dm(struct discord *client, struct discord_create_dm *params, struct discord_ret_channel *ret)
Create a new DM channel with a given user.
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_add_intents(struct discord *client, uint64_t code)
Subscribe to Discord Events.
void discord_remove_intents(struct discord *client, uint64_t code)
Unsubscribe from Discord Events.
void discord_set_on_message_create(struct discord *client, void(*callback)(struct discord *client, const struct discord_message *event))
Triggers when a message is created.
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 DISCORD_GATEWAY_MESSAGE_CONTENT
Definition: gateway.h:41
#define DISCORD_GATEWAY_GUILD_MESSAGES
Definition: gateway.h:35
#define log_info(...)
Definition: log.h:52
Definition: channel.h:122
u64snowflake id
Definition: channel.h:124
Definition: user.h:164
u64snowflake recipient_id
Definition: user.h:167
Definition: channel.h:683
Definition: channel.h:195
char * content
Definition: channel.h:207
struct discord_user * author
Definition: channel.h:203
Definition: gateway.h:332
struct discord_user * user
Definition: gateway.h:336
Request's return context.
Definition: discord-response.h:84
struct discord_channel * sync
Definition: discord-response.h:84
Request's return context.
Definition: discord-response.h:86
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