Guide

How to keep a Discord bot online 24/7

A bot is only online while a program is running and connected to Discord. Keeping it online means two things: somewhere that never switches off, and code that does not fall over.

← All guides3 min readUpdated

Why bots go offline

A Discord bot is not a website that Discord calls when it needs something. It is a program that holds a connection open to Discord for as long as it runs. The green dot next to its name means that connection is up; the moment the program stops, the bot goes grey.

So a bot goes offline for one of three reasons. The machine running it switched off or went to sleep — your laptop lid closed, your PC restarted for an update. The platform running it stopped it — many free hosts pause a project nobody is visiting, and a bot has no visitors. Or the program itself crashed, on an error nothing caught.

The first two are about where the bot runs. The third is about the code, and no host fixes it for you.

Stop it crashing

Both big libraries reconnect by themselves when Discord drops the connection, which it does routinely. What takes a bot down is an error in your own code that nothing handles: a command that throws, a promise that rejects with no catch, a failed request to another API.

Handle errors where they happen — around each command — and add a last line of defence that logs anything that slips through instead of letting it end the process. Logging is the important half: a bot that silently survives an error you never hear about will fail the same way tomorrow.

discord.js

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isChatInputCommand()) return;

    try {
        await commands.get(interaction.commandName)?.execute(interaction);
    } catch (error) {
        console.error(`/${interaction.commandName} failed:`, error);
        const reply = { content: 'Something went wrong.', ephemeral: true };
        if (interaction.replied || interaction.deferred) await interaction.followUp(reply);
        else await interaction.reply(reply);
    }
});

// The last line of defence: log it rather than crash.
process.on('unhandledRejection', (error) => console.error('Unhandled rejection:', error));
client.on('error', (error) => console.error('Client error:', error));

discord.py

@bot.tree.error
async def on_app_command_error(interaction, error):
    print(f"/{interaction.command.name if interaction.command else '?'} failed: {error!r}")
    send = interaction.followup.send if interaction.response.is_done() else interaction.response.send_message
    await send("Something went wrong.", ephemeral=True)

Somewhere that never switches off

There are three realistic places to run a bot all the time.

A machine you own. An old laptop or a Raspberry Pi left switched on works, and costs only electricity. The catch is that it depends on your power and your internet, and you are the one who notices when it goes down.

A VPS you manage. A small virtual server gives you a Linux machine in a data centre that never sleeps. You install the runtime, copy the bot across, and run it under a process manager that restarts it if it crashes and starts it again after a reboot. pm2 is the usual choice for Node.js; systemd, which every Linux server already has, works for anything.

A host built for bots. You upload the code and the host does the rest: installs dependencies, keeps it running, restarts it after a crash. This is what we do, for free, and it is described at the end of this guide.

pm2 on a VPS

npm install -g pm2
pm2 start index.js --name my-bot

# Start it again automatically after the server reboots.
pm2 save
pm2 startup

Avoid the keep-alive trick

Many older tutorials add a small web server to the bot and point an uptime-monitoring service at it every few minutes, so that a host that sleeps idle projects thinks the project is busy. It works until it does not: several hosts forbid it outright and stop projects that do it, and it keeps a web server running for no reason but to fool one.

A host that does not sleep your bot in the first place makes the trick unnecessary. If you are moving a bot that has one, it is safe to delete — see the guide to moving off Replit or Glitch.

Running it on our hosting

Our free servers do not sleep and have no hours cap, and a bot that crashes is restarted automatically. Upload your project without node_modules or a virtual environment, name your entry file, put your token in an environment variable, and press start; your package.json or requirements.txt is installed on deploy.

Each free server has 25% of a CPU core, 256 MB of memory and 512 MB of disk, which runs a typical bot in a few hundred servers comfortably. The live console shows your bot's output, which is where to look first when something goes wrong.

Automatic restarts are a safety net, not a fix. A bot that crashes on every start will be restarted into the same crash; the console shows the error, and the error reference explains most of them.

FAQ

Questions

Do I need an uptime monitor for a Discord bot?

Not to keep it running. A Discord bot receives nothing over HTTP, so pinging it does nothing unless the host sleeps idle projects. A monitor that alerts you when the bot goes offline is still useful — that is a different job.

My bot goes offline every few days on its own.

Look at the output just before it stopped. The two usual causes are running out of memory, which shows as a heap or "Killed" error, and an error nothing caught. Both have pages in the error reference.

Can I run my bot on my phone?

Technically, with apps like Termux on Android, but phones stop background programs to save battery, so it will not stay up. It is fine for experimenting and not for a bot other people rely on.