Learn to Code via Tutorials on Repl.it!

← Back to all posts
Making a Discord bot in Ruby!

How to get started

Step 1: install discordrb

The discordrb gem (library) is required to make a discord bot in Ruby. You can install it by typing `gem install discordrb` or by using bundler. Then, in your code put require "discordrb" on the first line.

Step 2: setup your bot

Assuming that you have created your bot on Discord already, you need to create a bot by doing the following (of course, you can change the name):

And of course, <my-token> and <my-id> are replaced with your bot's token and id. @my_bot.run true and @my_bot.join help keep the bot running btw. If you want a predetermined prefix, add , prefix: "<my-prefix>" to the end of the first line (and "<my-token>" is replaced with your token).

Step 3: adding commands to your bot

If you have a prefix

Having a prefix makes things easier (though a bit limited). To add a simple command, use the command method on your bot, followed by the command name (as a string or symbol), any options (will be described later), and the event block (like an anonymous function that looks like a control structure). Here's an example of a command that takes 2 or more numbers, and adds them up:

This would be used as <my-prefix> sum num1 num2 [... num].

If you don't have a prefix

Not having a prefix is not a bad thing, it just means that you want your bot to do more. You can get every message by using the message command on your bot. For each message, you can specify options like:

  • contains: "some text" (searching for text)
  • content: "some text" (to match entire message) or content: /some_regex/ (to match a pattern)
  • starts_with: "text" or ends_with: "text" (for matching the beginning or end of a message
    ...and other things. To create something like the previous example:

This would be used as <my-prefix> sum num1 num2 [... num].

Step 4: profit

Because you made your bot in Ruby, everyone will love it. Eventually, your bot turns into a paid subscription service. Good night.


Hey, I found a kinda important mistake in your post. Putting your token in plain text is an extremely bad idea, since anyone can just go in and modify your bot behavior to do whatever they want.
You can fix this by creating a .env file and putting your token with the format token=Asdfghjkl. Then you can access that env file in Ruby with ENV["TOKEN"] but no one else will be able to see it.

Please upvote this comment if it was helpful or useful in any way


@mat1 oh dang good catch


I always knew you could code Discord bots with Python and JavaScript, but I never realized Ruby's potential to do this, too! This is awesome :)


@21Miya Thanks! (btw Ruby has more potential than either of those)


@theangryepicbanana are you sure about that


Thanks for making this as I was quite confused about how to make a bot:)


@John_WardWard no problem! I didn't even know how they worked until a few days ago, and then I made tons of sense.


nice job! I might make my bot in ruby but I think I will stick JS and Python for making them.


require 'discordrb' doesn't like to work. :(

Nevermind, solved this issue.


@Zavexeon me too, how to fix :/


@Jovantri try require_relative 'discordrb'


Oh jeez I made this at 4:00 am pls tell me any mistakes I made.


@theangryepicbanana do you have an example repl?


@amasad This is what I'm working on right now: <link removed because ppl could access my bot>


@theangryepicbanana cool, please link it in the OP when you're ready


@amasad Got it


wait, what is OP?


@theangryepicbanana Original Post


@Polygon No its overpowered




hi, so um how do I get a channel by it's id to then send an embed in. I've searched through the discordrb docs and I can't see how I would go about this.

What I saw on the docs was to use the #send_message method but if I'm honest I'm not too sure how I'm supposed to use it. I've done what I think I am supposed to do on lines 18-27 of https://replit.com/@CodingCactus/rt-bot but I'm getting an error saying "400 Bad Request" so I'm doing something wrong, I'm just not sure what.


there's also this which seems like it would work really well if I could just figure out how to get a channel by it's id