class Discordrb::Interactions::OptionBuilder

A builder for defining slash commands options.

Constants

CHANNEL_TYPES

Channel types that can be provided to channel

TYPES

@!visibility private

Attributes

options[R]

@return [Array<Hash>]

Public Class Methods

new() click to toggle source

@!visibility private

# File lib/discordrb/data/interaction.rb, line 426
def initialize
  @options = []
end

Public Instance Methods

attachment(name, description, required: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @return (see option)

# File lib/discordrb/data/interaction.rb, line 539
def attachment(name, description, required: nil)
  option(TYPES[:attachment], name, description, required: required)
end
boolean(name, description, required: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @return (see option)

# File lib/discordrb/data/interaction.rb, line 488
def boolean(name, description, required: nil)
  option(TYPES[:boolean], name, description, required: required)
end
channel(name, description, required: nil, types: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @param types [Array<Symbol, Integer>] See {CHANNEL_TYPES} @return (see option)

# File lib/discordrb/data/interaction.rb, line 505
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, choices: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @param choices [Hash, nil] Available choices, mapped as ‘Name => Value`. @return (see option)

# File lib/discordrb/data/interaction.rb, line 480
def integer(name, description, required: nil, choices: nil)
  option(TYPES[:integer], name, description, required: required, choices: choices)
end
mentionable(name, description, required: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @return (see option)

# File lib/discordrb/data/interaction.rb, line 522
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) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @return (see option)

# File lib/discordrb/data/interaction.rb, line 530
def number(name, description, required: nil, min_value: nil, max_value: nil, choices: nil)
  option(TYPES[:number], name, description,
         required: required, min_value: min_value, max_value: max_value, choices: choices)
end
option(type, name, description, required: nil, choices: nil, options: nil, min_value: nil, max_value: nil, channel_types: nil) click to toggle source

@!visibility private @param type [Integer] The argument type. @param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @param min_value [Integer, Float] A minimum value for integer and number options. @param max_value [Integer, Float] A maximum value for integer and number options. @param channel_types [Array<Integer>] Channel types that can be provides for channel options. @return Hash

# File lib/discordrb/data/interaction.rb, line 552
def option(type, name, description, required: nil, choices: nil, options: nil, min_value: nil, max_value: nil,
           channel_types: nil)
  opt = { type: type, name: name, description: description }
  choices = choices.map { |option_name, value| { name: option_name, value: value } } if choices

  opt.merge!({ required: required, choices: choices, options: options, min_value: min_value,
               max_value: max_value, channel_types: channel_types }.compact)

  @options << opt
  opt
end
role(name, description, required: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @return (see option)

# File lib/discordrb/data/interaction.rb, line 514
def role(name, description, required: nil)
  option(TYPES[:role], name, description, required: required)
end
string(name, description, required: nil, choices: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @param choices [Hash, nil] Available choices, mapped as ‘Name => Value`. @return (see option)

# File lib/discordrb/data/interaction.rb, line 471
def string(name, description, required: nil, choices: nil)
  option(TYPES[:string], name, description, required: required, choices: choices)
end
subcommand(name, description) { |builder| ... } click to toggle source

@param name [String, Symbol] The name of the subcommand. @param description [String] A description of the subcommand. @yieldparam [OptionBuilder] @return (see option) @example

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
# File lib/discordrb/data/interaction.rb, line 440
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) { |builder| ... } click to toggle source

@param name [String, Symbol] The name of the subcommand group. @param description [String] A description of the subcommand group. @yieldparam [OptionBuilder] @return (see option) @example

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
# File lib/discordrb/data/interaction.rb, line 459
def subcommand_group(name, description)
  builder = OptionBuilder.new
  yield builder

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

@return [Array<Hash>]

# File lib/discordrb/data/interaction.rb, line 565
def to_a
  @options
end
user(name, description, required: nil) click to toggle source

@param name [String, Symbol] The name of the argument. @param description [String] A description of the argument. @param required [true, false] Whether this option must be provided. @return (see option)

# File lib/discordrb/data/interaction.rb, line 496
def user(name, description, required: nil)
  option(TYPES[:user], name, description, required: required)
end