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:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/discorb/app_command/handler.rb', line 281

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
    if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
      sputs "Cleared commands for #{guild_ids.length} guilds."
    end
  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:



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/discorb/app_command/handler.rb', line 143

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:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/discorb/app_command/handler.rb', line 213

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 do |c|
        c.guild_ids.is_a?(Array) and c.guild_ids.any?
      end
    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 { |command| iputs "- #{command.name["default"]}" }
      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) | | :length | Range | Range of length of the option. Only valid for :string. |

  • 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:



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
80
81
82
83
# File 'lib/discorb/app_command/handler.rb', line 55

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:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/discorb/app_command/handler.rb', line 103

def slash_group(
  command_name,
  description,
  guild_ids: nil,
  dm_permission: true,
  default_permission: nil
)
  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,
      self,
      dm_permission,
      default_permission
    )
  yield command 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:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/discorb/app_command/handler.rb', line 180

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