全部文章
DiscordGuide

在 VPS 上 24/7 托管 Discord 机器人

发布于 2025年4月22日 · 阅读约 7 分钟

本文提供法语和英语版本。

Your bot deserves better than your home router

The classic cycle: the Discord bot runs on a personal PC, then on a Raspberry Pi behind the home router, then — after the third outage in the middle of a community event — on a VPS. Might as well skip the steps: here's the reference setup.

Why the VPS wins

A Raspberry Pi at home stacks three handicaps: an unguaranteed connection (NAT, dynamic IP, outages), an SD card that dies silently, and a dependency on your physical presence. A VPS brings a fixed IP, measured availability (99.99% observed across our fleet in 2024), and stable latency to Discord's gateways — under 15 ms from our European datacenters.

systemd, not screen

The number one mistake of self-hosted bots: a screen or nohup that survives neither crash nor reboot. A systemd unit handles both:

[Unit]
Description=Discord Bot
After=network-online.target

[Service]
WorkingDirectory=/opt/mybot
ExecStart=/usr/bin/node index.js
Restart=always
RestartSec=5
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Enable it with systemctl enable --now mybot. From now on: crash → relaunch in 5 seconds; reboot → automatic startup; logs → journalctl -u mybot -f.

Clean restarts

Discord enforces periodic gateway reconnections. Handle them in code (session resume) rather than suffering hard disconnects. And treat bot updates like deployments: git pull, restart, log check — never hand-edit in production.

The real cost, do the math

A reasonable bot (a few thousand servers) fits in 150 to 400 MB of RAM and crumbs of CPU:

OptionReal monthly cost
Raspberry Pi 4 (power + SD wear)~€6 + your time
PC running 24/7€15–25 of electricity
Apex VPSthe price of a coffee per month

The Apex is cut for this: a Ryzen 9 at 5.7 GHz for a single-process bot, deployment in 55 seconds, and support answering in a 12-minute median when a Sunday evening goes wrong.

Mistakes that cost dearly

  • The token in the git repo: regenerate it immediately if that happened — it's already in leak databases.
  • Undeclared intents: the bot works in dev, silent in production.
  • Ignoring rate limiting: Discord bans chatty IPs; respect your library's request queues.
  • No alerting: a 5-minute monitoring ping against the bot's status is free, and it helps you sleep better.