Module: Discorb::Command::Handler

Included in:
Discorb::Client
Defined in:
lib/discorb/command.rb

Overview

Module to handle commands.

Instance Method Summary collapse

Instance Method Details

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

Add message context menu command.

Parameters:

  • command_name (String)

    Command name.

  • guild_ids (Array<#to_s>) (defaults to: [])

    Guild IDs to restrict the command to.

  • block (Proc)

    Command block.

Yields:

  • (interaction, message)

    Block to execute.

Yield Parameters:

Returns:



74
75
76
77
78
# File 'lib/discorb/command.rb', line 74

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

#setup_commands(token = nil) -> Object

Note:

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

Setup commands.

Parameters:

  • token (String) (defaults to: nil)

    Bot token.

See Also:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/discorb/command.rb', line 105

def setup_commands(token = nil)
  Async do
    @token ||= token
    @http = HTTP.new(self)
    global_commands = @commands.select { |c| c.guild_ids.empty? }
    guild_ids = Set[*@commands.map(&:guild_ids).flatten]
    app_info = fetch_application.wait
    http.put("/applications/#{app_info.id}/commands", global_commands.map(&:to_hash)).wait unless global_commands.empty?
    guild_ids.each do |guild_id|
      commands = @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 guild_ids.empty?
    @log.info "Successfully setup commands"
  end
end

#slash(command_name, description, options = {}, guild_ids: [], &block) -> Discorb::Command::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. | | :optional | Boolean | Whether the option is optional or not. | | :type | Object | Type of the option. | | :choice | Hash{String => String, Integer, Float} | Type of the option. |

  • guild_ids (Array<#to_s>) (defaults to: [])

    Guild IDs to restrict the command to.

  • block (Proc)

    Command block.

Returns:

See Also:



34
35
36
37
38
39
# File 'lib/discorb/command.rb', line 34

def slash(command_name, description, options = {}, guild_ids: [], &block)
  command = Discorb::Command::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: []) { ... } ⇒ Discorb::Command::Command::GroupCommand

Add new command with group.

Parameters:

  • command_name (String)

    Command name.

  • description (String)

    Command description.

  • guild_ids (Array<#to_s>) (defaults to: [])

    Guild IDs to restrict the command to.

Yields:

  • Block to execute as the command. It can be used to define sub-commands.

Returns:

See Also:



55
56
57
58
59
60
# File 'lib/discorb/command.rb', line 55

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

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

Add user context menu command.

Parameters:

  • command_name (String)

    Command name.

  • guild_ids (Array<#to_s>) (defaults to: [])

    Guild IDs to restrict the command to.

  • block (Proc)

    Command block.

Yields:

  • (interaction, user)

    Block to execute.

Yield Parameters:

Returns:



92
93
94
95
96
# File 'lib/discorb/command.rb', line 92

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