Old people have Facebook, millenials have #instastory, and gamers… well, we have Discord. Since I’ve joined Discord’s community, I noticed the importance of ChatBots in every channel. As I’m learning Ruby in my spare time and I’m always eager to try something new I decided to give it a shot.
Unfortunately, building a sample bot is not as straightforward as I was hoping. Also, there are not a lot of guides available that show you how to not only create a sample bot app but also deploy to “production” (Discord bot is a Rack application).
Luckily, there’s an excellent Discord API community, and quite a few useful pieces of content available online:
- Discordrb gem and its documentation. We’re going to use it to create our bot.
- This awesome, and THE ONLY tutorial on how to create bots with Discordrb I could find: “How to Make a Simple Discord Bot in Ruby to Annoy Your Friends.”
- Deploying Rack-based applications on Heroku. We will use Heroku to host our bot, so a basic understanding of the deploy flow on Heroku is required.
The guide is divided into 2 parts: Quick Setup guide for more advanced developers and a Step-by-Step guide for beginners. Both achieve the same result.
Create Bot Application on Discord
Goals: Familiarize yourself with Discord’s developer dashboard.
- Register an account on Discord (if you haven’t already 😅)
- Visit https://discordapp.com/developers/applications/
- Create an Application and give it a name.
- Go to the
Bottab, and create one by clicking the
- Copy your bot token and save it somewhere — you’re going to need it soon.
- Optional: Set the app to private mode for testing purposes. You can do it in the BOT tab in your application’s view.
Create Test Bot SHORT GUIDE
Goals: Deploy sample bot to Heroku in minutes.
- Create test Discord channel where you’ll be able to invite the bot
bot.rbfile and copy/paste the following example (you can skip bot invite link part using this direct link — select Send Messages permission and provide your application’s ClientID): https://github.com/meew0/discordrb/blob/master/examples/ping.rb
- Provide BOT TOKEN where it’s required. You can use
dotenvtoo. Just remember to require
- Create Gemfile with
- Create Procfile with
worker: bundle exec ruby bot.rb(worker is required due to https://github.com/keystonejs/keystone/issues/3994) I personally came across this issue. You don’t have to though and could probably try using
web: ...as usual.
- Create Heroku app, add ruby buildpack and add your env variables if you’re using
dotenvand push your code to
- If the bot is still offline on Discord, type
heroku scale worker=1in the terminal because we used worker and not web dyno in our Procfile.
- On your Discord channel type
- Congratz. Your first bot is ready!
Create Test Bot Step-by-Step guide (with code snippets)
Goals: Understand how we create a basic bot application from scratch following the official example provided in Discordrb’s documentation.
Note: If you’re not familiar with Command Line yet, make sure to read Learn Enough Command Line to be Dangerous ;)
- Create your application folder in terminal:
$ mkdir test_bot
- Go to your newly created folder:
$ cd test_bot
bot.rbfile in your folder:
$ touch bot.rb(or using your OS UI or your favorite code editor)
- Open the newly created file and paste the following code there. The screenshot shows a cleaned-up version of the
require ‘discordrb’bot = Discordrb::Bot.new token: 'B0T.T0KEN.here'bot.message(content: ‘Ping!’) do |event|
5. For now, replace
'B0T.T0KEN.here' with your bot’s token we acquired earlier. Your line should look like this:
bot = Discordrb::Bot.new token 'YOUR TOKEN'
6. It’s time to create our
Gemfile (Remember: Gemfile does not need any file extension. Simply run
touch Gemfile in your console). Paste the following code there:
# gem ‘dotenv’
You can uncomment
dotenv and use to it store environmental variables in an
.env file if you want. If you do, make sure to add
require 'dotenv/load' in your original bot.rb file.
To learn more about .env files go to Dotenv GitHub repository.
7. Once Gemfile is created an empty
Gemfile.lock appears. Run
bundle command in your terminal.
8. Invite our bot (not working yet) to your Discord channel. Grab
ClientID from your Application’s view:
9. Follow this link and paste
ClientID at the bottom of the page. Select Send Messages & Read Messages privileges and click the link at the bottom of the page to add bot to your discord channel. Follow the instructions and add a bot to your channel.
10. The bot was added successfully (I hope), but it’s not online, and you can’t use the
Ping! command yet.
bundle exec ruby bot.rb in the application’s folder. This is what you should see:
Ping! in your Discord’s channel. It works!
13. You’re almost done, but if you turn off the terminal, the bot will stop responding. That is why we need to deploy it to Heroku as described in the next part.
Deploy your first bot to Heroku Step-by-Step (with code snippets)
- Go to http://herokuapp.com and set up an account. Don’t worry; it’s free!
- Log in and create a new application by clicking the
3. Choose your application’s name & region (whichever you prefer)
3. On app creation, you’re automatically redirected straight to the
Deploy section. We are not using hosting service for our git repository, so what we’re going to do is push our app to Heroku straight from the console. However, before we’re able to deploy our application on Heroku, we need to do two things.
4. First, go to
Settings tab and click
Add buildpack button. A pop-up menu will appear. Select Ruby and press Save Changes.
5. Now, we need to create a Procfile in our application’s folder. Do
$ touch Procfile, open it in your editor and paste this line:
worker: bundle exec ruby bot.rb Save the file. Note: Not sure what Procfile is? Heroku got you covered!
6. Now, we’re finally ready to deploy using heroku CLI. What should we do?
7. Install Heroku CLI:
brew install heroku/brew/heroku
8. Follow the instructions visible in your Heroku’s application
Deploy section. Note: Since you’re probably already in you application’s folder, start with
heroku login and then jump right into
git init command. Just follow the git flow and finish with
git push heroku master
IMPORTANT: If this is your first Heroku deploy it might take a few minutes to complete.
9. We are almost there. The application has successfully built on Heroku. Type
heroku scale worker=1 in your terminal. We have to do this because the
worker process is not automatically booted after the Heroku deployment.
Note: If you’re using
dotenv, make sure to add all environmental variables to heroku. Go to Settings -> Click
Reveal Config Vars and add your token there.
10. Go to your Discord channel, type
Ping! and see how the bot responds! You made it, congratz! 🥳
What we did is just the beginning. Discord’s bot has endless possibilities.
- Want to run giveaways on your channel using an external API? ✔
- Want to play some YouTube music? ✔
- Maybe you’re into time tracking or fancy reminders? ✔
- And so much more!
This guide wouldn’t happen without the help from Discord API channel Ruby community: z64, VxJasonxV, connorshea, Ghosty, and greenbigfrog. Thank you, guys!