Discord bots
Discord Bots
A Discord Bot service type is built for a process that connects to Discord and stays listening. It needs no HTTP port or domain.
Creating a bot
- In your project, click + New service and choose the Discord Bot type.
- Select the runtime: JavaScript (Node.js) or Python.
- Connect the GitHub repository containing the bot's code.
- Add your token to the environment variables (never in the code):
DISCORD_TOKEN=votre-token-de-bot
Minimal example (discord.js)
import { Client, GatewayIntentBits } from "discord.js";
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once("ready", () => console.log(`Connecté : ${client.user.tag}`));
client.login(process.env.DISCORD_TOKEN);
Minimal example (discord.py)
import os, discord
client = discord.Client(intents=discord.Intents.default())
@client.event
async def on_ready():
print(f"Connecté : {client.user}")
client.run(os.environ["DISCORD_TOKEN"])
Shard mode
Beyond 2,500 servers, Discord requires splitting the bot's connection into shards. Turn on shard mode when creating the bot, or later in the Discord Bot tab, then pick the number of shards (2 to 32). The panel shows the number Discord recommends for your bot, roughly one shard per 1,000 servers.
Each shard runs in its own pod: the load is spread over several machines, and a shard that crashes doesn't take the others down. Pods start one after the other, a few seconds apart, to stay within Discord's identify limit. Each pod gets three variables:
| Variable | Content |
|---|---|
SHARD_ID | This pod's shard number (0, 1, 2…) |
SHARD_COUNT | Total number of shards |
SHARDS | [SHARD_ID], in the format discord.js expects |
discord.js reads SHARDS and SHARD_COUNT on its own: the minimal example above works unchanged. Don't add a ShardingManager on top, the platform already plays that role.
discord.py: pass both variables to the client.
import os, discord
from discord.ext import commands
bot = commands.AutoShardedBot(
command_prefix="!",
intents=discord.Intents.default(),
shard_ids=[int(os.environ["SHARD_ID"])],
shard_count=int(os.environ["SHARD_COUNT"]),
)
bot.run(os.environ["DISCORD_TOKEN"])
Changing the number of shards restarts every pod of the bot, since each one must know the new total. In the Logs tab, a selector picks the shard whose logs you read. Autoscaling isn't available for a sharded bot: it always runs one pod per shard.
Connection status
The service panel shows the bot's Discord connection status in addition to the container's status: you can immediately see whether the bot is online on Discord's side, not just whether the process is running.
Points to watch
- No port to configure: a bot doesn't expose an HTTP server. If your code still opens a port (built-in dashboard…), prefer a regular web service instead.
- The process must stay alive: a script that terminates causes a restart loop. The diagnosis is the same as for start commands.
- The bot's logs are available in real time in the Logs tab.