Build Discord bot in minutes using Discordrb gem.

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:

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.

Good luck!

Create Bot Application on Discord

Goals: Familiarize yourself with Discord’s developer dashboard.

  1. Register an account on Discord (if you haven’t already 😅)
  2. Visit https://discordapp.com/developers/applications/
  3. Create an Application and give it a name.
  4. Go to the Bot tab, and create one by clicking the Add Bot button.
  5. Copy your bot token and save it somewhere — you’re going to need it soon.
  6. 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.

  1. Create test Discord channel where you’ll be able to invite the bot
  2. Create bot.rb file 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
  3. Provide BOT TOKEN where it’s required. You can use dotenv too. Just remember to require dotenv/load in your bot.rbfile.
  4. Create Gemfile with gem 'discordrb'
  5. 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.
  6. Create Heroku app, add ruby buildpack and add your env variables if you’re using dotenv and push your code to master.
  7. If the bot is still offline on Discord, type heroku scale worker=1 in the terminal because we used worker and not web dyno in our Procfile.
  8. On your Discord channel type Ping!
  9. 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 ;)

  1. Create your application folder in terminal:$ mkdir test_bot
  2. Go to your newly created folder: $ cd test_bot
  3. Create bot.rb file in your folder: $ touch bot.rb (or using your OS UI or your favorite code editor)
  4. Open the newly created file and paste the following code there. The screenshot shows a cleaned-up version of the ping.rb example.
require ‘discordrb’bot = Discordrb::Bot.new token: 'B0T.T0KEN.here'bot.message(content: ‘Ping!’) do |event|
event.respond ‘Pong!’
end
bot.run

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:

source ‘https://rubygems.org'
gem ‘discordrb’
# 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.

11. Type bundle exec ruby bot.rb in the application’s folder. This is what you should see:

libsodium error is expected since we don’t use it.

12. Type 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)

  1. Go to http://herokuapp.com and set up an account. Don’t worry; it’s free!
  2. Log in and create a new application by clicking the New button.
Select “Create New Application” after clicking the “New” select button.

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 theworker 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! 🥳

Final Words

What we did is just the beginning. Discord’s bot has endless possibilities.

  1. Want to run giveaways on your channel using an external API? ✔
  2. Want to play some YouTube music? ✔
  3. Maybe you’re into time tracking or fancy reminders? ✔
  4. And so much more!

Dive into Discordrb’s examples, read the official documentation and start building the next big thing 💪 Oh, and if you ever need a hand, join us on Discord: Discord API channel.

This guide wouldn’t happen without the help from Discord API channel Ruby community: z64, VxJasonxV, connorshea, Ghosty, and greenbigfrog. Thank you, guys!

IT Project Manager.