Quick Start
Let's build your first Discord bot with DiscordRDA. This guide will walk you through creating a simple bot that responds to messages and slash commands.
Step 1: Create a Bot Applicationβ
- Go to the Discord Developer Portal
- Click "New Application" and give it a name
- Go to the "Bot" section and click "Add Bot"
- Copy your bot token (keep this secret!)
- In "Privileged Gateway Intents", enable:
- Message Content Intent (for reading message content)
- Server Members Intent (for member-related events)
Step 2: Create Your Bot Fileβ
Create a file named bot.rb:
require 'discord_rda'
# Create a new bot instance
bot = DiscordRDA::Bot.new(
token: ENV['DISCORD_TOKEN'],
intents: [:guilds, :guild_messages, :message_content]
)
# Respond to messages
bot.on(:message_create) do |event|
if event.content == '!ping'
event.message.respond(content: 'Pong! π')
end
end
# Run the bot
bot.run
Step 3: Set Your Tokenβ
Set your Discord token as an environment variable:
export DISCORD_TOKEN="your-bot-token-here"
Or create a .env file:
DISCORD_TOKEN=your-bot-token-here
Step 4: Run the Botβ
ruby bot.rb
You should see output indicating the bot has connected. Invite the bot to your server and type !ping - it should respond with "Pong! π".
Adding a Slash Commandβ
Let's add a modern slash command:
require 'discord_rda'
bot = DiscordRDA::Bot.new(
token: ENV['DISCORD_TOKEN'],
intents: [:guilds]
)
# Register a slash command
bot.slash('hello', 'Say hello') do |cmd|
cmd.string('name', 'Your name', required: true)
cmd.handler do |interaction|
name = interaction.option('name')
interaction.respond(content: "Hello, #{name}! π")
end
end
bot.run
Complete Exampleβ
Here's a more complete example with multiple features:
require 'discord_rda'
bot = DiscordRDA::Bot.new(
token: ENV['DISCORD_TOKEN'],
intents: [:guilds, :guild_messages, :message_content],
log_level: :info
)
# Message event handler
bot.on(:message_create) do |event|
case event.content
when '!ping'
event.message.respond(content: 'Pong!')
when '!help'
event.message.respond(content: 'Available commands: !ping, /hello, /info')
end
end
# Slash command: hello
bot.slash('hello', 'Say hello') do |cmd|
cmd.string('name', 'Your name', required: true)
cmd.handler do |interaction|
name = interaction.option('name')
interaction.respond(content: "Hello, #{name}!")
end
end
# Slash command: info
bot.slash('info', 'Bot information') do |cmd|
cmd.handler do |interaction|
interaction.respond(
content: '**DiscordRDA Bot**\nVersion: 1.0\nRuby: 3.0+',
ephemeral: true
)
end
end
# Ready event
bot.on(:ready) do |event|
puts "Bot is ready! Logged in as #{event.user.username}"
end
bot.run
Inviting Your Botβ
To invite your bot to a server:
- Go to Discord Developer Portal > OAuth2 > URL Generator
- Select
botscope - Select
applications.commandsscope (for slash commands) - Select permissions your bot needs (e.g., Send Messages, Read Message History)
- Copy and open the generated URL
Next Stepsβ
- Learn about configuration - Configure intents, logging, caching
- Explore events - Handle different Discord events
- Build slash commands - Create advanced commands