Skip to content

How to host it youself

Andre Tkachenko edited this page Jun 18, 2024 · 2 revisions

As of July 6th, 2024, my Discord bot will lose its verification. To renew it, Discord demands some sensitive information about my identity. With the recent news about Discord appearing on the Internet, I don't feel like sharing such information with them. As such, my bots will turn unverified, thus unable to be used by more than 100 servers. The Discord team doesn't provide a clear explanation of what will happen to the bots which have lost the verification badge. But from what I've gathered, those who already added the bot will be able to keep it, but you won't be able to add the bot to the new servers.
It remains to be seen if this assumption is correct, or if the process has changed. The bot might go dark completely.

In any case, I decided it'd be fair to provide a guide on how you can host the bot yourself for free to use for yourself.

Variables

To link database, bot account and bot hosting, we use so-called environment variables. These are sets of names and associated values, which allow code to use the different configurations instead of being tightly bound to one database and bot account I've personally configured. This also allows you to use the bot without changing its code in the slightest.
Names of each environment variable should be exact, otherwise the code won't be able to match it. Here's the list of environment variables required for the bot to work:

  • TEST_SERVER
  • MONGO_NAME
  • MONGO_PWD
  • MONGO_CLUSTER
  • MONGO_DB
  • IMG
  • APPLICATION_ID
  • TOKEN

Make sure to keep these values in the text editor of your choice to be able to quickly add them to the bot hosting instead of gathering them at the latest point.
For each environment variable, except for IMG, I will provide steps on how to get them in the guide.
For the IMG - it's the URL to the image you want to use as a bot's avatar.

Create a Discord Bot account

Each bot has its own Discord account, which you can create in the Developer Portal.

  1. Click the New Application button at the top right corner.
  2. Enter Name for your bot, read ToS and Developer Policy, tick the box and click Create.
  3. You will be redirected to the General Information tab of your new bot. Configure Icon, Description and Tags however you like. Click Save Changes in the popup at the bottom after you're done.
  4. Find the APPLICATION ID segment (below the TAGS section). You'll need this ID to log in the bot into the account later on. You'll be able to get it by clicking the Copy button. This is your APPLICATION_ID environment variable.
  5. Go to the Bot page on the sidebar at the left side of the page.
  6. Set the bot's Username.
  7. Click Reset Token, then Yes, do it!.
  8. Copy the token by clicking the Copy button and save it on your computer. The token will be shown to you only once, so make sure to keep it where you can find it, otherwise, you'll have to regenerate it again. This is your TOKEN environment variable.
  9. You can also make your bot private (only you can add it to a server) by unchecking the PUBLIC BOT flag below. The advantage of having a private bot is that your bot will use fewer resources to handle requests, making it easier to find free hosting able to keep the bot alive without requiring a paid plan. But it's up to you.
  10. Go to the OAuth2 page on the sidebar at the left side of the page.
  11. Scroll down to the OAuth2 URL Generator.
  12. In the SCOPES segment, select:
    1. bot
    2. applications.commands
  13. In the BOT PERMISSIONS segment, select:
    1. Manage Roles
    2. Manage Channels
    3. Read Messages/View Channels
    4. Send Messages
    5. Manage Messages
    6. Manage Threads
    7. Embed Links
    8. Attach Files
    9. Read Message History
    10. Mention Everyone
    11. Use Slash Commands Discord like to change it from time to time, so see for yourself: if you use the bot for yourself, you can just give it Administrator permissions and forget about this part (if you trust the bot, of course).
  14. Keep INTEGRATION TYPE as Guild Install.
  15. Copy GENERATED URL. Use it to invite the bot to a server.

Set up database

The bot uses the MongoDB database to keep information about connections between voice and text channels, ignored roles and other configurations for the server. If you want to host the bot, you'll also need to create the database. Luckily for us, MongoDB provides the MongoDB Atlas cloud solution, which allows you to create it in the cloud for free.

  1. Go to the MongoDB page
  2. Click the Try Free button at the top of the screen.
  3. Create your account (use your Google account or your email address).
  4. You will be prompted to create an organisation and a project. Just fill in the required fields - it doesn't make a big difference, but try to name it properly in case you will have multiple of them in the future and will have to deduce which one is which.
  5. Once you create both organisation and project, make sure they are selected by selectors at the top. Then, click the Create cluster button at the top centre part of the Overview screen.
  6. Select Shared cluster.
  7. Pick one of the cloud providers and a region among one of the provided. This will affect how quick the response will be between the bot and data storage. The best strategy is to keep the bot server and database server as close as possible. In my example, I hosted the bot at the Amsterdam server and the database at the Frankfurt server. Don't stress about it much though - if you keep both in fairly big cities on the same continent, the delay shouldn't be much of a deal.
  8. Make sure Cluster Tier is selected to be M0 Sandbox. Otherwise, MongoDB will charge you for the additional resources you've requested.
  9. You should return to the Overview page. There, you should see your newly created cluster.
  10. Click Browse Collections.
  11. Click CreateDatabase. The name of the database you created is your MONGO_DB environment variable.
  12. Go back to the Overview page.
  13. Click the Connect button below your cluster's name.
  14. Click Drivers
  15. Select Node.js Driver and 5.5 or later Version.
  16. Copy the connection string to the text editor of your choice. It should look like this:
