Run a Discord Bot 24/7 as a Windows Service on Your VPS

Keep a discord.js or discord.py bot online around the clock on a Windows VPS: protect the token, run it as a service, restart it on crashes, and keep logs.

dns WindowsVPS Team calendar_today schedule5 min read
Run a Discord Bot 24/7 as a Windows Service on Your VPS
tocOn this page 7 sections
  1. Why a VPS instead of your own PC
  2. Step 1: Prepare the bot in the Developer Portal
  3. Step 2: Install the runtime and copy the bot
  4. Step 3: Keep the token out of the code
  5. Step 4: Run the bot as a service with NSSM
  6. Step 5: Check health and ship updates
  7. FAQ

To host a Discord bot 24/7 on a Windows VPS, install the bot's runtime (Node.js or Python), keep the bot token out of your code, and register the bot as a Windows service that starts at boot and restarts itself when it crashes. Your bot then stays online after you close the Remote Desktop window and after Windows Update reboots the server. The bot only makes outbound connections to Discord, so you do not need to open any inbound firewall ports.

Why a VPS instead of your own PC

A Discord bot holds a long-lived connection to Discord's gateway. If that connection drops, the bot goes offline until something reconnects it. A home PC sleeps, reboots for updates, and loses its connection when the router restarts. A VPS stays powered on in a data center, and a service manager brings the bot back automatically. A single small or medium bot uses very little CPU and memory, so an entry-level plan is usually enough. Memory use grows with the number of servers the bot joins and with how much it caches.

Step 1: Prepare the bot in the Developer Portal

In the Discord Developer Portal, open your application, go to the Bot section, and copy the token. Treat the token like a password. Anyone who has it can control your bot. If the token ever appears in a screenshot, a Git commit, or a pasted log, reset it in the portal right away.

Turn on only the privileged gateway intents your code uses, such as Message Content or Server Members. If your code requests an intent that is disabled in the portal, the connection is refused. That is one of the most common reasons a bot that worked on a laptop fails on a server.

Step 2: Install the runtime and copy the bot

Connect to the VPS over RDP and open PowerShell as Administrator. Install the long-term-support release of Node.js for discord.js bots, or Python for discord.py bots. Use the official installers, or winget if your Windows Server build includes it. Then create a dedicated folder and install the dependencies there:

New-Item -ItemType Directory C:\bots\mybot
cd C:\bots\mybot
# Node.js bot
npm ci
# or Python bot
py -m venv .venv
.\.venv\Scripts\pip install -r requirements.txt

Run the bot once by hand, for example node index.js or .\.venv\Scripts\python bot.py. Confirm that it logs in and answers a command before you turn it into a service. Startup errors are much easier to read in a console than in a log file.

Step 3: Keep the token out of the code

Have the bot read its token from an environment variable such as DISCORD_TOKEN. Do not hard-code it. If you use a .env file, restrict who can read it so that only Administrators and the account the bot runs as have access:

icacls C:\bots\mybot\.env /inheritance:r /grant:r "Administrators:F" ".\botsvc:R"

Add .env to .gitignore so a git push never uploads it.

Step 4: Run the bot as a service with NSSM

Windows cannot run node.exe or python.exe as a service directly. NSSM (the Non-Sucking Service Manager) wraps them as services. If you have not used NSSM before, our walkthrough on running a Python bot as a Windows service with NSSM covers installing it. The Discord-specific setup looks like this:

nssm install DiscordBot "C:\Program Files\nodejs\node.exe" index.js
nssm set DiscordBot AppDirectory C:\bots\mybot
nssm set DiscordBot AppStdout C:\bots\mybot\logs\bot.log
nssm set DiscordBot AppStderr C:\bots\mybot\logs\bot.log
nssm set DiscordBot AppRotateFiles 1
nssm set DiscordBot AppRotateBytes 10485760
nssm set DiscordBot AppExit Default Restart
nssm set DiscordBot AppRestartDelay 5000
nssm set DiscordBot Start SERVICE_AUTO_START
nssm start DiscordBot

Create the logs folder before you start the service. For a Python bot, point the service at C:\bots\mybot\.venv\Scripts\python.exe and pass bot.py as the argument. The restart delay matters: if the bot crashes in a tight loop, it would log in to Discord over and over, and Discord rate-limits repeated logins.

Use a low-privilege account

By default the service runs as LocalSystem, which has far more rights than a chat bot needs. Create a standard local user and run the service under it:

nssm set DiscordBot ObjectName .\botsvc "StrongPasswordHere"

Give that user modify rights on the bot folder only. If a library the bot depends on is ever compromised, the damage stays inside that folder.

Step 5: Check health and ship updates

  • Status: Get-Service DiscordBot shows whether the service is running. Get-Content C:\bots\mybot\logs\bot.log -Tail 50 -Wait follows the log live.
  • Updates: stop the service, pull the new code, reinstall dependencies, then start the service again. If you update while the bot runs, it can load a mix of old and new modules.
  • Reboots: because the service starts automatically, the bot comes back by itself after patching. Check the log after each Patch Tuesday reboot to confirm.
  • Security: the bot needs no inbound ports, so keep RDP locked down and leave everything else closed. Our Windows Server VPS hardening checklist walks through the rest.

One service per bot keeps logs and restarts separate, so several bots can share one server. For more ideas on running unattended workloads, see our overview of a Windows VPS for automation and bots.

FAQ

Do I need to open a port in Windows Firewall for my Discord bot?

Usually not. A gateway bot connects outbound to Discord. You only need an inbound port if you set an HTTP interactions endpoint URL in the Developer Portal or run a web dashboard next to the bot.

Will the bot stop when I disconnect from RDP?

A bot started in a console window can stop when you sign out. A bot running as a Windows service does not depend on any user session, so closing RDP or signing out has no effect on it.

Can I run the same bot on two servers for redundancy?

Not with the same token at the same time. Both copies would receive every event and reply twice. Keep one active instance, and rely on automatic restarts and good backups for recovery.

Run this on your own Windows VPS

Get a Windows VPS from WindowsVPS in about 60 seconds, with full Administrator access over RDP, NVMe storage and the Windows license included.

Buy a Windows VPS →

dns

Written by the WindowsVPS Team

We run Windows Server VPS hosting and write these guides from the same setups our customers use: RDP, PowerShell, trading terminals and always-on bots. Questions about a guide? Contact us.

About WindowsVPS Windows VPS guides All articles

Ready to deploy your server?

Windows or Linux VPS — up and running in under 60 seconds.