Hosting Your Pycord Bot
If you're completely new to this, you might have thought something along the lines of "Yay! I got my bot working," after following our Creating your First Bot guide, only to close your IDE or shut down your computer to find your bot offline.
The reason for this is that programs have to be hosted somewhere, meaning some machine somewhere has to constantly run your bot. There are tons of hosting services that can help you host your bot for little to no cost.
Can I get a Clear Explanation?
Sure thing. When you run your bot, it first makes a connection to Discord's API. Once that's done, Pycord sends "heartbeats" to Discord. "heartbeats" are small packets sent at a set interval telling Discord that your bot is indeed still alive. If Discord doesn't receive a heartbeat after a certain amount of time, your bot is pronounced dead and buried in the graveyard (not really). It is, though, taken offline and its connection with Discord is terminated.
Once you close the terminal that you've run your program on, the program is no longer running, and the bot stops sending heartbeats and can no longer process commands. This is why you have to constantly keep the process running.
This goes for all programs, in a different nature. If the code isn't compiled/built/assembled/interpreted, it can't be running.
What is a Host?
A host is a server or container for running code offered by a hosting provider. There are a lot of hosting providers, such as bigger ones like Amazon Web Services (AWS) and Google Cloud, and smaller ones like GalaxyGate and DigitalOcean.
There are three types of hosts.
- Shared Hosting
- VPS
- Dedicated
Make sure you choose a hosting provider you trust. Also make sure the hosting provider you're looking at has good reviews on public forums. Googling along the lines of "[host] review" should do the trick. A provider you don't trust can compromise your token.
Most hosting providers rent their services to you for a monthly or yearly fee. These can be anywhere from a few dollars to hundreds or thousands of dollars.
How to Host Your Bot
Once you rent or buy a VPS, you can get to work. Most hosting providers allow you to choose from a wide range of operating systems, most allow you to choose one but some allow you to choose multiple. Once that's ready, you have to SSH into the system. We won't get into SSH here, but you can read this article from DigitalOcean. You can also use VNC, which is remote desktop software. If you are using an OS that does not have a GUI, and is only a command line (a "server" OS), such as Ubuntu Server or most Linux Server Operating Servers, you will most likely use SSH. If you are using an OS that has a GUI, such as Windows Server, you will most likely use VNC.
Once you've decided on your operating system, you'll want to set it up and install Python. Once that's done, you can copy your bot's files to your remote system, install the required packages, and run the bot.
If you are using SSH to run the file, your bot will not stay running. This is because the file is only told to run during your session. You can use a command like nohup to make sure your file stays running after you disconnect.
Now your bot is hosted, but that doesn't mean it can't go offline. If your bot encounters an error, it can often go offline or stop its process. For this, make sure you have proper error handling. Also be sure to pay attention to your hosting provider's updates or news, as they usually notify users of when they plan on doing maintenance to their servers, making their services unavailable for a short time.