| 1 | \page shardless Shardless Cluster: Events via Webhooks |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## What are shardless clusters? |
| 6 | |
| 7 | D++ gives you two ways to handle Discord events: sharding via websockets (the default) and webhooks. Websocket shards connect to Discord, handle up to 2,500 servers each, and process events asynchronously via a REST API call. Webhooks, on the other hand, let Discord push events straight to your bot, skipping the extra API call and making responses instant. WebSockets are great for most bots, but webhooks are super efficient for massive bots, using fewer resources while handling tons of traffic. The recommended setup is to run Nginx or Apache as a reverse proxy in front of your D++ bot, ensuring security and scalability. Enabling webhooks is simple—just call a method in your bot to activate them. We’ll guide you through setting it up step by step. |
| 8 | |
| 9 | A simple summary of this process and how it differs is shown below: |
| 10 | |
| 11 | \dot |
| 12 | digraph DPP_Event_Delivery { |
| 13 | rankdir=TB; |
| 14 | node [shape=rect, style="filled, rounded", fontcolor=black, fontsize=10, fontname="Sans", width=3.0, height=0]; |
| 15 | |
| 16 | subgraph cluster_websocket { |
| 17 | label="Shard WebSockets Method"; |
| 18 | fontname="Sans"; |
| 19 | color=skyblue; |
| 20 | style=filled; |
| 21 | fillcolor=lightskyblue; |
| 22 | |
| 23 | websocket [label="WebSocket Shard connects to Discord\n- Each shard handles up to 2500 guilds", fillcolor=lightcoral]; |
| 24 | receive_event [label="Events received from Discord\n- Incoming events from multiple guilds arrive", fillcolor=salmon]; |
| 25 | dispatch_pool [label="Events dispatched to thread pool\n- Events are assigned to worker threads", fillcolor=lightgoldenrodyellow]; |
| 26 | user_process [label="User processes event asynchronously\n- Application logic handles event", fillcolor=yellowgreen]; |
| 27 | rest_api [label="User sends separate REST API request to\nDiscord - This confirms the event was\nprocessed.", fillcolor=mediumseagreen]; |
| 28 | |
| 29 | websocket -> receive_event -> dispatch_pool -> user_process -> rest_api; |
| 30 | } |
| 31 | |
| 32 | subgraph cluster_webhook { |
| 33 | label="Webhook Method"; |
| 34 | fontname="Sans"; |
| 35 | color=skyblue; |
| 36 | style=filled; |
| 37 | fillcolor=lightskyblue; |
| 38 | |
| 39 | https_listener [label="HTTPS Listener waits for Discord webhooks\n- Server listens for incoming HTTP requests", fillcolor=lightcoral]; |
| 40 | webhook_event [label="Discord sends event via webhook\n- Event data sent in HTTP POST request", fillcolor=salmon]; |
| 41 | dispatch_webhook [label="Event dispatched to thread pool\n- Assigned to a worker thread", fillcolor=lightgoldenrodyellow]; |
| 42 | user_process_webhook [label="User processes the event\n- Application logic handles event", fillcolor=yellowgreen]; |
| 43 | respond_webhook [label="Response sent immediately in HTTPS\nresponse - No additional REST API request\nneeded", fillcolor=mediumseagreen]; |
| 44 | |
| 45 | https_listener -> webhook_event -> dispatch_webhook -> user_process_webhook -> respond_webhook; |
| 46 | } |
| 47 | } |
| 48 | \enddot |
| 49 | |
| 50 | There are advantages and disadvantages to a shardless webhook bot, the main ones are: |
| 51 | |
| 52 | <table> |
| 53 | <tr> |
| 54 | <th>Advantages</th> |
| 55 | <th>Disadvantages</td> |
| 56 | </tr> |
| 57 | <tr> |
| 58 | <td> |
| 59 | ✅ Scales without restarts, any cluster can service any request <br> |
| 60 | ✅ Can be highly scalable on demand using a load balancer <br> |
| 61 | ✅ Can be scaled and proxied by cloudflare and other free services <br> |
| 62 | ✅ No need to keep shards connected, saving resources <br> |
| 63 | </td> |
| 64 | <td> |
| 65 | ❎ Only slash commands and other components interactions can be used as entry points to the bot. Other events do not fire. <br> |
| 66 | ❎ Special consideration needs to be given to request/response flow. <br> |
| 67 | ❎ Without shards you don't have cache, so must rely entirely on resolved data and API calls. <br> |
| 68 | </td> |
| 69 | </tr> |
| 70 | </table> |
| 71 | |
| 72 | ## Creating the Discord bot |
| 73 | |
| 74 | The first step is actually the easiest; enabling websocket event support in D++ is as simple as one method call, and specific parameters when initialising the cluster.bot |
| 75 | |
| 76 | Please see the example below: |
| 77 | |
| 78 | \include{cpp} shardless.cpp |
| 79 | |
| 80 | Note that you can get the public key from your application settings in the Discord Developer Portal. It is **not** your bot token! |
| 81 | |
| 82 | \warning It is important that if your bot works via webhook events, you do not delay in responding. You must respond as fast as possible to the request, with an `event.reply()`. You can always acknowledge the response with `event.thinking()` and edit the interaction later, but this must be done in a separate flow, NOT within the slashcommand event. This is because when you are in the event handler you are directly building a HTTP response that is directly sent back to Discord in an established HTTP request. You can follow up with other API calls if you wish later on. Each request is placed into D++'s thread pool, so you don't have to worry too much about blocking for a little while but remember users expect snappy responses and Discord have a 3 second hard limit on replies! |
| 83 | |
| 84 | ## Setting Up a reverse proxy to route to your bot |
| 85 | |
| 86 | Next, you should set up a reverse proxy using **Nginx** or **Apache** to forward requests from a public-facing HTTPS endpoint to a Discord bot listening on `localhost:3000`. Discord mandates that all webhook endpoints for interactions must be HTTPS with a valid non self-signed certificate. |
| 87 | |
| 88 | We will use a free **Let's Encrypt** certificate for SSL on the proxy, while allowing the bot itself to run using plaintext or self-signed SSL. |
| 89 | |
| 90 | ### Prerequisites |
| 91 | - A **Linux server** |
| 92 | - **Nginx** or **Apache** installed |
| 93 | - A domain name (`example.com`) pointing to your server |
| 94 | - A Discord bot running on **port 3000** |
| 95 | - `certbot` installed for Let's Encrypt SSL |
| 96 | |
| 97 | ## Option 1: Setting Up Nginx as a Reverse Proxy |
| 98 | |
| 99 | ### Install Nginx |
| 100 | ```sh |
| 101 | sudo apt update |
| 102 | sudo apt install nginx -y |
| 103 | ``` |
| 104 | |
| 105 | ### Configure Nginx |
| 106 | Create a new configuration file: |
| 107 | ```sh |
| 108 | sudo nano /etc/nginx/sites-available/discord_bot |
| 109 | ``` |
| 110 | |
| 111 | Paste the following for **plaintext (http)** bot communication: |
| 112 | ```nginx |
| 113 | server { |
| 114 | listen 80; |
| 115 | server_name example.com; |
| 116 | |
| 117 | location / { |
| 118 | proxy_pass http://localhost:3000; |
| 119 | proxy_set_header Host $host; |
| 120 | proxy_set_header X-Real-IP $remote_addr; |
| 121 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 122 | } |
| 123 | } |
| 124 | ``` |
| 125 | |
| 126 | If the bot uses **self-signed SSL**, modify `proxy_pass`: |
| 127 | ```nginx |
| 128 | location / { |
| 129 | proxy_pass https://localhost:3000; |
| 130 | proxy_ssl_verify off; |
| 131 | } |
| 132 | ``` |
| 133 | |
| 134 | Enable the site and restart Nginx: |
| 135 | ```sh |
| 136 | sudo ln -s /etc/nginx/sites-available/discord_bot /etc/nginx/sites-enabled/ |
| 137 | sudo systemctl restart nginx |
| 138 | ``` |
| 139 | |
| 140 | ### Enable Let's Encrypt SSL |
| 141 | ```sh |
| 142 | sudo certbot --nginx -d example.com |
| 143 | ``` |
| 144 | Certbot will automatically update the Nginx configuration for HTTPS. |
| 145 | |
| 146 | ## Option 2: Setting Up Apache as a Reverse Proxy |
| 147 | |
| 148 | ### Install Apache and Modules |
| 149 | ```sh |
| 150 | sudo apt update |
| 151 | sudo apt install apache2 -y |
| 152 | sudo a2enmod proxy proxy_http ssl |
| 153 | sudo systemctl restart apache2 |
| 154 | ``` |
| 155 | |
| 156 | ### Configure Apache Virtual Host |
| 157 | ```sh |
| 158 | sudo nano /etc/apache2/sites-available/discord_bot.conf |
| 159 | ``` |
| 160 | |
| 161 | Paste the following for **plaintext (http)** bot communication: |
| 162 | ```apache |
| 163 | <VirtualHost *:80> |
| 164 | ServerName example.com |
| 165 | |
| 166 | ProxyPass "/" "http://localhost:3000/" |
| 167 | ProxyPassReverse "/" "http://localhost:3000/" |
| 168 | </VirtualHost> |
| 169 | ``` |
| 170 | |
| 171 | If the bot uses **self-signed SSL**, update it as follows: |
| 172 | ```apache |
| 173 | <VirtualHost *:80> |
| 174 | ServerName example.com |
| 175 | |
| 176 | ProxyPass "/" "https://localhost:3000/" |
| 177 | ProxyPassReverse "/" "https://localhost:3000/" |
| 178 | SSLProxyEngine on |
| 179 | SSLProxyVerify none |
| 180 | SSLProxyCheckPeerCN off |
| 181 | SSLProxyCheckPeerName off |
| 182 | </VirtualHost> |
| 183 | ``` |
| 184 | |
| 185 | Enable the site and restart Apache: |
| 186 | ```sh |
| 187 | sudo a2ensite discord_bot.conf |
| 188 | sudo systemctl restart apache2 |
| 189 | ``` |
| 190 | |
| 191 | ### Enable Let's Encrypt SSL |
| 192 | ```sh |
| 193 | sudo certbot --apache -d example.com |
| 194 | ``` |
| 195 | |
| 196 | ## Configuring Your Public URL in the Discord Developer Portal |
| 197 | |
| 198 | Finally, to enable your Discord bot to receive interactions such as slash commands, you need to update Discord with the URL you set up above. |
| 199 | |
| 200 | This involves configuring the **Interactions Endpoint URL** in your Discord application's settings. |
| 201 | |
| 202 | ### Steps to Configure the Interactions Endpoint URL |
| 203 | |
| 204 | 1. **Access the Discord Developer Portal**: |
| 205 | - Navigate to the [Discord Developer Portal](https://discord.com/developers/applications). |
| 206 | - Log in with your Discord account. |
| 207 | |
| 208 | 2. **Select Your Application**: |
| 209 | - In the **Applications** section, click on your bot's application to open its settings. |
| 210 | |
| 211 | 3. **Navigate to the "General Information" Tab**: |
| 212 | - On the left sidebar, select **General Information**. |
| 213 | |
| 214 | 4. **Set the Interactions Endpoint URL**: |
| 215 | - Locate the **Interactions Endpoint URL** field. |
| 216 | - Enter your public URL (e.g., `https://example.com/interactions`). |
| 217 | - Ensure this URL points to the endpoint you've set up to handle Discord's interaction payloads. |
| 218 | - Take note of the **public key** for use in your bot |
| 219 | |
| 220 | 5. **Save Changes**: |
| 221 | - Scroll to the bottom of the page and click **Save Changes**. |
| 222 | |
| 223 | \image html webhook-app.png |
| 224 | |
| 225 | ### Important Considerations |
| 226 | |
| 227 | - **Public Accessibility**: Ensure that the URL you provide is publicly accessible over the internet. Discord needs to reach this endpoint to send interaction events. |
| 228 | |
| 229 | - **SSL/TLS Encryption**: As outlined above in the section on setting up your reverse proxy, the URL must use HTTPS, meaning you need a valid SSL/TLS certificate for your domain. If you followed the guide properly, this should be working. |
| 230 | |
| 231 | - **Endpoint Validation**: Upon setting the Interactions Endpoint URL, Discord will send a `PING` request to verify the endpoint. D++ will ensure your bot replies appropriately. |
| 232 | |
| 233 | You can find even more information on Discord's official documentation on [Setting Up an Interaction Endpoint](https://discord.com/developers/docs/getting-started#setting-up-an-interaction-endpoint). |
| 234 | |
| 235 | ## Further reading/Related pages |
| 236 | |
| 237 | * dpp::cluster::enable_webhook_server |
| 238 | * dpp::discord_webhook_server |
| 239 | * dpp::http_server |
| 240 | * dpp::http_server_request |
| 241 | * dpp::socket_listener |
| 242 |