-
Notifications
You must be signed in to change notification settings - Fork 3
How to host it youself
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.
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.
Each bot has its own Discord account, which you can create in the Developer Portal.
- Click the
New Application
button at the top right corner. - Enter
Name
for your bot, read ToS and Developer Policy, tick the box and clickCreate
. - You will be redirected to the
General Information
tab of your new bot. Configure Icon, Description and Tags however you like. ClickSave Changes
in the popup at the bottom after you're done. - Find the
APPLICATION ID
segment (below theTAGS
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 theCopy
button. This is yourAPPLICATION_ID
environment variable. - Go to the
Bot
page on the sidebar at the left side of the page. - Set the bot's
Username
. - Click
Reset Token
, thenYes, do it!
. - 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 yourTOKEN
environment variable. - 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. - Go to the
OAuth2
page on the sidebar at the left side of the page. - Scroll down to the
OAuth2 URL Generator
. - In the
SCOPES
segment, select:- bot
- applications.commands
- In the
BOT PERMISSIONS
segment, select:- Manage Roles
- Manage Channels
- Read Messages/View Channels
- Send Messages
- Manage Messages
- Manage Threads
- Embed Links
- Attach Files
- Read Message History
- Mention Everyone
- 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).
- Keep
INTEGRATION TYPE
asGuild Install
. - Copy
GENERATED URL
. Use it to invite the bot to a server.
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.
- Go to the MongoDB page
- Click the
Try Free
button at the top of the screen. - Create your account (use your Google account or your email address).
- 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.
- 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 theOverview
screen. - Select
Shared
cluster. - 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.
- Make sure
Cluster Tier
is selected to beM0 Sandbox
. Otherwise, MongoDB will charge you for the additional resources you've requested. - You should return to the
Overview
page. There, you should see your newly created cluster. - Click
Browse Collections
. - Click
CreateDatabase
. The name of the database you created is yourMONGO_DB
environment variable. - Go back to the
Overview
page. - Click the
Connect
button below your cluster's name. - Click
Drivers
- Select
Node.js
Driver and5.5 or later
Version. - 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.
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).
- Download and install Node.js.
- Go to the bot repository main page.
- Click
Code
->Download ZIP
(or clone it using git, if you know how to do it) - If you downloaded the archive - unpack it into a folder and go into it.
- Create a copy of the
.env.sample
file and rename it into the.env
. - Fill the file with the environment variables you gathered in the previous steps of the guide.
- Click in the address bar of your File Explorer, and type
cmd
. - In the opened terminal window, type
npm install
. - After the process is completed, type
npm start
. - Your bot should be up and running within a couple of seconds. Just keep the console open.
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.
- Create an account at the fly.io.
- Install flyctl. This is a command-line utility developed by
fly.io
to host your app in their cloud. - Open the terminal in the folder with the bot.
- Type
flyctl auth login
. Log into your account using your login/password. - Type
flyctl launch
- The process will ask several questions from you:
-
Choose an app name (leave blank to generate one):
- pressEnter
-
Choose a region for deployment:
- pick one of supported regions. For Europe,ams
orfra
is a solid choice (in my case, with DB long established in Frankfurt, it'd be the best to usefra
, but I deployed to theams
for some reason smh) -
Would you like to set up a Postgresql database now?
- typeNo
-
Would you like to set up an Upstash Redis database now?
- typeNo
-
Would you like to deploy now?
- typeYes
-
- Once you see
v1 deployed successfully
in the terminal, go to the dashboard. - Click on your new app.
- On the side to the left, click
Secrets
. - By clicking
New Secret +
, add environment variables you've gathered throughout this guide. - Restart the bot to apply new environment variables.
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:
- Open the terminal in the folder with the bot.
- Type
flyctl scale count 0 -y
. - Once the process is completed, type
flyctl scale count 1 -y
.
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!
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.
- Enable Developer Mode
- Go to
Settings
->Advanced
- Toggle
Developer Mode
.
- Go to
- Find Server ID
- Right-click the server icon.
- Click
Copy Server ID
at the bottom of the context menu.