Module: Discorb::ApplicationCommand::Handler

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

Overview

Module to handle application commands.

Instance Method Summary collapse

Instance Method Details

#clear_commands(token, guild_ids) -> Object

Note:

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

Claer commands in specified guilds.

Parameters:

  • token (String)

    Bot token.

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

    Guild IDs to clear.

See Also:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/discorb/app_command/handler.rb', line 232

def clear_commands(token, guild_ids)
  Async do
    @token ||= token
    @http = HTTP.new(self)
    app_info = fetch_application.wait

    guild_ids.each do |guild_id|
      @http.request(
        Route.new("/applications/#{app_info.id}/guilds/#{guild_id}/commands",
                  "//applications/:application_id/guilds/:guild_id/commands",
                  :put),
        []
      ).wait
    end
    sputs "Cleared commands for #{guild_ids.length} guilds." if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
  end
end

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

Add message context menu command.

Parameters:

  • command_name (String, Hash{Symbol => 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.

  • dm_permission (Boolean) (defaults to: true)

    Whether the command is available in DM.

  • default_permission (Discorb::Permission) (defaults to: nil)

    The default permission of the command.

  • block (Proc)

    Command block.

Yields:

  • (interaction, message)

    Block to execute.

Yield Parameters:

Returns:



126
127
128
129
130
131
132
133
# File 'lib/discorb/app_command/handler.rb', line 126

def message_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
  command_name = { default: command_name } if command_name.is_a?(String)
  command_name = ApplicationCommand.modify_localization_hash(command_name)
  command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 3, dm_permission,
                                                     default_permission)
  @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:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/discorb/app_command/handler.rb', line 170

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
    unless global_commands.empty?
      @http.request(
        Route.new(
          "/applications/#{app_info.id}/commands",
          "//applications/:application_id/commands",
          :put
        ),
        global_commands.map(&:to_hash)
      ).wait
    end
    if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
      sputs "Registered commands for global:"
      global_commands.each do |command|
        iputs "- #{command.name["default"]}"
      end
    end
    unless final_guild_ids.empty?
      final_guild_ids.each do |guild_id|
        commands = local_commands.select { |c| c.guild_ids.include?(guild_id) }
        @http.request(
          Route.new("/applications/#{app_info.id}/guilds/#{guild_id}/commands",
                    "//applications/:application_id/guilds/:guild_id/commands",
                    :put),
          commands.map(&:to_hash)
        ).wait
        sputs "Registered commands for #{guild_id}:"
        commands.each do |command|
          iputs "- #{command.name["default"]}"
        end
      end
    end
    @logger.info "Successfully setup commands"
  end
end

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

Add new top-level command.

You can use _ instead of - in language code.

Parameters:

  • command_name (String, Hash{Symbol => String})

    Command name. If hash is passed, it must be a pair of Language code and Command name, and :default key is required. You can use _ instead of - in language code.

  • description (String, Hash{Symbol => String})

    Command description. If hash is passed, it must be a pair of Language code and Command description, and :default key is required.

  • 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 | | — | — | — | | :name_localizations | Hash=> String | Localizations of option name. | | :description | String | Hash{Symbol => String} | Description of the option. If hash is passed, it must be a pair of Language code and description, and :default key is required. You can use _ instead of - in language code. | | :required | Boolean(true | false) | Whether the argument is required. optional will be used if not specified. | | :optional | Boolean(true | false) | Whether the argument is optional. required will be used if not specified. | | :type | Object | Type of the option. | | :choices | Hash{String => String, Integer, Float} | Type of the option. | | :choices_localizations | Hash{String => Hash{Symbol => String}} | Localization of the choice. Key must be the name of a choice. | | :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.

  • dm_permission (Boolean) (defaults to: true)

    Whether the command is available in DM.

  • default_permission (Discorb::Permission) (defaults to: nil)

    The default permission of the command.

  • block (Proc)

    Command block.

Returns:

See Also:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/discorb/app_command/handler.rb', line 52

def slash(
  command_name,
  description,
  options = {},
  guild_ids: nil,
  dm_permission: true,
  default_permission: nil,
  &block
)
  command_name = { default: command_name } if command_name.is_a?(String)
  description = { default: description } if description.is_a?(String)
  command_name = ApplicationCommand.modify_localization_hash(command_name)
  description = ApplicationCommand.modify_localization_hash(description)
  command = Discorb::ApplicationCommand::Command::ChatInputCommand.new(
    command_name,
    description,
    options,
    guild_ids,
    block,
    1,
    nil,
    dm_permission,
    default_permission
  )
  @commands << command
  @callable_commands << command
  command
end

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

Add new command with group.

Parameters:

  • command_name (String, Hash{Symbol => String})

    Command name.

  • description (String, Hash{Symbol => 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.

  • dm_permission (Boolean) (defaults to: true)

    Whether the command is available in DM.

  • default_permission (Discorb::Permission) (defaults to: nil)

    The default permission of the command.

Yields:

  • Block to yield with the command.

Yield Parameters:

Returns:

See Also:



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/discorb/app_command/handler.rb', line 99

def slash_group(command_name, description, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
  command_name = { default: command_name } if command_name.is_a?(String)
  description = { default: description } if description.is_a?(String)
  command_name = ApplicationCommand.modify_localization_hash(command_name)
  description = ApplicationCommand.modify_localization_hash(description)
  command = Discorb::ApplicationCommand::Command::GroupCommand.new(command_name, description, guild_ids, nil,
                                                                   self, dm_permission, default_permission)
  command.then(&block) if block_given?
  @commands << command
  command
end

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

Add user context menu command.

Parameters:

  • command_name (String, Hash{Symbol => 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.

  • dm_permission (Boolean) (defaults to: true)

    Whether the command is available in DM.

  • default_permission (Discorb::Permission) (defaults to: nil)

    The default permission of the command.

  • block (Proc)

    Command block.

Yields:

  • (interaction, user)

    Block to execute.

Yield Parameters:

Returns:



150
151
152
153
154
155
156
157
# File 'lib/discorb/app_command/handler.rb', line 150

def user_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
  command_name = { default: command_name } if command_name.is_a?(String)
  command_name = ApplicationCommand.modify_localization_hash(command_name)
  command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 2, dm_permission,
                                                     default_permission)
  @commands << command
  command
end