class Discordrb::Events::ApplicationCommandEvent

Event for ApplicationCommand interactions.

Constants

Resolved

Struct to allow accessing data via [] or methods.

Attributes

command_id[R]

@return [Integer] The ID of the command.

command_name[R]

@return [String] The name of the command.

options[R]

@return [Hash<Symbol, Object>] Arguments provided to the command, mapped as ‘Name => Value`.

resolved[R]

@return [Resolved]

subcommand[R]

@return [String, nil] The name of the subcommand relevant to this event.

subcommand_group[R]

@return [String, nil] The name of the subcommand group relevant to this event.

target_id[R]

@return [Integer, nil] The target of this command when it is a context command.

Public Class Methods

new(data, bot) click to toggle source
# File lib/discordrb/events/interactions.rb, line 156
def initialize(data, bot)
  super

  command_data = data['data']

  @command_id = command_data['id']
  @command_name = command_data['name'].to_sym

  @target_id = command_data['target_id']&.to_i
  @resolved = Resolved.new({}, {}, {}, {}, {}, {})
  process_resolved(command_data['resolved']) if command_data['resolved']

  options = command_data['options'] || []

  if options.empty?
    @options = {}
    return
  end

  case options[0]['type']
  when 2
    options = options[0]
    @subcommand_group = options['name'].to_sym
    @subcommand = options['options'][0]['name'].to_sym
    options = options['options'][0]['options']
  when 1
    options = options[0]
    @subcommand = options['name'].to_sym
    options = options['options']
  end

  @options = transform_options_hash(options || {})
end

Public Instance Methods

target() click to toggle source

@return [Message, User, nil] The target of this command, for context commands.

# File lib/discordrb/events/interactions.rb, line 191
def target
  return nil unless @target_id

  @resolved.find { |data| data.key?(@target_id) }[@target_id]
end

Private Instance Methods

process_resolved(resolved_data) click to toggle source
# File lib/discordrb/events/interactions.rb, line 199
def process_resolved(resolved_data)
  resolved_data['users']&.each do |id, data|
    @resolved[:users][id.to_i] = @bot.ensure_user(data)
  end

  resolved_data['roles']&.each do |id, data|
    @resolved[:roles][id.to_i] = Discordrb::Role.new(data, @bot)
  end

  resolved_data['channels']&.each do |id, data|
    data['guild_id'] = @interaction.server_id
    @resolved[:channels][id.to_i] = Discordrb::Channel.new(data, @bot)
  end

  resolved_data['members']&.each do |id, data|
    data['user'] = resolved_data['users'][id]
    data['guild_id'] = @interaction.server_id
    @resolved[:members][id.to_i] = Discordrb::Member.new(data, nil, @bot)
  end

  resolved_data['messages']&.each do |id, data|
    @resolved[:messages][id.to_i] = Discordrb::Message.new(data, @bot)
  end

  resolved_data['attachments']&.each do |id, data|
    @resolved[:attachments][id.to_i] = Discordrb::Attachment.new(data, nil, @bot)
  end
end
transform_options_hash(hash) click to toggle source
# File lib/discordrb/events/interactions.rb, line 228
def transform_options_hash(hash)
  hash.to_h { |opt| [opt['name'], opt['options'] || opt['value']] }
end