Class: Discorb::AutoComplete

Inherits:
Interaction show all
Defined in:
lib/discorb/interaction.rb

Overview

Represents auto complete interaction.

Instance Attribute Summary

Attributes inherited from Interaction

#application_id, #id, #member, #token, #type, #user, #version

Instance Method Summary collapse

Methods inherited from Interaction

#channel, #guild, #inspect, #target

Methods inherited from DiscordModel

#==, #eql?, #hash

Instance Method Details

#_set_data(data) -> Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/discorb/interaction.rb', line 461

def _set_data(data)
  super
  Sync do
    name = data[:name]
    options = nil
    if (option = data[:options]&.first)
      case option[:type]
      when 1
        name += " #{option[:name]}"
        options = option[:options]
      when 2
        name += " #{option[:name]}"
        if (option_sub = option[:options]&.first)
          if option_sub[:type] == 1
            name += " #{option_sub[:name]}"
            options = option_sub[:options]
          else
            options = option[:options]
          end
        end
      else
        options = data[:options]
      end
    end

    unless (command = @client.bottom_commands.find { |c| c.to_s == name && c.type_raw == 1 })
      @client.log.warn "Unknown command name #{name}, ignoring"
      next
    end

    option_map = command.options.map { |k, v| [k.to_s, v[:default]] }.to_h
    options ||= []
    options.each_with_index do |option|
      val = case option[:type]
        when 3, 4, 5, 10
          option[:value]
        when 6
          guild.members[option[:value]] || guild.fetch_member(option[:value]).wait
        when 7
          guild.channels[option[:value]] || guild.fetch_channels.wait.find { |channel| channel.id == option[:value] }
        when 8
          guild.roles[option[:value]] || guild.fetch_roles.wait.find { |role| role.id == option[:value] }
        when 9
          guild.members[option[:value]] || guild.roles[option[:value]] || guild.fetch_member(option[:value]).wait || guild.fetch_roles.wait.find { |role| role.id == option[:value] }
        end
      option_map[option[:name]] = val
    end
    focused_index = options.find_index { |o| o[:focused] }
    val = command.options.values[focused_index][:autocomplete]&.call(self, *command.options.map { |k, v| option_map[k.to_s] })
    send_complete_result(val)
  end
end

#send_complete_result(val) -> Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/discorb/interaction.rb', line 514

def send_complete_result(val)
  @client.http.post("/interactions/#{@id}/#{@token}/callback", {
    type: 8,
    data: {
      choices: val.map do |vk, vv|
        {
          name: vk,
          value: vv,
        }
      end,
    },
  }).wait
rescue Discorb::NotFoundError
  @client.log.warn "Failed to send auto complete result, This may be caused by the suggestion is taking too long (over 3 seconds) to respond", fallback: $stderr
end