Skip to content
← Back to Community
Making a Discord bot in Ruby!
Profile icon
theangryepicbanana

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.

Voters
Profile icon
Tech90
Profile icon
AnyClaw
Profile icon
Shoot-XX
Profile icon
Nanowrimoijk
Profile icon
CodingCactus
Profile icon
frissyn
Profile icon
firefish
Profile icon
MemeManHimself
Profile icon
KobeFF
Profile icon
snbremang
Comments
hotnewtop
Profile icon
mat1

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

Profile icon
theangryepicbanana

@mat1 oh dang good catch

Profile icon
21Miya

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

Profile icon
theangryepicbanana

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

Profile icon
Navinor

@theangryepicbanana are you sure about that

Profile icon
theangryepicbanana
Profile icon
Navinor
Profile icon
John_WardWard

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

Profile icon
theangryepicbanana

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

Profile icon
[deleted]

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

Profile icon
Zavexeon

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

Nevermind, solved this issue.

Profile icon
Jovantri

@Zavexeon me too, how to fix :/

Profile icon
Zavexeon

@Jovantri try require_relative 'discordrb'

Profile icon
theangryepicbanana

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

Profile icon
amasad

@theangryepicbanana do you have an example repl?

Profile icon
theangryepicbanana

@amasad This is what I'm working on right now:

Profile icon
amasad

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

Profile icon
theangryepicbanana

@amasad Got it

Profile icon
theangryepicbanana

wait, what is OP?

Profile icon
[deleted]

@theangryepicbanana Original Post

Profile icon
[deleted]

@Polygon No its overpowered

Profile icon
theangryepicbanana

ok

Profile icon
DaCuteRaccoon

@theangryepicbanana bro why do you wake up so early? you need your sleep.

Profile icon
theangryepicbanana

@DaCuteRaccoon Because 4 years ago, I was either on the bus or at school at 7-8am

Profile icon
DaCuteRaccoon

@theangryepicbanana I go to school at 8:30 AM and wake up at 8:15

Profile icon
DaCuteRaccoon

@theangryepicbanana leaves 15 minutes to get ready and eat, perfect time

Profile icon
DaCuteRaccoon

@theangryepicbanana then I walk to school and arrive at 9:00

Profile icon
CodingCactus

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.

Profile icon
CodingCactus

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