How to Build a Discord Bot in Ruby on Rails
At OmbuLabs, we recently had the opportunity to develop a Discord bot from scratch. We found the experience both rewarding and insightful, prompting us to create this tutorial to share our approach with you.
In this guide, we’ll walk you through the process of creating a Discord bot using Ruby on Rails, leveraging the efficiency and ease of development provided by this powerful framework.
Why Ruby on Rails
We opted for Ruby on Rails for this project for several reasons. Firstly, we’re big fans of the framework and appreciate its productivity and ease of use.
Additionally, we needed a solution that allowed us to build the bot quickly without compromising on functionality or maintainability, and Ruby on Rails proved to be the perfect fit.
While we ended up settling on Ruby, this is not the only language that a discord bot can be built in, other popular languages include Python and JavaScript.
Getting Started
Before we begin, it’s best to have a basic understanding of Ruby and Rails. If you’re new to either language or framework, we recommend familiarizing yourself with the fundamentals before diving into this tutorial.
With that said, let’s get started on creating your Discord bot with Ruby on Rails!
Prerequisites
- Ruby and Rails
- Discord Developer account
Setting up the Rails Project
- Create a new Rails project:
rails new discord_bot_project
- Set up necessary gems:
- discordrb : A Ruby library for interacting with the Discord API
- dotenv-rails : For managing environment variables
In your Gemfile add the following and run
bundle install
:gem 'discordrb' gem 'dotenv-rails'
- Discord bot setup
- Create a new application using the Discord Developer Portal
- Obtain the bot token and add it to your Rails project, you can read more about this in the documentation for getting started in the Discord Developer Portal .
- Continue setting up the Rails Application
- Create a
.env
file and addDISCORD_BOT_TOKEN=your_token_here
. - Add
.env
to your.gitignore
to keep the token secret.
- Create a
-
Create a Discord bot client in your Rails application.
There are a number of ways that the bot can be setup and run, one of the simplest would be to do something like this:
In config/discord_bot.rb
require 'discordrb' bot = Discordrb::Bot.new token: ENV['DISCORD_BOT_TOKEN'] bot.message(content: 'Ombu!') do |event| event.respond 'Labs!' end bot.run
For our purposes we needed a setup that was a bit more involved. We had two main problems happening.
The first issue was that our Puma configuration was spinning up 5 threads and this was causing multiple instances of the bot to be running at the same time. This in turn would cause multiple instances of the bot to acknowledge events that were fired, leading to multiple messages being sent in response to the same event, for example.
We also had an issue where sometimes listeners were not being turned on by the time the event happened, and therefore we occasionally had no response. We decided the simplest solution would be to turn on all the listeners when starting up the bot.
To solve these issues we created a
Discord::Bot
class that took care of setting up and starting the bot. We placed this in ourapp/lib
directory because it made autoloading easier via Zeitwerk.Moving our bot to a class and starting the bot with a
rake task
allowed us to separate our web server and our bot. This prevented our issue of having multiple instances of the bot running at the same time, and therefore the listeners only acknowledged events once as we intended.Using the
rake task
to configure and run the bot had the added functionality of allowing us to register all of our slash commands at the same time. This meant that we didn’t have to re-register commands, but we could update commands if necessary. When we were first developing the app we found that we spent a lot of time waiting for the app to start up when it was registering the slash commands every time. -
Testing your discord bot
To interact with your bot, you’ll need to add it to a Discord server. Follow these steps:
- Go to the Discord Developer Portal and select the application you previously created.
- In the sidebar, navigate to OAuth2 -> URL Generator.
- Choose the scopes you want to generate for your application. For testing purposes, we selected
bot
in the first list andAdministrator
in the second. However, the scopes can be decided depending on what your needs are. We went with broad scopes so we wouldn’t have issues during testing. - Copy the generated URL.
- Paste the URL into your browser and authorize the bot to join a server that you have administrative access to. If needed, you can create a test server for this purpose.
Now, your bot should be successfully added to the Discord server, allowing you to test its functionality..
Future Steps
Now that you have your bot up and running you can start adding features such as:
- Responding to specific commands
- Managing server events
- Interacting with external APIs
Although we won’t delve into all the details in this post, we’re currently working on additional posts that will provide further insights. However, we’ll leave you with a glimpse of how to register a slash command.
To register the command we can grab the server_id
from Discord. We can then use register_application_command
and pass in the correct arguments to register the command. Later we can add listeners to handle the event of when the command is called.
Here’s an example of registering a command called say_hello
:
bot.register_application_command(:say_hello, "Say hello to the server", server_id: ENV.fetch("DISCORD_SERVER_ID", nil))
When you register a slash command like this, you’re essentially informing Discord about a new slash command that your bot can handle. In this case, the command say_hello
will enable users to greet others within the server.
Conclusion
Creating a Discord bot with Ruby on Rails opens up a world of possibilities for community engagement and management. By integrating the discordrb gem, you can easily develop powerful bots that automate tasks, moderate discussions, and provide entertainment to your Discord server.
We loved building this project in Ruby on Rails, but of course Discord bots can be built in multiple other languages. There are very robust libraries to build Discord bots in Python and JavaScript, for example.
Looking to build a Discord bot with Ruby, Python or JavaScript? Get in touch and see how we can help! .