How to Make a Simple Discord Bot in Ruby to Annoy Your Friends

Behold the effects of prolonged exposure to spamming Pearl Jam’s “Alive” in the group voice chat (Source: Chris)

Discord is a great program for hanging out with friends while playing video games. Mainly, it’s the quickest way to transfer “memes” from one social media site to your personal circle of friends and then yell at each other in the voice chat in response to said memes.

Are you upset yet?

Another great feature of Discord is that you can create bots that can play music, make announcements in group chat, assign privileges to users, and much more. And while there are already a ton of bots written by other users, writing your own can be a fun exercise in learning how to interact with APIs and someone else’s documentation. Plus, you get to personalize it with whatever features you want and none that you don’t. Let’s get started!

  1. Register your bot

Head on over to the Discord website’s developer page (make sure you’re logged in to your Discord account) and click the “My Apps” button in the top left under the Applications header.

Discord’s developer page

From there it will take you to a page listing all of your apps. Mine looks like this because I’ve already spawned two beautiful bot boys, but yours should just have the “New App” button. Click that one!

Do not talk bad about my beautiful adult sons anywhere in the comments section, thank you

From here you need to give your bot a sexy name that makes it easy to pick it out from a crowd. I suggest something short and catchy, possibly ending with suffix “-boy”. Don’t worry about the “Add redirects” on the right section, you can deal with that later if you want to add fancier features that I won’t be talking about here. Give it a nice image that will be its profile picture in-app, and maybe a short bio and then click “Create App”! If all went according to plan you should be redirected to your new app’s profile page.

Go ahead and click the “Create Bot User” button on this page to convert your app into a bot. You’ll need to click OK on on a dialog box warning you this is a one-way process. Why a bot instead of just an app? A bot can do some things a user can like post messages, join voice channels, and essentially be an identifiable entity that users can interact with in ways they can’t with just an app.

Welcome to the new world my new digital son

Here you can set if you want the bot to be public, as in anyone can add your bot to their server. Leave this unchecked if you plan on just having a bot for your own personal server. For now all you need is the Client ID and Client Secret — the ID being Discord’s way of identifying your bot and the secret being how Discord knows it’s you rather than some rando using your bot to communicate with the API!

2. Install Dependencies

Just for brevity’s sake I’m going to assume you have a reasonably up-to-date version of Ruby and are working on a Mac or Linux system. I’m using Ruby 2.4 and I’m pretty sure anything past 2.1 will work just fine. Also, a lot of these steps still apply if you’re working on a Windows machine but it’s generally a much bigger pain to get it set up. Make sure to check the Windows section of installation instructions for each of these tools. Besides that here’s everything you’ll need to get started:

  • homebrew (for Mac): This is a nifty tool for a lot of uses beyond this tutorial, and is usually the easiest way to install a development tool on Mac.
  • discordrb: A nice Ruby gem for the Discord API. Make sure to keep the documentation on hand, it takes a bit of time to figure out the structure and flow of how it works but it makes a lot of sense conceptually once you figure out the basics. Install by typing this into your terminal:
gem install discordrb
  • libsodium: You’ll need this for discordrb’s voice functionality to work. If you’ve got homebrew set up it’s as easy as:
brew install libsodium
  • ffmpeg: Another popular library for audio/video that’s required by discordrb for voice support. To install:
brew install ffmpeg

Also, this probably goes without saying but you also need to have Discord and some sort of text editor installed. With that said let’s get to writing!

3. Create Your Beautiful Robot Son

The discordrb github page has a nice collection of example bots that I like to use as reference. For now, I’ll refer to the example used in their readme:

require 'discordrb'

bot = token: '<token here>', client_id: 168123456789123456

bot.message(with_text: 'Ping!') do |event|
event.respond 'Pong!'

