Class: OnyxCord::Interactions::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/onyxcord/interactions/option.rb

Constant Summary collapse

OPTION_TYPES =
{
  subcommand: 1,
  subcommand_group: 2,
  string: 3,
  integer: 4,
  boolean: 5,
  user: 6,
  channel: 7,
  role: 8,
  mentionable: 9,
  number: 10,
  attachment: 11
}.freeze
SKIP_OPTION_TYPES =
%i[subcommand subcommand_group].freeze
OPTION_METHODS =
OPTION_TYPES.each_with_object({}) do |(name, value), hash|
  next if SKIP_OPTION_TYPES.include?(name)

  hash[name] = value
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, type, **attributes, &block) ⇒ Option

Returns a new instance of Option.



30
31
32
33
34
35
36
37
38
39
# File 'lib/onyxcord/interactions/option.rb', line 30

def initialize(name, description, type, **attributes, &block)
  @name = name.to_s
  @description = description
  @type = type
  @attributes = attributes
  @options = []
  @block = block

  instance_eval(&@block) if @block && type == :subcommand
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/onyxcord/interactions/option.rb', line 6

def attributes
  @attributes
end

#descriptionObject (readonly)

Returns the value of attribute description.



6
7
8
# File 'lib/onyxcord/interactions/option.rb', line 6

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/onyxcord/interactions/option.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/onyxcord/interactions/option.rb', line 6

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/onyxcord/interactions/option.rb', line 6

def type
  @type
end

Instance Method Details

#subcommand(name, description, **attrs, &block) ⇒ Object



73
74
75
76
77
# File 'lib/onyxcord/interactions/option.rb', line 73

def subcommand(name, description, **attrs, &block)
  sub = Option.new(name, description, :subcommand, **attrs, &block)
  @options << sub
  sub
end

#to_hObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/onyxcord/interactions/option.rb', line 41

def to_h
  data = {
    name: @name,
    description: @description,
    type: OPTION_TYPES[@type] || @type
  }

  data[:name_localizations] = @attributes[:name_localizations] if @attributes[:name_localizations]
  data[:description_localizations] = @attributes[:description_localizations] if @attributes[:description_localizations]
  data[:required] = @attributes[:required] unless @attributes[:required].nil?
  data[:min_length] = @attributes[:min_length] if @attributes[:min_length]
  data[:max_length] = @attributes[:max_length] if @attributes[:max_length]
  data[:min_value] = @attributes[:min_value] if @attributes[:min_value]
  data[:max_value] = @attributes[:max_value] if @attributes[:max_value]
  data[:autocomplete] = @attributes[:autocomplete] unless @attributes[:autocomplete].nil?
  data[:channel_types] = @attributes[:channel_types] if @attributes[:channel_types]

  if @attributes[:choices]
    choice_localizations = @attributes[:choice_localizations] || {}
    data[:choices] = @attributes[:choices].map do |choice_name, value|
      choice = { name: choice_name.to_s, value: value }
      locs = choice_localizations[choice_name] || choice_localizations[choice_name.to_s]
      choice[:name_localizations] = locs if locs
      choice
    end
  end

  data[:options] = @options.map(&:to_h) unless @options.empty?

  data
end