Hey there, fellow dev! Ready to dive into the world of Discord bots with Ruby? You're in for a treat. We'll be using the awesome discordrb
gem to create a Discord bot that'll make your server the coolest hangout spot. Let's get cracking!
Before we start, make sure you've got:
First things first, let's get our project set up. Open your terminal and run:
gem install discordrb
Now, create a new Ruby file. Let's call it awesome_bot.rb
. Feeling creative already, aren't we?
Time to breathe life into our bot. Open awesome_bot.rb
and add these lines:
require 'discordrb' bot = Discordrb::Bot.new token: 'YOUR_BOT_TOKEN_HERE'
Replace 'YOUR_BOT_TOKEN_HERE'
with your actual bot token. Keep it secret, keep it safe!
Let's give our bot some personality. Add these event handlers:
bot.ready do |event| puts "Logged in as #{bot.profile.username}!" end bot.message(content: 'ping') do |event| event.respond 'Pong!' end bot.run
Now your bot will say hello when it's ready and respond to 'ping' with 'Pong!'. Classic.
Ready to level up? Let's add some cooler features:
bot.application_command(:greet) do |event| event.respond content: "Hello, #{event.user.name}!" end
bot.reaction_add do |event| puts "#{event.user.name} reacted with #{event.emoji.name}" end
bot.message(content: '!embed') do |event| event.channel.send_embed do |embed| embed.title = "Cool Embed" embed.description = "This is a super cool embed!" embed.color = 0xFF0000 # Red end end
Time to unleash your creation! You've got a few hosting options:
Whichever you choose, make sure your bot runs continuously. A simple bash script can help:
while true; do ruby awesome_bot.rb sleep 5 done
Remember, with great power comes great responsibility:
bot.message do |event| begin # Your code here rescue => e puts "Oops, something went wrong: #{e.message}" end end
And there you have it! You've just created a Discord bot with Ruby. Pretty cool, right? Remember, this is just the beginning. The Discord API is vast and full of possibilities. Keep exploring, keep coding, and most importantly, have fun!
Want to learn more? Check out the discordrb
documentation and join the Discord API server. The community is awesome and always ready to help.
Now go forth and bot! 🤖✨