mongodb+srv://admin:<password>@nexuscluster-1.aaaaa.mongodb.net/?retryWrites=true&w=majority&appName=ClusterName-1

We will need to disassemble this string into a couple of environment variables:

  • MONGO_NAME - admin (or any other username you decide to use for a connection);
  • MONGO_PWD - password for this username (without <> brackets; they are added only to show you where the password part starts/ends);
  • MONGO_CLUSTER - nexuscluster-1.aaaaa.mongodb.net/?retryWrites=true&w=majority part from the connection string you received from the MongoDB.

Host the bot

There are a lot of options for how to host your applications, but the amount of hosting which allows you to do that for free is constantly dwindling.
You can host it on your machine - the easiest to set up, but it will require you to keep the machine running all the time.
There are also hosting which do have free tiers, however the application goes to sleep after 5 minutes of inactivity. New calls to it will wake it up but with a significant delay.
In this guide, I will show you how to host it locally and also how to host it at the fly.io - hosting where I keep my own bots at the moment. If fly.io stops providing a free tier - well, you'll have to just search for a new one on your own (or pay up).

Host locally

  1. Download and install Node.js.
  2. Go to the bot repository main page.
  3. Click Code -> Download ZIP (or clone it using git, if you know how to do it)
  4. If you downloaded the archive - unpack it into a folder and go into it.
  5. Create a copy of the .env.sample file and rename it into the .env.
  6. Fill the file with the environment variables you gathered in the previous steps of the guide.
  7. Click in the address bar of your File Explorer, and type cmd.
  8. In the opened terminal window, type npm install.
  9. After the process is completed, type npm start.
  10. Your bot should be up and running within a couple of seconds. Just keep the console open.

Host at the fly.io

The fly.io has a free tier plan. However, you'll still have to give your card details and might be charged 1$ to prove it (this 1$ will be returned back to you). After that - if you use paid resources, you're fine. I've been using the hosting for a few years now, and have been charged only once - but it was due to the changes made to how they allocated IP addresses, and due to poor communication with the users beforehand, they refunded the cost completely. Luckily, you don't care about the dedicated IP for this bot, so you can be safe in this regard with your new application.

  1. Create an account at the fly.io.
  2. Install flyctl. This is a command-line utility developed by fly.io to host your app in their cloud.
  3. Open the terminal in the folder with the bot.
  4. Type flyctl auth login. Log into your account using your login/password.
  5. Type flyctl launch
  6. The process will ask several questions from you:
    1. Choose an app name (leave blank to generate one): - press Enter
    2. Choose a region for deployment: - pick one of supported regions. For Europe, ams or fra is a solid choice (in my case, with DB long established in Frankfurt, it'd be the best to use fra, but I deployed to the ams for some reason smh)
    3. Would you like to set up a Postgresql database now? - type No
    4. Would you like to set up an Upstash Redis database now? - type No
    5. Would you like to deploy now? - type Yes
  7. Once you see v1 deployed successfully in the terminal, go to the dashboard.
  8. Click on your new app.
  9. On the side to the left, click Secrets.
  10. By clicking New Secret +, add environment variables you've gathered throughout this guide.
  11. Restart the bot to apply new environment variables.

To restart the bot

Sometimes you need to change environment variables or your bot crashes for some reason and doesn't restart automatically or for any other reason you need to restart the bot.
To do that:

  1. Open the terminal in the folder with the bot.
  2. Type flyctl scale count 0 -y.
  3. Once the process is completed, type flyctl scale count 1 -y.

Host on other hosting

Don't be afraid to search for another hosting - they are all providing documentation with step-by-step guides on how to set up everything. In all cases, it boils down to:

  • Create an account;
  • Create an application account;
  • Link to the code to host;
  • Add environment variables;
  • Deploy.

I've been using Heroku for the longest time until they've been bought by Salesforce, who quickly closed the free tier. Heroku was incredible for this kind of small app, allowing one to just connect a GitHub account, select a repository and then click Deploy. I also tried Render, Glitch, Railway, Vercel and other hostings before settling with fly.io. Each of them has its own way of configuring deployment, so knowledge of previous attempts didn't give me much advantage over you - so each and every one of us had to go through tutorials, tons of opened tabs and stupid mistakes many times. With enough dedication - so can anyone. Just keep your spirit up!

To check if the bot is alive

Have you already invited the bot to your server? If you didn't, it's time to do it now, using the link we've generated at the end of the Create Discord Bot account section.
Then - check the bot status. It should become green(online). You can also check it using the /ping command in the chat (make sure you use this bot's command - ping is a common name for health checks, so pay attention to the bot's name and avatar before choosing the command.
Note that it takes 5-10 minutes for Discord to establish commands for the bot in the general server's pool of commands. You can make them appear faster on one server by adding the TEST_SERVER environment variable to your hosting. It should contain the ID of the server - this is a feature Discord implemented for developers for quicker testing.

How to get Server ID

  • Enable Developer Mode
    • Go to Settings -> Advanced
    • Toggle Developer Mode.
  • Find Server ID
    • Right-click the server icon.
    • Click Copy Server ID at the bottom of the context menu.