Skip to content
Black Friday · −50 % off your first invoice on Xeon VPS with code BF50XEON, −30 % off our other services with BF30 · domain names and dedicated servers excluded See our plans
CloudFive
Log in

01

Why a bot stops when your computer turns off

A Discord bot is a program that stays permanently connected to Discord's servers. As long as it runs, it receives messages and commands and responds to them. As soon as the program stops (computer asleep, ISP box down, window closed), the bot shows as offline.

For a bot available day and night, you therefore need a machine that is always on:

  • your own computer: free, but it must stay on and connected, and the bot stops at every restart;
  • hosting made for bots: you upload your code and it runs continuously, with no system to administer;
  • a VPS: a whole machine, with root access, for several bots, a database, a website or Docker.

Good news: a standard bot opens its own connection to Discord. It has no ports to open on the machine, unlike a game server or a website.

02

Creating the application and inviting the bot

  1. On the Discord Developer Portal, create an application (New Application).
  2. In the Bot tab, choose a name and an avatar, then generate the token (Reset Token). Copy it straight away: Discord only shows it once.
  3. In OAuth2, generate the invite link with the bot and applications.commands scopes (for slash commands).
  4. Tick only the permissions the bot needs. Avoid Administrator: if the token leaks, someone would have full rights over your servers.

On the code side, the two most widely used libraries are discord.js (JavaScript, on Node.js) and discord.py (Python). Pick the one for the language you know: both cover slash commands, buttons, menus and input forms.

03

Intents: what the bot is allowed to receive

When it connects, the bot declares its intents, i.e. the families of events it wants to receive: servers, messages, reactions, members… It receives nothing else.

Three intents are called privileged and must also be enabled in the portal's Bot tab:

  • Message Content: reading the text of messages, useful for old prefix commands (!aide). Slash commands do not need it;
  • Server Members: arrivals, departures and the member list;
  • Presence: members' statuses and activities.

An intent requested in the code but not ticked in the portal prevents the bot from connecting: it is the most common error at first start-up. Only request what you need: beyond 100 servers, Discord requires the bot to be verified and each privileged intent to be justified.

04

The token: a password you must never publish

The token is enough to take control of the bot: sending messages, banning members, deleting channels, depending on its permissions. It must never appear in the code, in a screenshot or in a Git repository, even a private one.

Store it in an environment variable, often read from a .env file placed next to the code:

  • in the .env file: DISCORD_TOKEN=votre_jeton;
  • in Node.js: process.env.DISCORD_TOKEN, with the dotenv package or the --env-file option of recent Node.js versions;
  • in Python: os.environ["DISCORD_TOKEN"], with the python-dotenv package.

Add .env to the .gitignore file before the first push to Git. Store the other secrets in the same place: database password, API keys.

If the token has leaked, regenerate it immediately in the portal (Reset Token): the old one stops working at once.

05

Staying online: automatic restart and logs

The libraries reconnect on their own after a network outage. However, if the program crashes (unhandled error, memory exhausted), something is needed to restart it:

  • on a Linux VPS, a systemd service with Restart=always restarts the bot after a crash and when the machine boots;
  • for Node.js, pm2 does the same (pm2 start index.js --name bot, then pm2 startup and pm2 save);
  • in the code, catch command errors: a command that fails should reply “error”, not stop the whole bot.

Logs tell you what happened before a crash. Write the date, the command and the full error to them, never the token. On a VPS, journalctl or pm2 logs display them; limit their size so that they do not fill up the disk.

06

Where to store the bot's data

Levels, warnings, per-server settings: as soon as the bot needs to remember something, it needs storage.

  • A JSON file works for a few settings, but it easily gets corrupted if two writes happen at the same time or if the bot stops while saving.
  • SQLite is a real database in a single file, with no server to install: the right starting point.
  • MariaDB, MySQL or PostgreSQL become useful when several programs share the data (a bot and its website, for example) or when the volume grows.

Whatever you choose, back up the database regularly and keep a copy somewhere other than the bot's machine.

07

Updates, security and growth

  • Update the library: Discord keeps changing its API, and a version of discord.js or discord.py that is too old will eventually stop working. Read the release notes before a major update.
  • Pin versions: package-lock.json in Node.js, a requirements.txt with version numbers in Python. The bot can then be reinstalled identically on another machine.
  • Check permissions in the code: a moderation command must check that the person calling it is allowed to use it.
  • Respect rate limits: the libraries handle them, but a loop that sends bursts of messages slows the bot down and can get it blocked.
  • Beyond 2,500 servers, Discord requires sharding: the bot opens several connections. discord.js and discord.py both handle it.
08

Hosting your bot at CloudFive

Node.js and Python hosting is made for a bot, an API or a script that runs continuously, without administering a whole VPS:

  • Node.js or Python, chosen when ordering, same tiers and same prices; switching from one to the other only takes a ticket;
  • Ryzen 9 9950X processor and NVMe storage, in France, with basic protection included;
  • online in 30 seconds, no commitment, and the bot's console can be read from the customer area;
  • the Developer tier is enough for a bot serving a few Discord servers; Advanced adds an included database, Expert two databases and a backup.

For several bots with their website or tools, a Ryzen VPS gives you root access and a dedicated IP address; the VPS Start is enough for a few Discord bots. For Docker, choose the KVM VPS. The web hosting or VPS guide helps you decide, and securing a Linux VPS covers the first settings.

09

Frequently asked questions

How can I host a Discord bot for free?

The simplest way is to run it on your computer, but it stops as soon as the computer turns off or goes to sleep. For a bot available day and night, you need a machine that is always on: bot hosting or a VPS.

Do I need to open a port for a Discord bot?

No. A standard bot opens its own connection to Discord: no inbound port is needed. Only a website or a webhook endpoint, if there is one alongside the bot, requires an open port.

discord.js or discord.py?

Pick the library for the language you know. discord.js works with Node.js, discord.py with Python; both handle slash commands, buttons and menus.

What should I do if my bot's token has leaked?

Regenerate it immediately in the Bot tab of the Discord Developer Portal: the old token stops working. Then put the new one in your environment variable and check that the .env file is not tracked by Git.

My bot refuses to connect because of intents. Why?

A privileged intent (Message Content, Server Members or Presence) is requested in the code without being enabled in the portal's Bot tab. Tick it in the portal, or remove it from the code if it is not used.

Support CloudFive AI

An answer right away, a real team behind it.

CloudFive AI answers you at any hour, in seconds. When it doesn’t know, or as soon as you ask, the team that runs the machines takes over. No offshore call centre.

  • TicketThe team replies in under 2 hours, 7 days a week.
  • CloudFive AIAt any hour, on ia.cloudfive.fr, Discord and in your customer area.
  • DiscordThe team, the AI and other customers, in the help channel.
  • Your data stays with usAI runs on our servers in France; e-mails, IPs and passwords are masked.
A ticket in the customer area: a customer’s question about a Minecraft plugin, and the CloudFive team’s reply
A real ticket in the customer area, answered by the team (demo account; shown in French).
CUSTOMER AREA · APP

Your servers.
At your fingertips.

Restart a server, watch its resources live, pay an invoice or open a ticket — and get an alert when something goes wrong.

  • The status of all your services at a glance
  • Blocked attacks, filtered traffic, monthly peak
  • A notification when a server goes down or an invoice is due

App coming soon · in the meantime, everything works in your browser at manager.cloudfive.fr

The CloudFive app: the list of your services, with their status
The CloudFive app: the home screen, your services’ status, blocked attacks and an invoice to pay