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.on :message do |message|
  next if message.author.bot?
  next unless message.content == "ping"

  message.channel.post("Pong!")
end

client.run(ENV.fetch("DISCORD_BOT_TOKEN", nil))

Wait for a message

# frozen_string_literal: true

require "discorb"

client = Discorb::Client.new

client.once :standby do
  puts "Logged in as #{client.user}"
end

client.on :message do |message|
  next if message.author.bot?
  next unless message.content == "!quiz"

  operator = %i[+ - *].sample
  next unless operator

  num1 = rand(1..10)
  num2 = rand(1..10)

  val = num1.send(operator, num2)
  message.channel.post("Quiz: `#{num1} #{operator} #{num2}`")
  begin
    msg = client.event_lock(:message, 30) do |m|
      m.content == val.to_s && m.channel == message.channel
    end.wait
  rescue Discorb::TimeoutError
    message.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

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 |message|
  next if message.author.bot?
  next unless message.content.start_with?("!auth ")

  role_name = message.content.delete_prefix("!auth ")
  role = convert_role(message.guild, role_name)
  if role.nil?
    message.reply("Unknown role: #{role_name}").wait
    next
  end
  message.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.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.on :message do |message|
  next if message.author.bot?
  next unless message.content == "!ruby"

  options = SECTIONS.map.with_index do |section, i|
    Discorb::SelectMenu::Option.new("Page #{i + 1}", "sections:#{i}", description: section[0])
  end
  message.channel.post(
    "Select a section", components: [Discorb::SelectMenu.new("sections", options)],
  )
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.