Learn to Code via Tutorials on Repl.it!

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

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!

@my_bot.join

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
end

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
end

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.

Comments
hotnewtop
mat1 (4418)

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

21Miya (145)

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 (364)

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

theangryepicbanana (1692)

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

[deleted]

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

Zavexeon (1187)

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

Nevermind, solved this issue.

Jovantri (5)

@Zavexeon me too, how to fix :/

Zavexeon (1187)

@Jovantri try require_relative 'discordrb'

theangryepicbanana (1692)

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

amasad (3351)

@theangryepicbanana do you have an example repl?

theangryepicbanana (1692)

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

amasad (3351)

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

[deleted]

@theangryepicbanana Original Post

[deleted]

@Polygon No its overpowered

CodingCactus (4138)

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.

CodingCactus (4138)

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