Cpp-Concord: C library for handling the Discord API


Concord



Discord server

About

Concord is implemented in plain C99, its symbols are organized to be easily matched to the documentation of the API being covered. Concord's implementation has minimum external dependencies to make bot deployment deadly simple.

Minimal example

#include <string.h> // strcmp()
#include <concord/discord.h>

void on_ready(struct discord *client) 
{
  const struct discord_user *bot = discord_get_self(client);
  log_info("Logged in as %s!", bot->username);
}

void on_message(struct discord *client, const struct discord_message *msg)
{
  if (strcmp(msg->content, "ping") != 0)
    return; // ignore messages that aren't 'ping'

  discord_async_next(client, NULL); // make next request non-blocking (OPTIONAL)
  struct discord_create_message_params params = { .content = "pong" };
  discord_create_message(client, msg->channel_id, &params, NULL);
}

int main(void)
{
  struct discord *client = discord_init(BOT_TOKEN);
  discord_set_on_ready(client, &on_ready);
  discord_set_on_message_create(client, &on_message);
  discord_run(client);
}

This is a minimalistic example, refer to examples/ for a better overview.

Build Instructions

On Windows

  • Install Cygwin
  • Make sure that you installed libcurl, gcc, make, and git when you ran the Cygwin installer!

On Linux

The only dependency is curl-7.4.1 or higher

Ubuntu and Debian

$ sudo apt install -y build-essential libcurl4-openssl-dev

Void Linux

$ sudo xbps-install -S libcurl-devel

Setting up your environment

Clone Concord into your workspace

$ git clone https://github.com/cogmasters/concord.git && cd concord

Compile Concord

$ make

Configuring Concord

The following outlines the default fields of config.json

{
  "logging": { // logging directives
    "level": "trace",        // trace, debug, info, warn, error, fatal
    "filename": "bot.log",   // the output file
    "quiet": false,          // change to true to disable logs in console
    "overwrite": false,      // overwrite existing file with "filename"
    "use_color": true,       // log with color
    "http": {
      "enable": true,        // generate http specific logging
      "filename": "http.log" // the output file
    },
    "disable_modules": ["WEBSOCKETS", "USER_AGENT"] // disable logging for these modules
  },
  "discord": { // discord directives
    "token": "YOUR-BOT-TOKEN",          // replace with your bot token
    "default_prefix": {                 
      "enable": false,                  // enable default command prefix
      "prefix": "YOUR-COMMANDS-PREFIX"  // replace with your prefix
    }
  }
}

Test Copycat-Bot

  1. 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.
  2. Build example executables:
    $ make examples
  3. Run Copycat-Bot:
    $ cd examples && ./copycat

Get Copycat-Bot Response

Type a message in any channel the bot is part of and the bot should send an exact copy of it in return.

Terminate Copycat-Bot

With Ctrl+c or by closing the Terminal.

Create your first bot

  • Head to my_bot/, a special folder set-up for your convenience that may be modified freely.
  • Read our guide for building your first bot.

Installing Concord

Concord can be installed in case developing inside of my_bot/ doesn't suit your needs:

$ sudo make install

Included headers must be concord/ prefixed:

#include <concord/discord.h>

Standalone executable

GCC (Recommended)

$ gcc myBot.c -o myBot -pthread -ldiscord -lcurl

Clang

$ clang myBot.c -o myBot -pthread -ldiscord -lcurl

Recommended debuggers

First, make sure your executable is compiled with the -g flag to ensure human-readable debugger messages.

Valgrind

Using valgrind to check for memory leaks:

$ valgrind --leak-check=full ./myBot

For a more comprehensive guide check Valgrind's Quick Start.

GDB

Using GDB to check for runtime errors, such as segmentation faults:

$ gdb ./myBot

And then execute your bot from the gdb environment:

(gdb) run

If the program has crashed, get a backtrace of the function calls leading to it:

(gdb) bt

For a more comprehensive guide check Beej's Quick Guide to GDB

Support

Problems? Check out our Discord Server.

Links

Contributing

Check our Contributing Guidelines to get started! If you are here for the Discord API, please check our Discord API Roadmap.

Give us a star if you like this project!

Comments

  • Missing things.
    Missing things.

    Jan 8, 2022

    discord_start_thread_with_message_params is missing rate_limit_per_user. discord_start_thread_without_message_params is missing invitable & rate_limit_per_user.

    missing feature 
    Reply
  • 4 space tabbing
    4 space tabbing

    Jan 11, 2022

    null

                                                                                                                                                                                                           
    Reply
  • Make async default
    Make async default

    Jan 16, 2022

    Adding the link to the documentation to the Links section.

    Reply
  • Make async default
    Make async default

    Jan 17, 2022

    null

                                                                                                                                                                                                           
    Reply
  • docs: added Alpine instructions
    docs: added Alpine instructions

    Jan 17, 2022

    null

                                                                                                                                                                                                           
    Reply
  • docs(README.md): fix logo link
    docs(README.md): fix logo link

    Jan 7, 2022

    null

                                                                                                                                                                                                           
    Reply
  • High CPU usage after ws shutdown.
    High CPU usage after ws shutdown.

    Jan 11, 2022

    If the ws hoster shutdowns, it will happen a high CPU usage.

    Reply