DEV Community

Colum Kelly
Colum Kelly

Posted on

Create and Deploy a Discord Bot for Free with Fly.io

Introduction

A Discord Bot needs to be able to receive requests at all times. Unfortunately, it can be tricky to get a server up and running for free. Many of the existing free tiers involve frequent shut downs and lengthy spin up times. Going serverless is an option but it comes with its own unique challenges.

Thankfully, Fly.io offers a trial which includes $5 free credit. This is enough to run a small full-stack app, full-time, for one month. It is more than enough to run a Discord bot. I'll go through the steps required to get it up and running.

(If you are lucky to have made an account on Fly last year, their Legacy Hobby plan includes a $5 free allowance each month, making it free to run forever)

Step 1 - Create your Discord Bot

I recommend using a library like Discord.js for this. It will handle the gateway connection, caching users, channels etc. for you. You can follow this guide to get a simple bot running locally within a few minutes.

When you get to the configuration section, don't create a config.json. Instead, create a .env file and add your token, client ID and guild ID. You will copy these over to the server later.

Continue along with the guide and don't forget to add your bot to the server using an invite link.

When you have finished creating the bot, run the deploy-commands.js file to register your slash commands with the server.

Step 2 - Create an Account on Fly.io

Sign up for a Hobby account and add your payment information. You won't be charged until the $5 of trial credit has been used up.

Step 3 - Fly Launch Configuration

Create a file called fly.toml in the root of your project directory and add the text from the snippet below. (Check here for a list of available regions)

app = '<YOUR_APP_NAME_HERE>'
primary_region = '<YOUR_REGION_HERE>'

[build]

[[services]]
  internal_port = 3000
  protocol = "tcp"

  # Allow machines to run continuously
  auto_start_machines = false
  auto_stop_machines = false

Enter fullscreen mode Exit fullscreen mode

Step 3 - Launch the Application

Follow the instructions here to install the Fly command line utility. When it is installed, run the command fly launch from the source directory of your project.

Step 4 - Add Environment Variables

You should now see your app on the Fly.io dashboard. Go to the Secrets page and add your environment variables. When you are finished, the app will automatically redeploy with the new variables.

Now, navigate to the Live Log page. If you followed the starter bot example in the Discord.js docs, you should see the text "Ready! Logged in as " in the console.

If so, congratulations! Your bot is up and running. You should now be able to run commands in your Discord server.

Top comments (1)

Collapse
 
katelovescode profile image
Kate Donaldson

Thank you so much for this, it's the first place I've seen that mentions that you need to set the autostart and autostop to false. My bot was shutting down and I couldn't figure out why.