Some basic things you should have covered before proceeding:
- All of the necessary dependencies. The dependencies can be found at the Build Instructions.
- A bot token. discord-irc has instructions on how to get one.
my_bot/
is your working directory, go into it to make your basic configuration:
cd my_bot
make config
Add your token to config.json
by assigning it to discord's "token" field.
Here you have the option of editing myBot.c
or start one from scratch.
The entire code of ping-pong bot is below. We will go over it in further down:
#include <stdio.h>
#include <stdlib.h>
#include "discord.h"
void on_ready(struct discord *client)
{
const struct discord_user *bot = discord_get_self(client);
log_info("PingPong-Bot succesfully connected to Discord as %s#%s!",
bot->username, bot->discriminator);
}
void on_ping(struct discord *client, const struct discord_message *msg)
{
if (msg->author->bot) return; // ignore bots
struct discord_create_message params = { .content = "pong" };
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
void on_pong(struct discord *client, const struct discord_message *msg)
{
if (msg->author->bot) return; // ignore bots
struct discord_create_message params = { .content = "ping" };
discord_create_message(client, msg->channel_id, ¶ms, NULL);
}
int main(void)
{
struct discord *client = discord_config_init("../config.json");
discord_set_on_ready(client, &on_ready);
discord_set_on_command(client, "ping", &on_ping);
discord_set_on_command(client, "pong", &on_pong);
discord_run(client);
discord_cleanup(client);
return 0;
}
You can initialize the bot by providing a config.json
file:
struct discord *client = discord_config_init("../config.json");
You can also initialize it by providing the token directly to discord_init()
:
struct discord *client = discord_init(BOT_TOKEN);
discord_config_init(char[])
: initialize the bot with a configuration file
Returns struct discord
: the bot client
Member Parameters | Description |
---|---|
char[] | the name of the bot config file |
discord_init(char[])
: initialize the bot with a token
Returns struct discord
the bot client
Member Parameters | Description |
---|---|
char[] | the bot token string |
discord_set_on_ready(client, &on_ready);
discord_set_on_command(client, "ping", &on_ping);
discord_set_on_command(client, "pong", &on_pong);
discord_run(client);
discord_set_on_ready(struct discord*, discord_on_idle*)
: calls on_ready
callback when the connection is succesfully established
Member Parameters | Description |
---|---|
struct discord |
the client stucture |
discord\_on\_idle() |
the callback to run when the READY event is triggered |
discord_set_on_command(struct discord*, char[], discord_message_cb*)
: runs callback when a command prefix is detected on chat
Member Parameters | Description |
---|---|
struct discord |
the client stucture |
char[] | The chat command expected to trigger a callback response |
discord\_on\_message |
the message type function callback to run when its corresponding event is triggered |
discord_run(struct discord*)
: establishes a connection to Discord, run until error or shutdown
Member Parameters | Description |
---|---|
struct discord |
the client stucture |
discord_cleanup(client);
discord_cleanup(struct discord*)
: cleanup client initialized by discord_init()
or discord_config_init()
make
Note: The preset Makefile will separately compile each file from the my_bot
folder with .c
extension.
Simply run the generated executable like so:
./myBot
By heading to a channel your bot has access to and then type "ping" or "pong".
With Ctrl+c or by closing the Terminal.