Concord is an asynchronous C99 Discord API library with minimal external dependencies, and a low-level translation of the Discord official documentation to C code.
| Contents | Description |
|---|---|
| Build Instructions | How to build libcurl and Concord |
| Environment Setup | Project setup and compilation |
| Configuration | How to configure Concord with config.json |
| Example Bot | Test Concord with a simple copycat bot |
| Build Options | Special flags and build targets |
| Installation | Installing Concord on your system |
| Contributing | How to contribute to Concord |
- Config.json Directives - Configure your bot using JSON
- Setup with Environment Variables - Use environment variables
- Embeds - Create rich message embeds
- Slash Commands - Implement Discord application commands
- Event Scheduler - Control Discord event processing
- SQLite3 with Concord - Persist data with SQLite
- Databases with Concord - General database integration
- Debugging - Debug your Concord applications
Following are other standalone guides created by the community to help you get started with Concord, organized by category:
- Compiling on Windows - Build Concord on Windows systems
- Installing Concord (Termux) - Install on Android using Termux
- Msys2 and Mingw64 - Setup using MSYS2 environment
- Cross Compiling - Compile for different target platforms
- Concord on Old Systems - Support for legacy systems
The following are minimalistic examples, refer to examples/ for a better overview.
#include <string.h>
#include <concord/discord.h>
#define GUILD_ID 1234567898765431 // replace with your guild ID
void on_ready(struct discord *client, const struct discord_ready *event) {
struct discord_create_guild_application_command params = {
.name = "ping",
.description = "Ping command!"
};
discord_create_guild_application_command(client, event->application->id,
GUILD_ID, ¶ms, NULL);
}
void on_interaction(struct discord *client, const struct discord_interaction *event) {
if (event->type != DISCORD_INTERACTION_APPLICATION_COMMAND)
return; /* return if interaction isn't a slash command */
if (strcmp(event->data->name, "ping") == 0) {
struct discord_interaction_response params = {
.type = DISCORD_INTERACTION_CHANNEL_MESSAGE_WITH_SOURCE,
.data = &(struct discord_interaction_callback_data){
.content = "pong"
}
};
discord_create_interaction_response(client, event->id,
event->token, ¶ms, NULL);
}
}
int main(void) {
struct discord *client = discord_from_token(BOT_TOKEN);
discord_set_on_ready(client, &on_ready);
discord_set_on_interaction_create(client, &on_interaction);
discord_run(client);
}#include <string.h>
#include <concord/discord.h>
#include <concord/logmod.h>
void on_ready(struct discord *client, const struct discord_ready *event) {
logmod_log(INFO, NULL, "Logged in as %s!", event->user->username);
}
void on_message(struct discord *client, const struct discord_message *event) {
if (strcmp(event->content, "ping") == 0) {
struct discord_create_message params = { .content = "pong" };
discord_create_message(client, event->channel_id, ¶ms, NULL);
}
}
int main(void) {
struct discord *client = discord_from_token(BOT_TOKEN);
discord_add_intents(client, DISCORD_GATEWAY_MESSAGE_CONTENT);
discord_set_on_ready(client, &on_ready);
discord_set_on_message_create(client, &on_message);
discord_run(client);
}- GNU/Linux 4.x
- FreeBSD 12
- NetBSD 8.1
- Windows 7 (Cygwin)
- GNU/Hurd 0.9
- Mac OS X 10.9
Note: big-endian processors running certain OSes like SPARC Solaris, PowerPC AIX, System Z z/OS or Linux, or MIPS IRIX are NOT supported. There are currently a few issues that prevent some of the logic from correctly on big-endian systems. This will be fixed soon.
The only dependency is curl-8.7.1 or higher with websockets support. Since many system package managers don't provide this specific version with websockets enabled, we recommend compiling from source.
Important
Libcurl must be compiled with the --enable-websockets flag to work with Concord.
- Install Cygwin
- During Cygwin installation, select packages:
gcc,make,git,autoconf,libtool,pkg-config,openssl-devel,zlib-devel - Follow the Windows tutorial here
- Then compile libcurl from source (instructions below)
# Install build dependencies
# Ubuntu/Debian:
$ sudo apt-get update && sudo apt-get install -y build-essential autoconf libtool pkg-config libssl-dev zlib1g-dev
# Fedora/RHEL/CentOS:
$ sudo dnf install -y gcc make autoconf libtool pkgconfig openssl-devel zlib-devel
# Arch/Manjaro:
$ sudo pacman -S --needed base-devel openssl zlib
# Alpine:
$ apk add build-base autoconf libtool pkgconfig openssl-dev zlib-dev
# FreeBSD:
$ pkg install autoconf libtool pkgconf openssl
# OS X:
$ xcode-select --install
$ brew install autoconf libtool pkg-config openssl@3
# or with MacPorts:
$ port install autoconf libtool pkgconfig openssl
# Download and compile libcurl
$ curl -LO https://curl.se/download/curl-8.7.1.tar.gz
$ tar -xzf curl-8.7.1.tar.gz
$ cd curl-8.7.1
$ ./configure --with-openssl --enable-websockets
$ make
$ sudo make install
# You might need to inform the system's dynamic linker about the new library location with:
$ echo "/usr/local/lib" > /etc/ld.so.conf.d/curl.conf
$ sudo ldconfig$ git clone https://github.com/cogmasters/concord.git && cd concord$ makeYou can speed up the compilation process by using -j flag with make:
$ make -j$(nproc)This will use all available CPU cores to compile Concord. You can also specify a number instead of $(nproc) to limit the number of jobs.
Concord does not support voice connections yet. If you want to use voice connections, you can use CogLink instead. CogLink is a separate project that provides a LavaLink client for Concord. It is not part of the Concord library, but it is designed to work seamlessly with Concord.
You might run into trouble with the compiler and linker not finding your Libcurl headers. You can do something like this:
$ CFLAGS=-I<some_path> LDFLAGS=-L<some_path> makeFor instance, on a FreeBSD system:
$ CFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib makeOn OS X using MacPorts:
$ CFLAGS=-I/opt/local/include LDFLAGS=-L/opt/local/lib makeOn OS X using a self-compiled libcurl:
$ CFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/include makeOn Windows with Cygwin, you might need to pass both arguments to use POSIX threading:
$ CFLAGS="-pthread -lpthread" makeIn some cases, you might want to link Concord into a shared object, or link it as a shared object into another shared
object. In that case, you will need to compile Concord with CFLAGS="-fpic" make.
Concord will compile with compilers that aren't GCC or Clang, but you may need to do some work. For instance, support exists for Sun Studio C (specifically, Oracle Developer Studio 12.6), but you will need to change options in the Makefiles (look for CFLAGS and WFLAGS) to prevent errors about unknown compiler option flags.
discord_config_init() is the initialization method that allows configuring your bot without recompiling.
The following outlines config.json fields:
{
"token": "YOUR-BOT-TOKEN", // replace with your bot token
"log": { // logging directives
"level": "TRACE", // TRACE, DEBUG, INFO, WARN, ERROR, FATAL
"trace": "bot.log", // the log output file (null to disable)
"quiet": false, // true to disable logs in console
"overwrite": true, // true overwrites the file on each run
"color": true, // display color on console
"http": "http.log", // the HTTP log output file (null to disable)
"ws": "ws.log", // the WebSockets log output file (null to disable)
"disable": ["WEBSOCKETS", "HTTP"] // disable logging for specific modules
}
// here you can add your custom fields *
}* Your custom field contents can be fetched with discord_config_get_field()
See more information about the config.json file in the config.json directives guide.
- Get your bot token and add it to
config.json, by assigning it to discord's "token" field. There are well written instructions from discord-irc explaining how to get your bot token and adding it to a server. - Build example executables:
$ make examples - Run Copycat-Bot:
$ cd examples && ./copycat
Type a message in any channel the bot is part of and the bot should send an exact copy of it in return.
With Ctrl+c or with Ctrl+|
The following outlines special flags and targets to override the default Makefile build with additional functionalities.
-DCCORD_SIGINTCATCH- By default Concord will not shutdown gracefully when a SIGINT is received (i.e. Ctrl+c), enable this flag if you wish it to be handled for you.
-DCCORD_DEBUG_WEBSOCKETS- Enable verbose debugging for WebSockets communication.
-DCCORD_DEBUG_HTTP- Enable verbose debugging for HTTP communication.
Example:
$ CFLAGS="-DCCORD_SIGINTCATCH -DCCORD_DEBUG_HTTP" makemake shared- Produce a dynamically-linked version of Concord. This Makefile is intended for GNU-style compilers, such as
gccorclang.
- Produce a dynamically-linked version of Concord. This Makefile is intended for GNU-style compilers, such as
make shared_osx- Produce a dynamically-linked version of Concord, for OS X and Darwin systems.
make debug- Enable some flags useful while developing, such as
-O0and-g
- Enable some flags useful while developing, such as
(note -- # means that you should be running as root)
# make installThis will install the headers and library files into $PREFIX. You can override this as such:
# PREFIX=/opt/concord make installTo cross-compile Concord, see the manual here.
The following are stable and well documented dependencies that are packaged with Concord and can be included to your projects:
| File | Description |
|---|---|
| cog-utils | General purpose functions aimed at portability |
| logmod | A modular logging library |
| carray* | Macro-based implementation of type-safe arrays |
| anomap* | Sorted key/value storage for C99 |
| oa_hash | A lightweight open-addressing hashtable |
| json-build | Tiny, zero-allocation JSON serializer |
| jsmn-find | Tiny, zero-allocation JSON tokenizer |
* Concord uses its own modified version that may be not up to date with the original
Note that included headers must be concord/ prefixed:
#include <concord/discord.h>
#include <concord/logmod.h>$ gcc myBot.c -o myBot -pthread -ldiscord -lcurl$ clang myBot.c -o myBot -pthread -ldiscord -lcurl- IBM XL C/C++ (AIX, z/OS, IBM i)
- Sun/Oracle Studio (Solaris)
- IRIX MIPSpro C++ (IRIX) -- NOTE: currently not supported
- HP aCC (HP-UX)
- Compaq C (Tru64 UNIX) -- NOTE: also currently not supported Note: if you want to actually compile this on one of the systems listed above, please see the "Compiling on old computers" guide.
$ cc myBot.c -o myBot -ldiscord -lcurl -lpthreadNote: some systems such as Cygwin require you to do this:
$ gcc myBot.c -o myBot -pthread -lpthread -ldiscord -lcurl(this links against libpthread.a in /usr/lib)
Problems? Check out our Discord Server
All kinds of contributions are welcome, all we ask is to abide to our guidelines! If you want to help but is unsure where to get started then our Discord API Roadmap is a good starting point. Check the following guides for more information on how to contribute:
- Contributing - How to contribute to Concord
- Project Outline - Overview of the project structure
- Coding Guidelines - Coding style and best practices
- Discord API Roadmap - Overview of Discord API features and their status
- Concord Internals - Understand how Concord works internally