Sha256: 09a3d0b4b4cf6c5eb1d199bbcc7b9ebe39a0ef66d2a948c79d1a14d0d06a694c

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

# 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.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"])

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
discorb-0.16.0 examples/components/authorization_button.rb
discorb-0.15.1 examples/components/authorization_button.rb
discorb-0.15.0 examples/components/authorization_button.rb
discorb-0.14.0 examples/components/authorization_button.rb