Concord - C Discord API library
A Discord API wrapper library written in C
discord.h
Go to the documentation of this file.
1
11#ifndef DISCORD_H
12#define DISCORD_H
13
14#ifdef __cplusplus
15extern "C" {
16#endif /* __cplusplus */
17
18#include <inttypes.h>
19#include <stdbool.h>
20
21#include "concord-error.h"
22#include "types.h"
23#include "io_poller.h"
24#define LOGMOD_HEADER
25#include "logmod.h"
26
27#ifndef DISCORD_VERSION
32#define DISCORD_VERSION "10"
33#endif
34
35#define DISCORD_API_BASE_URL "https://discord.com/api/v" DISCORD_VERSION
36#define DISCORD_GATEWAY_URL_SUFFIX "?v=" DISCORD_VERSION "&encoding=json"
37
38/* forward declaration */
39struct discord;
40struct reflectc;
41
42
49struct reflectc *discord_get_registry(struct discord *client);
50
51#include "discord_codecs.h"
52#include "discord-response.h"
53
65#define DISCORD_MAX_NAME_LEN 4 * 100 + 1
66#define DISCORD_MAX_TOPIC_LEN 4 * 1024 + 1
67#define DISCORD_MAX_DESCRIPTION_LEN 4 * 2048 + 1
68#define DISCORD_MAX_USERNAME_LEN 4 * 32 + 1
69#define DISCORD_MAX_DISCRIMINATOR_LEN 4 + 1
70#define DISCORD_MAX_REASON_LEN 4 * 512 + 1
71#define DISCORD_MAX_MESSAGE_LEN 4 * 2000 + 1
72#define DISCORD_MAX_PAYLOAD_LEN 4 * 4096 + 1
78#define DISCORD_EMBED_TITLE_LEN 4 * 256 + 1
79#define DISCORD_EMBED_DESCRIPTION_LEN 4 * 4096 + 1
80#define DISCORD_EMBED_MAX_FIELDS 25
81#define DISCORD_EMBED_FIELD_NAME_LEN 4 * 256 + 1
82#define DISCORD_EMBED_FIELD_VALUE_LEN 4 * 1024 + 1
83#define DISCORD_EMBED_FOOTER_TEXT_LEN 4 * 2048 + 1
84#define DISCORD_EMBED_AUTHOR_NAME_LEN 4 * 256 + 1
90#define DISCORD_WEBHOOK_NAME_LEN 4 * 80 + 1
98/* XXX: As new values are added, discord_strerror() and
99 * discord_code_as_string() should be updated accordingly! */
105#define CCORD_DISCORD_OK CCORD_OK
108#define CCORD_PENDING 1
110#define CCORD_DISCORD_JSON_CODE 100
112#define CCORD_DISCORD_BAD_AUTH 101
114#define CCORD_DISCORD_RATELIMIT 102
116#define CCORD_DISCORD_CONNECTION 103
117
125
133const char *discord_strerror(CCORDcode code, struct discord *client);
134
143#include "audit_log.h"
144#include "auto_moderation.h"
145#include "invite.h"
146#include "channel.h"
147#include "emoji.h"
148#include "guild.h"
150#include "guild_template.h"
151#include "stage_instance.h"
152#include "sticker.h"
153#include "user.h"
154#include "voice.h"
155#include "webhook.h"
156#include "gateway.h"
157#include "oauth2.h"
161#include "application_command.h"
162#include "interaction.h"
173#include "discord-cache.h"
174#include "discord-events.h"
175
184#define discord_claim(client, data) (__discord_claim(client, data), data)
185void __discord_claim(struct discord *client, const void *data);
186
197void discord_unclaim(struct discord *client, const void *data);
198
200#define ccord_global_init()
201
203#define ccord_global_cleanup()
204
213
220
225#define ccord_shutdown_async() discord_shutdown_all()
226
234struct discord *discord_from_token(const char token[]);
235
243struct discord *discord_from_json(const char config_file[]);
244
252 char *token;
261 char *base_url;
262 struct {
266 bool quiet;
268 bool color;
271 /* the trace log file */
272 FILE *trace;
273 /* the http log file */
274 FILE *http;
275 /* the ws log file */
276 FILE *ws;
277 struct {
278 size_t size;
279 char **ids;
281 } log;
282};
283
293
298#define discord_init discord_from_token
299
304#define discord_config_init discord_from_json
305
335 char *const path[],
336 unsigned depth);
337
347struct discord *discord_clone(const struct discord *orig);
348
355void discord_cleanup(struct discord *client);
356
364const struct discord_user *discord_get_self(struct discord *client);
365
373
379void discord_shutdown(struct discord *client);
380
388void discord_reconnect(struct discord *client, bool resume);
389
399void *discord_set_data(struct discord *client, void *data);
400
409void *discord_get_data(struct discord *client);
410
419int discord_get_ping(struct discord *client);
420
427uint64_t discord_timestamp(struct discord *client);
428
435uint64_t discord_timestamp_us(struct discord *client);
436
444struct logmod *discord_get_logmod(struct discord *client);
445
452struct io_poller *discord_get_io_poller(struct discord *client);
453
458#define __CAT(_a, _b) _a##_b
459#define _CAT(_a, _b) __CAT(_a, _b)
460#define _EXPECT_CONTAINER__struct
461#define _EXPECT_CONTAINER__union
462#define _DISCORD_SYMBOL_WITHOUT_CONTAINER(_symbol) \
463 _CAT(_EXPECT_CONTAINER__, _symbol)
464
470#define discord_data_wrap reflectc_wrap
471
480#define discord_data_wrap_from(_symbol, _client, _data) \
481 _CAT(reflectc_from_, _DISCORD_SYMBOL_WITHOUT_CONTAINER(_symbol))( \
482 discord_get_registry((_client)), _data, NULL)
483
496#define discord_data_cleanup(_client, _data) \
497 discord_data_free(discord_get_registry((_client)), (_data))
498
500void discord_data_free(struct reflectc *registry, void *data);
501
515#define discord_data_unwrap(_client, _data) \
516 discord_data_release(discord_get_registry((_client)), (_data))
517
519void discord_data_release(struct reflectc *registry, void *data);
520
525/* forward declaration */
526struct jsmnf_pair;
527struct jsonb;
528
538 char *p_buf[],
539 size_t *p_bufsize);
540
551#define discord_data_to_json(_symbol, _client, _data, _p_buf, _p_bufsize) \
552 discord_data_wrap_to_json( \
553 discord_data_wrap_from(_symbol, _client, _data), _p_buf, _p_bufsize)
554
565 const struct discord_data_wrap *member,
566 char *p_buf[],
567 size_t *p_bufsize);
568
580#define discord_data_to_jsonb(_symbol, _client, _jb, _data, _p_buf, \
581 _p_bufsize) \
582 discord_data_wrap_to_jsonb( \
583 _jb, discord_data_wrap_from(_symbol, _client, _data), _p_buf, \
584 _p_bufsize)
585
595 size_t len,
596 struct discord_data_wrap *root);
597
608#define discord_data_from_json(_symbol, _client, _json, _len, _data) \
609 discord_data_wrap_from_json( \
610 _json, _len, discord_data_wrap_from(_symbol, _client, _data))
611
621CCORDcode discord_data_wrap_from_jsmnf(const struct jsmnf_pair *p,
622 const char *json,
623 size_t length,
624 const struct discord_data_wrap *member);
625
637#define discord_data_from_jsmnf(_client, _p, _json, _length, _type, _data) \
638 discord_data_wrap_from_jsmnf( \
639 _p, _json, _length, discord_data_wrap_from(_type, _client, _data))
640
649/* forward declaration */
650struct discord_timer;
651
652
654typedef void (*discord_ev_timer)(struct discord *client,
655 struct discord_timer *ev);
656
675};
676
680 unsigned id;
689 void *data;
691 int64_t delay;
693 int64_t interval;
695 int64_t repeat;
696};
697
705unsigned discord_timer_ctl(struct discord *client,
706 struct discord_timer *timer);
707
721unsigned discord_timer(struct discord *client,
722 discord_ev_timer on_tick_cb,
723 discord_ev_timer on_status_changed_cb,
724 void *data,
725 int64_t delay);
726
742unsigned discord_timer_interval(struct discord *client,
743 discord_ev_timer on_tick_cb,
744 discord_ev_timer on_status_changed_cb,
745 void *data,
746 int64_t delay,
747 int64_t interval,
748 int64_t repeat);
749
758bool discord_timer_get(struct discord *client,
759 unsigned id,
760 struct discord_timer *timer);
761
769bool discord_timer_start(struct discord *client, unsigned id);
770
778bool discord_timer_stop(struct discord *client, unsigned id);
779
788bool discord_timer_cancel(struct discord *client, unsigned id);
789
797bool discord_timer_delete(struct discord *client, unsigned id);
798
806bool discord_timer_cancel_and_delete(struct discord *client, unsigned id);
807
814#ifdef __cplusplus
815}
816#endif /* __cplusplus */
817
818#endif /* DISCORD_H */
Application Command public functions and datatypes.
Audit Log public functions and datatypes.
Auto Moderation public functions and datatypes.
Channel public functions and datatypes.
Caching of Discord resources.
Listen, react and trigger Discord Gateway events.
Generic macros for initializing a discord_response and return handles.
struct reflectc * discord_get_registry(struct discord *client)
Get the reflectc registry from a Discord client.
Emoji public functions and datatypes.
Gateway public functions and datatypes.
int CCORDcode
Concord error codes.
Definition: concord-error.h:11
void discord_cleanup(struct discord *client)
Free a Discord Client handle.
bool discord_shutdown_all_ongoing(void)
Check if all Discord connections shutting down is in progress.
CCORDcode discord_run(struct discord *client)
Start a connection to the Discord Gateway.
uint64_t discord_timestamp(struct discord *client)
Get the current timestamp (in milliseconds)
const struct discord_user * discord_get_self(struct discord *client)
Get the client's cached user.
void discord_unclaim(struct discord *client, const void *data)
Unclaim ownership of a resource provided by Concord.
struct discord * discord_from_token(const char token[])
Creates a Discord Client handle from a token.
struct logmod * discord_get_logmod(struct discord *client)
Retrieve client's logging module for configuration purposes.
struct discord * discord_clone(const struct discord *orig)
Clone a discord client.
void discord_shutdown_all(void)
Gracefully notify all Discord connections for shutting down.
void discord_shutdown(struct discord *client)
Gracefully shutdown an ongoing Discord connection.
void discord_reconnect(struct discord *client, bool resume)
Gracefully reconnects an ongoing Discord connection.
void * discord_get_data(struct discord *client)
Receive user arbitrary data stored with discord_set_data()
int discord_get_ping(struct discord *client)
Get the client WebSockets ping.
struct discord * discord_from_config(const struct discord_config *config)
Creates a Discord Client handle from a discord_config structure.
struct discord * discord_from_json(const char config_file[])
Creates a Discord Client handle from a config.json file.
void * discord_set_data(struct discord *client, void *data)
Store user arbitrary data that can be retrieved by discord_get_data()
struct io_poller * discord_get_io_poller(struct discord *client)
get the io_poller used by the discord client
uint64_t discord_timestamp_us(struct discord *client)
Get the current timestamp (in microseconds)
struct ccord_szbuf_readonly discord_config_get_field(struct discord *client, char *const path[], unsigned depth)
Get the contents from the config file field.
void __discord_claim(struct discord *client, const void *data)
CCORDcode discord_data_wrap_to_json(const struct reflectc_wrap *member, char *p_buf[], size_t *p_bufsize)
Transform a wrapped Discord data type into a JSON string.
CCORDcode discord_data_wrap_to_jsonb(struct jsonb *jb, const struct reflectc_wrap *member, char *p_buf[], size_t *p_bufsize)
Transform a wrapped Discord data type into a jsonb handle.
CCORDcode discord_data_wrap_from_json(const char *json, size_t len, struct reflectc_wrap *root)
Parse a JSON string and fill a wrapped Discord data type.
CCORDcode discord_data_wrap_from_jsmnf(const struct jsmnf_pair *p, const char *json, size_t length, const struct reflectc_wrap *member)
Parse a jsmnf_pair and fill a wrapped Discord data type.
void discord_data_free(struct reflectc *registry, void *data)
#define discord_data_wrap
The Discord data wrap structure.
Definition: discord.h:470
void discord_data_release(struct reflectc *registry, void *data)
const char * discord_code_as_string(CCORDcode code)
Return the value of CCORDcode as a string.
const char * discord_strerror(CCORDcode code, struct discord *client)
Return the meaning of CCORDcode.
unsigned discord_timer_ctl(struct discord *client, struct discord_timer *timer)
modifies or creates a timer
bool discord_timer_cancel(struct discord *client, unsigned id)
cancels a timer, this will delete the timer if DISCORD_TIMER_DELETE_AUTO is enabled
bool discord_timer_start(struct discord *client, unsigned id)
starts a timer
unsigned discord_timer_interval(struct discord *client, discord_ev_timer on_tick_cb, discord_ev_timer on_status_changed_cb, void *data, int64_t delay, int64_t interval, int64_t repeat)
creates a repeating timer that automatically deletes itself upon completion
discord_timer_flags
flags used to change behaviour of timer
Definition: discord.h:658
void(* discord_ev_timer)(struct discord *client, struct discord_timer *ev)
callback to be used with struct discord_timer
Definition: discord.h:654
bool discord_timer_get(struct discord *client, unsigned id, struct discord_timer *timer)
get the data associated with the timer
bool discord_timer_stop(struct discord *client, unsigned id)
stops a timer
unsigned discord_timer(struct discord *client, discord_ev_timer on_tick_cb, discord_ev_timer on_status_changed_cb, void *data, int64_t delay)
creates a one shot timer that automatically deletes itself upon completion
bool discord_timer_cancel_and_delete(struct discord *client, unsigned id)
cancels, and deletes a timer
bool discord_timer_delete(struct discord *client, unsigned id)
deletes a timer
@ DISCORD_TIMER_TICK
Definition: discord.h:670
@ DISCORD_TIMER_MILLISECONDS
Definition: discord.h:660
@ DISCORD_TIMER_INTERVAL_FIXED
Definition: discord.h:674
@ DISCORD_TIMER_DELETE_AUTO
Definition: discord.h:666
@ DISCORD_TIMER_CANCELED
Definition: discord.h:668
@ DISCORD_TIMER_DELETE
Definition: discord.h:664
@ DISCORD_TIMER_MICROSECONDS
Definition: discord.h:662
@ DISCORD_TIMER_GET
Definition: discord.h:672
Guild public functions and datatypes.
Guild Scheduled Event public functions and datatypes.
Guild Template public functions and datatypes.
Interaciton public functions and datatypes.
Invite public functions and datatypes.
logmod_levels
Log levels supported by LogMod.
Definition: logmod.h:64
OAuth2 public functions and datatypes.
Stage Instance public functions and datatypes.
Sticker public functions and datatypes.
Read-only generic sized buffer.
Definition: types.h:57
The Discord configuration handler.
Definition: discord.h:250
FILE * trace
Definition: discord.h:272
char * token
Definition: discord.h:252
struct discord_config::@0::@1 disable
FILE * http
Definition: discord.h:274
FILE * ws
Definition: discord.h:276
bool color
Definition: discord.h:268
char ** ids
Definition: discord.h:279
bool quiet
Definition: discord.h:266
enum logmod_levels level
Definition: discord.h:264
size_t size
Definition: discord.h:278
struct discord_config::@0 log
char * base_url
Definition: discord.h:261
bool overwrite
Definition: discord.h:270
struct used for modifying, and getting info about a timer
Definition: discord.h:678
discord_ev_timer on_status_changed
Definition: discord.h:687
void * data
Definition: discord.h:689
enum discord_timer_flags flags
Definition: discord.h:682
unsigned id
Definition: discord.h:680
int64_t interval
Definition: discord.h:693
discord_ev_timer on_tick
Definition: discord.h:684
int64_t repeat
Definition: discord.h:695
int64_t delay
Definition: discord.h:691
The Discord client handler.
Definition: discord-internal.h:1206
struct discord_config config
Definition: discord-internal.h:1220
Handle for building a JSON string.
Definition: json-build.h:63
Main logging context structure.
Definition: logmod.h:240
User public functions and datatypes.
Voice public functions and datatypes.
Webhook public functions and datatypes.