module Discorb::Command::Handler

Module to handle commands.

Public Instance Methods

message_command(command_name, guild_ids: [], &block) click to toggle source

Add message context menu command.

@param [String] command_name Command name. @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to. @param [Proc] block Command block. @yield [interaction, message] Block to execute. @yieldparam [Discorb::CommandInteraction::UserMenuCommand] Interaction object. @yieldparam [Discorb::Message] user Message object.

@return [Discorb::Command::Command] Command object.

# 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) click to toggle source

Setup commands. @see Client#initialize

@param [String] token Bot token. @note `token` parameter only required if you don't run client.

# 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) click to toggle source

Add new top-level command.

@param [String] command_name Command name. @param [String] description Command description. @param [Hash{String => Hash{:description => String, :optional => Boolean, :type => Object}}] options 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. |

@param [Array<#to_s>] guild_ids Guild IDs to restrict the command to. @param [Proc] block Command block.

@return [Discorb::Command::Command::SlashCommand]

@see file:docs/application_command.md#register-slash-command

# 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: [], &block) click to toggle source

Add new command with group.

@param [String] command_name Command name. @param [String] description Command description. @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to.

@yield Block to execute as the command. It can be used to define sub-commands. @yieldself [Discorb::Command::Command::GroupCommand] Group command.

@return [Discorb::Command::Command::GroupCommand] Command object.

@see file:docs/slash_command.md

# 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) click to toggle source

Add user context menu command.

@param [String] command_name Command name. @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to. @param [Proc] block Command block. @yield [interaction, user] Block to execute. @yieldparam [Discorb::CommandInteraction::UserMenuCommand] Interaction object. @yieldparam [Discorb::User] user User object.

@return [Discorb::Command::Command] Command object.

# 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