This example bot will respond with ‘Pong!’ to any message which says ‘Ping!’, as long as the bot has messaging privileges within that server (we’ll go over how to add privileges in a bit!). The most important bits in this code to notice are these:

  • You have to have a ‘require’ statement for the discordrb gem just like any other gem!
  • You have to spawn your robot boy using the ‘’ line of code. There are different types of Bots built-in to the discordrb gem, including CommandBot, but calling the constructor for any of them generally follows the same syntax.
  • Make sure to replace ‘<token here>’ with your bot’s Client Secret and the ‘client_id’ with your bot’s Client ID from your bot’s Discord page.
  • Use your bot’s .run command at the end of your script to commence the birthing ritual.

For my bot I added some commands which make the bot join voice servers and play music files locally. I also used the CommandBot instead of the standard bot for reasons I’ll explain in just a second. First, I’d like to mention that since I wanted to keep my code on github I decided to keep my Client Secret in a separate .json file called ‘info.json ’within the same directory as my Ruby code. I used the json Ruby gem to parse my .json file and saved the results as a hash called ‘info_hash’. Thus my client secret is in the info_hash under the key ‘token’. Anyway, I instantiated my bot like so:

bot = token: info_hash['token'], client_id: 359082109119365140, prefix: '~'

A nice feature of the CommandBot is that you can add a ‘prefix’ symbol to its instantiation statement so your bot will only respond to text commands which start with that symbol. The CommandBot also has a nifty method called command which runs whenever the bot’s prefix symbol is called in conjunction with the symbol passed in as a parameter. For example, here’s the block of code that I use to play songs through my bot.

bot.command(:play) do |event|  
song = event.message.content.split(' ')[1]
if songs[song]

So, if I’m in a Discord server that the bot has access to, and I post the message “~play song”, the .command block will execute since the message had the prefix (‘~’) and the command symbol (‘play’). From there I split the message by spaces so I can get the second word from the message sent by the event handler. I then check if that string is the title of a song in my songs hash from my info.json file. The values of those keys are paths for those songs stored in the same folder as my Ruby code. The ‘event.voice’ is the location of a voice channel from where the “~play song” command came from, meaning this code crashes if the user who posted the message isn’t in voice server.

If everything all went according to plan the bot will join the voice server it was summoned from and start playing the music file over voice chat!

There’s obviously a lot of different things you can do with a Discord bot, but I think playing music is a nice jumping off point for adding more functionality. Check the documentation for discordrb and remember to go slow and add incrementally!

4. Add Your Bot to Your Server

Go here to create an invite link with your bot. You can add permissions to the invite so you don’t have to manually add them later. Your permissions may vary depending on what exactly you want your bot to do. In general, the orange-text permissions are more admin-based permissions and should therefore be avoided unless you’re sure of what you’re doing. Otherwise, stacking all white-text permissions isn’t the worst idea. For reference, here’s the permissions I used for my bot.

Add your bot’s Client ID to the Client ID form below and click the link at the bottom of the page! From there, it should ask you to either log in to Discord in-browser or ask you which server you want to add the bot to. Pick the the right one, and your bot should automatically show up in that server’s user list!

5. Activate!

If everything went according to plan you should be able to run the Ruby script and have your bot show up as logged in!

Here he is, the most beautiful boy in the world

And now you should be able to type any of your programmed commands into the text chat (with the prefix if you’re using the CommandBot) and your bot should react accordingly. If it doesn’t, follow standard debugging practice and check your terminal where the script is running and scream eternally.

My preferred test is to join a voice channel full of friends currently engaged in serious gaming and type ‘~play rush’ to blast Rush’s “Tom Sawyer” in order to soothe their nerves and strengthen their team bonding.

Hopefully this was informative and a helpful resource for anyone wanting to learn how Discord bots work, regardless of skill level. In general, I think the practice of writing your own Discord bot is very fun and a good way to parse documentation. Don’t forget to add new features slowly and incrementally. Most of the time I spent writing my bot came down to just poring over the discordrb documentation to figure out what does what. Be patient, it may take a bit of time but you’ll figure it out eventually. Also consider pushing your code remotely to github to make it easy to revert changes and track your progress. Just remember to keep your Client Secret separate if you do!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.