Examples
Simple ping pong
# frozen_string_literal: true
require "discorb"
client = Discorb::Client.new
client.once :standby do
puts "Logged in as #{client.user}"
end
client.slash("ping", "Ping!") do |interaction|
interaction.post("Pong!", ephemeral: true)
end
client.run(ENV.fetch("DISCORD_BOT_TOKEN", nil))
Wait for a message
# frozen_string_literal: true
require "discorb"
intents = Discorb::Intents.new
intents. = true
client = Discorb::Client.new(intents: intents)
client.once :standby do
puts "Logged in as #{client.user}"
end
client.on :message do ||
next if ..bot?
next unless .content == "!quiz"
operator = %i[+ - *].sample
next unless operator
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) do |m|
m.content == val.to_s && m.channel == .channel
end
.wait
rescue Discorb::TimeoutError
.channel.post("No one answered...")
else
msg.reply("Correct!")
end
end
client.run(ENV.fetch("DISCORD_BOT_TOKEN", nil))
Use components
Authorization button
# frozen_string_literal: true
require "discorb"
client = Discorb::Client.new(intents: Discorb::Intents.new(message_content: true))
client.once :standby do
puts "Logged in as #{client.user}"
end
client.slash(
"auth",
"Send authorization button",
{ role: { description: "The role to give.", type: :role, required: true } }
) do |interaction|
.channel.post(
"Click this button if you are human:",
components: [
Discorb::Button.new("Get role", custom_id: "auth:#{interaction.id}")
]
)
end
client.on :button_click do |response|
if response.custom_id.start_with?("auth:")
id = response.custom_id.delete_prefix("auth:")
response.user.add_role(id).wait
response.post(
"You got your role!\nHere's your role: <@&#{id}>",
ephemeral: true
)
end
end
client.run(ENV.fetch("DISCORD_BOT_TOKEN", nil))
Pagination with select menu
# frozen_string_literal: true
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.slash("ruby", "Learn more about Ruby.") do |interaction|
=
SECTIONS.map.with_index do |section, i|
Discorb::SelectMenu::Option.new(
"Page #{i + 1}",
"sections:#{i}",
description: section[0]
)
end
interaction.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.fetch("DISCORD_BOT_TOKEN", nil))
Note
This example is not shown correctly in the docs. Read this example on GitHub.