Examples
Simple ping pong
require "discorb"
client = Discorb::Client.new
client.once :standby do
puts "Logged in as #{client.user}"
end
client.on :message do ||
next if ..bot?
next unless .content == "ping"
.channel.post("Pong!")
end
client.run(ENV["DISCORD_BOT_TOKEN"])
Wait for a message
require "discorb"
client = Discorb::Client.new
client.once :standby do
puts "Logged in as #{client.user}"
end
client.on :message do ||
next if ..bot?
next unless .content == "!quiz"
operator = [:+, :-, :*].sample
num1 = rand(1..10)
num2 = rand(1..10)
val = num1.send(operator, num2)
.channel.post("Quiz: `#{num1} #{operator} #{num2}`")
begin
msg = client.event_lock(:message, 30) { |m|
m.content == val.to_s && m.channel == .channel
}.wait
rescue Discorb::TimeoutError
.channel.post("No one answered...")
else
msg.reply("Correct!")
end
end
client.run(ENV["DISCORD_BOT_TOKEN"])
Use components
Authorization button
require "discorb"
client = Discorb::Client.new
def convert_role(guild, string)
guild.roles.find do |role|
role.id == string || role.name == string || role.mention == string
end
end
client.once :standby do
puts "Logged in as #{client.user}"
end
client.on :message do ||
next if ..bot?
next unless .content.start_with?("!auth ")
role_name = .content.delete_prefix("!auth ")
role = convert_role(.guild, role_name)
if role.nil?
.reply("Unknown role: #{role_name}").wait
next
end
.channel.post(
"Click this button if you are human:",
components: [
Discorb::Button.new(
"Get role", custom_id: "auth:#{role.id}",
),
],
)
end
client.on :button_click do |response|
if response.custom_id.start_with?("auth:")
id = response.custom_id.delete_prefix("auth:")
response.fired_by.add_role(id).wait
response.post("You got your role!\nHere's your role: <@&#{id}>", ephemeral: true)
end
end
client.run(ENV["DISCORD_BOT_TOKEN"])
Pagination with select menu
require "discorb"
client = Discorb::Client.new
SECTIONS = [
["About", <<~WIKI],
["Early concept", <<~WIKI],
["First publication", <<~WIKI],
The first public release of Ruby 0.95 was announced on Japanese domestic newsgroups on December 21, 1995.
Subsequently, three more versions of Ruby were released in two days.
The release coincided with the launch of the Japanese-language ruby-list mailing list, which was the first mailing list for the new language.
Already present at this stage of development were many of the features familiar in later releases of Ruby, including object-oriented design, classes with inheritance, mixins, iterators, closures, exception handling and garbage collection.
WIKI
].freeze
WIKIPEDIA_CREDIT = "(From: [Wikipedia](https://en.wikipedia.org/wiki/Ruby_(programming_language)))"
client.once :standby do
puts "Logged in as #{client.user}"
end
client.on :message do ||
next if ..bot?
next unless .content == "!ruby"
= SECTIONS.map.with_index { |section, i| Discorb::SelectMenu::Option.new("Page #{i + 1}", "sections:#{i}", description: section[0]) }
.channel.post(
"Select a section", components: [Discorb::SelectMenu.new("sections", )],
)
end
client.on :select_menu_select do |response|
next unless response.custom_id == "sections"
id = response.value.delete_prefix("sections:")
selected_section = SECTIONS[id.to_i]
response.post(
"**#{selected_section[0]}**\n" \
"#{selected_section[1].strip}\n\n" \
"#{WIKIPEDIA_CREDIT}", ephemeral: true,
)
end
client.run(ENV["DISCORD_BOT_TOKEN"])
Note
This example is not shown correctly in the docs. Read this example on GitHub.