Sha256: 72bb6e131aebea7d39aa8f4846ae7ddfcb28796bb4112a5d42214af95b6aabd4

Contents?: true

Size: 1.65 KB

Versions: 5

Compression:

Stored size: 1.65 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

# description: Connect to discord and start IRB.

require "io/console"
require "discorb"
require "optparse"

intents_value = Discorb::Intents.all.value
token_file = "token"

opt = OptionParser.new <<~BANNER
                         This command will start an interactive Ruby shell with connected client.

                         Usage: discorb irb [options]
                       BANNER
opt.on("-i", "--intents", "intents to use, default to all") { |v| intents_value = v }
opt.on("-t", "--token-file", "token file to load, default to \"token\"") { |v| token_file = v }
opt.parse!(ARGV)

client = Discorb::Client.new(intents: Discorb::Intents.from_value(intents_value))
$messages = []

client.on :standby do
  puts "\e[96mLogged in as #{client.user}\e[m"

  def message
    $messages.last
  end

  def dirb_help
    puts <<~MESSAGE
           \e[96mDiscord-IRB\e[m
           This is a debug client for Discord.
           \e[90mmessage\e[m to get latest message.

           \e[36mhttps://discorb-lib.github.io/#{Discorb::VERSION}/file.irb.html\e[m for more information.
         MESSAGE
  end

  puts <<~FIRST_MESSAGE
         Running on \e[31mRuby #{RUBY_VERSION}\e[m, disco\e[31mrb #{Discorb::VERSION}\e[m
    Type \e[90mdirb_help\e[m to help.
       FIRST_MESSAGE

  binding.irb

  client.close!
end

client.on :message do |message|
  $messages << message
end

token = ENV.fetch("DISCORD_BOT_TOKEN", nil) || ENV.fetch("DISCORD_TOKEN", nil)
if token.nil?
  if File.exist?(token_file)
    token = File.read(token_file)
  else
    print "\e[90mToken?\e[m : "
    token = $stdin.noecho(&:gets).chomp
    puts
  end
end

client.run token

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
discorb-0.19.0 lib/discorb/exe/irb.rb
discorb-0.18.1 lib/discorb/exe/irb.rb
discorb-0.18.0 lib/discorb/exe/irb.rb
discorb-0.17.1 lib/discorb/exe/irb.rb
discorb-0.17.0 lib/discorb/exe/irb.rb