Learn to Code via Tutorials on Repl.it!

← Back to all posts
Making a Discord bot in Ruby!
theangryepicbanana (1604)

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):

@my_bot = Discordrb::Bot.new token: <my-token>, client_id: <my-id>
@my_bot.run true

# commands go here!


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:

@my_bot.command :sum, min_args: 2 do |event, *args|
	numbers = args.collect &:to_i # turns each argument into a number
	sum = numbers.collect &:+    # adds up everything in "numbers"
	event.respond sum
	# I could have just done `event.respond args.collect(&:to_i).collect(&:+)` btw

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:
@my_bot.message content: /^<my\-prefix>\s+sum\s+.+$/ do |event|
	args = event.content.split " " # "event.content" is the message
	numbers = args.collect &:to_i # turns each string in "args" into a number
	sum = numbers.collect &:+    # adds up everything in "numbers"
	event.respond sum
	# I could have just done `event.respond event.content.split(" ").collect(&:to_i).collect(&:+)` btw

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.

mat1 (3274)

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


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

21Miya (89)

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 :)

John_WardWard (257)

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

theangryepicbanana (1604)

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

Zavexeon (1034)

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

Nevermind, solved this issue.

theangryepicbanana (1604)

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

amasad (2407)

@theangryepicbanana do you have an example repl?

theangryepicbanana (1604)

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

amasad (2407)

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


@theangryepicbanana Original Post


@Polygon No its overpowered