Module: Discorb::ApplicationCommand::Handler

Included in:
Client
Defined in:
lib/discorb/app_command.rb

Overview

Module to handle application commands.

Instance Method Summary collapse

Instance Method Details

#message_command(command_name, guild_ids: nil, &block) {|interaction, message| ... } ⇒ Discorb::ApplicationCommand::Command

Add message context menu command.

Parameters:

  • command_name (String)

    Command name.

  • guild_ids (Array<#to_s>, false, nil) (defaults to: nil)

    Guild IDs to set the command to. false to global command, nil to use default.

  • block (Proc)

    Command block.

Yields:

  • (interaction, message)

    Block to execute.

Yield Parameters:

  • interaction (Discorb::ApplicationCommandInteraction::UserMenuCommand)

    Interaction object.

  • message (Discorb::Message)

    Message object.

Returns:



81
82
83
84
85
# File 'lib/discorb/app_command.rb', line 81

def message_command(command_name, guild_ids: nil, &block)
  command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 3)
  @commands << command
  command
end

#setup_commands(token = nil, guild_ids: nil) -> Object

Note:

token parameter only required if you don't run client.

Setup commands.

Parameters:

  • token (String) (defaults to: nil)

    Bot token.

  • guild_ids (Array<#to_s>, false, nil) (defaults to: nil)

    Guild IDs to use as default. If false is given, it will be global command.

See Also:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/discorb/app_command.rb', line 114

def setup_commands(token = nil, guild_ids: nil)
  Async do
    @token ||= token
    @http = HTTP.new(self)
    global_commands = @commands.select { |c| c.guild_ids == false or c.guild_ids == [] }
    local_commands = @commands.select { |c| c.guild_ids.is_a?(Array) and c.guild_ids.any? }
    default_commands = @commands.select { |c| c.guild_ids.nil? }
    if guild_ids.is_a?(Array)
      default_commands.each do |command|
        command.instance_variable_set(:@guild_ids, guild_ids)
      end
      local_commands += default_commands
    else
      global_commands += default_commands
    end
    final_guild_ids = local_commands.map(&:guild_ids).flatten.map(&:to_s).uniq
    app_info = fetch_application.wait
    http.put("/applications/#{app_info.id}/commands", global_commands.map(&:to_hash)).wait unless global_commands.empty?
    final_guild_ids.each do |guild_id|
      commands = local_commands.select { |c| c.guild_ids.include?(guild_id) }
      http.put("/applications/#{app_info.id}/guilds/#{guild_id}/commands", commands.map(&:to_hash)).wait
    end unless final_guild_ids.empty?
    @log.info "Successfully setup commands"
  end
end

#slash(command_name, description, options = {}, guild_ids: nil, &block) -> Discorb::ApplicationCommand::Command::SlashCommand

Add new top-level command.

Parameters:

  • command_name (String)

    Command name.

  • description (String)

    Command description.

  • options (Hash{String => Hash{:description => String, :optional => Boolean, :type => Object}}) (defaults to: {})

    Command options. The key is the option name, the value is a hash with the following keys:

    | Key | Type | Description | | — | — | — | | :description | String | Description of the option. | | :required | Whether the argument is required. optional will be used if not specified. | | :optional | Whether the argument is optional. required will be used if not specified. | | :type | Object | Type of the option. | | :choice | Hash{String => String, Integer, Float} | Type of the option. | | :default | Object | Default value of the option. | | :channel_types | Array<Class<Discorb::Channel>> | Type of the channel option. | | :autocomplete | Proc | Autocomplete function. | | :range | Range | Range of the option. Only valid for numeric options. (:int, :float) |

  • guild_ids (Array<#to_s>, false, nil) (defaults to: nil)

    Guild IDs to set the command to. false to global command, nil to use default.

  • block (Proc)

    Command block.

Returns:

See Also:



40
41
42
43
44
45
# File 'lib/discorb/app_command.rb', line 40

def slash(command_name, description, options = {}, guild_ids: nil, &block)
  command = Discorb::ApplicationCommand::Command::SlashCommand.new(command_name, description, options, guild_ids, block, 1, "")
  @commands << command
  @bottom_commands << command
  command
end

#slash_group(command_name, description, guild_ids: nil) {|group| ... } ⇒ Discorb::ApplicationCommand::Command::GroupCommand

Add new command with group.

Parameters:

  • command_name (String)

    Command name.

  • description (String)

    Command description.

  • guild_ids (Array<#to_s>, false, nil) (defaults to: nil)

    Guild IDs to set the command to. false to global command, nil to use default.

Yields:

  • Block to yield with the command.

Yield Parameters:

Returns:

See Also:



62
63
64
65
66
67
# File 'lib/discorb/app_command.rb', line 62

def slash_group(command_name, description, guild_ids: nil, &block)
  command = Discorb::ApplicationCommand::Command::GroupCommand.new(command_name, description, guild_ids, nil, self)
  command.yield_self(&block) if block_given?
  @commands << command
  command
end

#user_command(command_name, guild_ids: nil, &block) {|interaction, user| ... } ⇒ Discorb::ApplicationCommand::Command

Add user context menu command.

Parameters:

  • command_name (String)

    Command name.

  • guild_ids (Array<#to_s>, false, nil) (defaults to: nil)

    Guild IDs to set the command to. false to global command, nil to use default.

  • block (Proc)

    Command block.

Yields:

  • (interaction, user)

    Block to execute.

Yield Parameters:

  • interaction (Discorb::ApplicationCommandInteraction::UserMenuCommand)

    Interaction object.

  • user (Discorb::User)

    User object.

Returns:



99
100
101
102
103
# File 'lib/discorb/app_command.rb', line 99

def user_command(command_name, guild_ids: nil, &block)
  command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 2)
  @commands << command
  command
end