| 1 | \page firstbot Creating Your First Bot |
| 2 | |
| 3 | In this example we will create a C++ version of the [discord.js](https://discord.js.org/#/) example program. |
| 4 | |
| 5 | The two programs can be seen side by side below: |
| 6 | |
| 7 | <table> |
| 8 | <tr> |
| 9 | <th>D++</th> |
| 10 | <th>Discord.js</td> |
| 11 | </tr> |
| 12 | <tr> |
| 13 | <td> |
| 14 | |
| 15 | \include{cpp} firstbot.cpp |
| 16 | |
| 17 | </td> |
| 18 | <td> |
| 19 | |
| 20 | ~~~~~~~~~~~~~~~js |
| 21 | let Discord = require('discord.js'); |
| 22 | |
| 23 | |
| 24 | let BOT_TOKEN = 'add your token here'; |
| 25 | |
| 26 | |
| 27 | let bot = new Discord.Client({ intents: [] }); |
| 28 | |
| 29 | |
| 30 | bot.on('interactionCreate', (interaction) => { |
| 31 | if (interaction.isCommand() && interaction.commandName === 'ping') { |
| 32 | interaction.reply({content: 'Pong!'}); |
| 33 | } |
| 34 | }); |
| 35 | |
| 36 | |
| 37 | bot.once('ready', async () => { |
| 38 | await client.commands.create({ |
| 39 | name: 'ping', |
| 40 | description: "Ping pong!" |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | bot.login(BOT_TOKEN); |
| 45 | ~~~~~~~~~~~~~~~ |
| 46 | |
| 47 | </td> |
| 48 | </tr> |
| 49 | </table> |
| 50 | |
| 51 | Let's break this program down step by step: |
| 52 | |
| 53 | ## 1. Start with an empty C++ program |
| 54 | |
| 55 | Make sure to include the header file for the D++ library with the instruction `#include <dpp/dpp.h>`! |
| 56 | |
| 57 | \include{cpp} firstbot1.cpp |
| 58 | |
| 59 | ## 2. Create an instance of dpp::cluster |
| 60 | |
| 61 | To make use of the library you must create a dpp::cluster object. This object is the main object in your program like the `Discord.Client` object in Discord.js. |
| 62 | |
| 63 | You can instantiate this class as shown below. Remember to put your bot token in the constant! |
| 64 | |
| 65 | \include{cpp} firstbot2.cpp |
| 66 | |
| 67 | ## 3. Attach to an event |
| 68 | |
| 69 | To have a bot that does something, you should attach to some events. Let's start by attaching to the `on_ready` event (dpp::cluster::on_ready) which will notify your program when the bot is connected. In this event, we will register a slash command called 'ping'. Note that we must wrap our registration of the command in a template called dpp::run_once which prevents it from being re-run every time your bot does a full reconnection (e.g. if the connection fails). |
| 70 | |
| 71 | \include{cpp} firstbot3.cpp |
| 72 | |
| 73 | ## 4. Attach to another event to receive slash commands |
| 74 | |
| 75 | If you want to handle a slash command, you should also attach your program to the `on_slashcommand` event (dpp::cluster::on_slashcommand) which is basically the same as the Discord.js `interactionCreate` event. Lets add this to the program before the `on_ready` event: |
| 76 | |
| 77 | \include{cpp} firstbot4.cpp |
| 78 | |
| 79 | ## 5. Add some content to the events |
| 80 | |
| 81 | Attaching to an event is a good start, but to make a bot you should actually put some program code into the interaction event. We will add some code to the `on_slashcommand` to look for our slash command '/ping' and reply with `Pong!`: |
| 82 | |
| 83 | \include{cpp} firstbot5.cpp |
| 84 | |
| 85 | Let's break down the code in the `on_slashcommand` event so that we can discuss what it is doing: |
| 86 | |
| 87 | ~~~~~~~~~~~~~~~~~~~~~~~cpp |
| 88 | bot.on_slashcommand([](const dpp::slashcommand_t& event) { |
| 89 | if (event.command.get_command_name() == "ping") { |
| 90 | event.reply("Pong!"); |
| 91 | } |
| 92 | }); |
| 93 | ~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | |
| 95 | This code is simply comparing the command name `event.command.get_command_name()` (dpp::interaction::get_command_name) against the value in a constant string value `"ping"`. If they match, then the `event.reply` method is called. |
| 96 | |
| 97 | The `event.reply` function (dpp::slashcommand_t::reply) replies to a slash command with a message. There are many ways to call this function to send embed messages, upload files, and more, but for this simple demonstration we will just send some message text. |
| 98 | |
| 99 | ## 6. Add code to start the bot! |
| 100 | |
| 101 | To make the bot start, we must call the dpp::cluster::start method, e.g. in our program by using `bot.start(dpp::st_wait)`. |
| 102 | |
| 103 | We also add a line to tell the library to output all its log information to the console, `bot.on_log(dpp::utility::cout_logger());` - if you wanted to do something more advanced, you can replace this parameter with a lambda just like all other events. |
| 104 | |
| 105 | The parameter which we set to false indicates if the function should return once all shards are created. Passing dpp::st_wait here tells the program you do not need to do anything once `bot.start` is called. |
| 106 | |
| 107 | \include{cpp} firstbot6.cpp |
| 108 | |
| 109 | ## 7. Compile and run your bot |
| 110 | |
| 111 | Compile your bot using `g++ -std=c++20 -o bot bot.cpp -ldpp` (if your .cpp file is called `bot.cpp`) and run it with `./bot`. |
| 112 | |
| 113 | ## 8. Inviting your bot to your server |
| 114 | |
| 115 | When you invite your bot, you must use the `applications.commands` and `bots` scopes to ensure your bot can create guild slash commands. For example: |
| 116 | |
| 117 | ```url |
| 118 | https://discord.com/oauth2/authorize?client_id=YOUR-BOTS-ID-HERE&scope=bot+applications.commands&permissions=BOT-PERMISSIONS-HERE |
| 119 | ``` |
| 120 | |
| 121 | Replace `YOUR-BOTS-ID-HERE` with your bot's user ID, and `BOT-PERMISSIONS-HERE` with the permissions your bot requires. |
| 122 | |
| 123 | **Congratulations** - you now have a working bot using the D++ library! |
| 124 |