Class: Discordrb::Interactions::OptionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/data/interaction.rb

Overview

A builder for defining slash commands options.

Constant Summary collapse

CHANNEL_TYPES =

Channel types that can be provided to #channel

{
  text: 0,
  dm: 1,
  voice: 2,
  group_dm: 3,
  category: 4,
  news: 5,
  store: 6,
  news_thread: 10,
  public_thread: 11,
  private_thread: 12,
  stage: 13
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


540
541
542
# File 'lib/discordrb/data/interaction.rb', line 540

def options
  @options
end

Instance Method Details

#attachment(name, description, required: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

Returns:

  • Hash



667
668
669
# File 'lib/discordrb/data/interaction.rb', line 667

def attachment(name, description, required: nil)
  option(TYPES[:attachment], name, description, required: required)
end

#boolean(name, description, required: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

Returns:

  • Hash



613
614
615
# File 'lib/discordrb/data/interaction.rb', line 613

def boolean(name, description, required: nil)
  option(TYPES[:boolean], name, description, required: required)
end

#channel(name, description, required: nil, types: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

  • types (Array<Symbol, Integer>) (defaults to: nil)

Returns:

  • Hash



630
631
632
633
# File 'lib/discordrb/data/interaction.rb', line 630

def channel(name, description, required: nil, types: nil)
  types = types&.collect { |type| type.is_a?(Numeric) ? type : CHANNEL_TYPES[type] }
  option(TYPES[:channel], name, description, required: required, channel_types: types)
end

#integer(name, description, required: nil, min_value: nil, max_value: nil, choices: nil, autocomplete: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

  • min_value (Integer) (defaults to: nil)

    A minimum value for option.

  • max_value (Integer) (defaults to: nil)

    A maximum value for option.

  • choices (Hash, nil) (defaults to: nil)

    Available choices, mapped as Name => Value.

  • autocomplete (true, false) (defaults to: nil)

    Whether this option can dynamically show choices.

Returns:

  • Hash



604
605
606
607
# File 'lib/discordrb/data/interaction.rb', line 604

def integer(name, description, required: nil, min_value: nil, max_value: nil, choices: nil, autocomplete: nil)
  option(TYPES[:integer], name, description,
         required: required, min_value: min_value, max_value: max_value, choices: choices, autocomplete: autocomplete)
end

#mentionable(name, description, required: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

Returns:

  • Hash



647
648
649
# File 'lib/discordrb/data/interaction.rb', line 647

def mentionable(name, description, required: nil)
  option(TYPES[:mentionable], name, description, required: required)
end

#number(name, description, required: nil, min_value: nil, max_value: nil, choices: nil, autocomplete: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

  • min_value (Float) (defaults to: nil)

    A minimum value for option.

  • max_value (Float) (defaults to: nil)

    A maximum value for option.

  • autocomplete (true, false) (defaults to: nil)

    Whether this option can dynamically show choices.

Returns:

  • Hash



658
659
660
661
# File 'lib/discordrb/data/interaction.rb', line 658

def number(name, description, required: nil, min_value: nil, max_value: nil, choices: nil, autocomplete: nil)
  option(TYPES[:number], name, description,
         required: required, min_value: min_value, max_value: max_value, choices: choices, autocomplete: autocomplete)
end

#role(name, description, required: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

Returns:

  • Hash



639
640
641
# File 'lib/discordrb/data/interaction.rb', line 639

def role(name, description, required: nil)
  option(TYPES[:role], name, description, required: required)
end

#string(name, description, required: nil, min_length: nil, max_length: nil, choices: nil, autocomplete: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

  • min_length (Integer) (defaults to: nil)

    A minimum length for option value.

  • max_length (Integer) (defaults to: nil)

    A maximum length for option value.

  • choices (Hash, nil) (defaults to: nil)

    Available choices, mapped as Name => Value.

  • autocomplete (true, false) (defaults to: nil)

    Whether this option can dynamically show choices.

Returns:

  • Hash



591
592
593
594
# File 'lib/discordrb/data/interaction.rb', line 591

def string(name, description, required: nil, min_length: nil, max_length: nil, choices: nil, autocomplete: nil)
  option(TYPES[:string], name, description,
         required: required, min_length: min_length, max_length: max_length, choices: choices, autocomplete: autocomplete)
end

#subcommand(name, description) {|| ... } ⇒ Object

Returns Hash.

Examples:

bot.register_application_command(:test, 'Test command') do |cmd|
  cmd.subcommand(:echo) do |sub|
    sub.string('message', 'What to echo back', required: true)
  end
end

Parameters:

  • name (String, Symbol)

    The name of the subcommand.

  • description (String)

    A description of the subcommand.

Yield Parameters:

Returns:

  • Hash



557
558
559
560
561
562
# File 'lib/discordrb/data/interaction.rb', line 557

def subcommand(name, description)
  builder = OptionBuilder.new
  yield builder if block_given?

  option(TYPES[:subcommand], name, description, options: builder.to_a)
end

#subcommand_group(name, description) {|| ... } ⇒ Object

Returns Hash.

Examples:

bot.register_application_command(:test, 'Test command') do |cmd|
  cmd.subcommand_group(:fun) do |group|
    group.subcommand(:8ball) do |sub|
      sub.string(:question, 'What do you ask the mighty 8ball?')
    end
  end
end

Parameters:

  • name (String, Symbol)

    The name of the subcommand group.

  • description (String)

    A description of the subcommand group.

Yield Parameters:

Returns:

  • Hash



576
577
578
579
580
581
# File 'lib/discordrb/data/interaction.rb', line 576

def subcommand_group(name, description)
  builder = OptionBuilder.new
  yield builder

  option(TYPES[:subcommand_group], name, description, options: builder.to_a)
end

#to_aArray<Hash>

Returns:

  • (Array<Hash>)


697
698
699
# File 'lib/discordrb/data/interaction.rb', line 697

def to_a
  @options
end

#user(name, description, required: nil) ⇒ Object

Returns Hash.

Parameters:

  • name (String, Symbol)

    The name of the argument.

  • description (String)

    A description of the argument.

  • required (true, false) (defaults to: nil)

    Whether this option must be provided.

Returns:

  • Hash



621
622
623
# File 'lib/discordrb/data/interaction.rb', line 621

def user(name, description, required: nil)
  option(TYPES[:user], name, description, required: required)
end