Class: DiscordRDA::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/interactions/application_command.rb

Overview

Builder for creating application commands with DSL

Instance Method Summary collapse

Constructor Details

#initialize(name, description = nil) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/discord_rda/interactions/application_command.rb', line 207

def initialize(name, description = nil)
  @name = name
  @description = description || ''
  @options = []
  @name_localizations = {}
  @description_localizations = {}
  @default_member_permissions = nil
  @dm_permission = true
  @nsfw = false
  @handler = nil
end

Instance Method Details

#attachment(name, description:, required: false) ⇒ Object

Add an attachment option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required



339
340
341
342
# File 'lib/discord_rda/interactions/application_command.rb', line 339

def attachment(name, description:, required: false)
  @options << build_option(11, name, description, required: required)
  self
end

#boolean(name, description:, required: false) ⇒ Object

Add a boolean option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required



274
275
276
277
# File 'lib/discord_rda/interactions/application_command.rb', line 274

def boolean(name, description:, required: false)
  @options << build_option(5, name, description, required: required)
  self
end

#buildApplicationCommand

Build and return ApplicationCommand

Returns:



417
418
419
420
421
# File 'lib/discord_rda/interactions/application_command.rb', line 417

def build
  cmd = ApplicationCommand.new(to_h)
  cmd.handler = @handler
  cmd
end

#channel(name, description:, required: false, channel_types: nil) ⇒ Object

Add a channel option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required

  • channel_types (Array<Integer>) (defaults to: nil)

    Allowed channel types



293
294
295
296
297
298
# File 'lib/discord_rda/interactions/application_command.rb', line 293

def channel(name, description:, required: false, channel_types: nil)
  option = build_option(7, name, description, required: required)
  option[:channel_types] = channel_types if channel_types
  @options << option
  self
end

#default_permissions(permissions) ⇒ Object

Set default member permissions

Parameters:

  • permissions (Integer, Array<Symbol>)

    Permission bits or symbols



370
371
372
373
374
375
376
377
# File 'lib/discord_rda/interactions/application_command.rb', line 370

def default_permissions(permissions)
  @default_member_permissions = if permissions.is_a?(Array)
    permissions.map { |p| ApplicationCommand::PERMISSIONS[p] || p }.reduce(:|).to_s
  else
    permissions.to_s
  end
  self
end

#dm_allowed(allowed = true) ⇒ Object

Set DM permission

Parameters:

  • allowed (Boolean) (defaults to: true)

    Whether command works in DMs



381
382
383
384
# File 'lib/discord_rda/interactions/application_command.rb', line 381

def dm_allowed(allowed = true)
  @dm_permission = allowed
  self
end

#group(name, description) {|CommandBuilder| ... } ⇒ Object

Add a subcommand group

Parameters:

  • name (String)

    Group name

  • description (String)

    Group description

Yields:



360
361
362
363
364
365
366
# File 'lib/discord_rda/interactions/application_command.rb', line 360

def group(name, description, &block)
  builder = CommandBuilder.new(name, description)
  builder.instance_variable_set(:@type, 2) # sub_command_group type
  block.call(builder) if block
  @options << builder.to_h
  self
end

#handler {|Interaction| ... } ⇒ Object

Define the handler block

Yields:



395
396
397
398
# File 'lib/discord_rda/interactions/application_command.rb', line 395

def handler(&block)
  @handler = block
  self
end

#integer(name, description:, required: false, choices: nil, min_value: nil, max_value: nil, autocomplete: false) ⇒ Object

Add an integer option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required

  • choices (Array<Hash>) (defaults to: nil)

    Predefined choices

  • min_value (Integer) (defaults to: nil)

    Minimum value

  • max_value (Integer) (defaults to: nil)

    Maximum value



260
261
262
263
264
265
266
267
268
# File 'lib/discord_rda/interactions/application_command.rb', line 260

def integer(name, description:, required: false, choices: nil, min_value: nil, max_value: nil, autocomplete: false)
  option = build_option(4, name, description, required: required)
  option[:choices] = choices if choices
  option[:min_value] = min_value if min_value
  option[:max_value] = max_value if max_value
  option[:autocomplete] = true if autocomplete
  @options << option
  self
end

#localized_description(locale, description) ⇒ Object

Set localized description

Parameters:

  • locale (String)

    Locale code

  • description (String)

    Localized description



230
231
232
233
# File 'lib/discord_rda/interactions/application_command.rb', line 230

def localized_description(locale, description)
  @description_localizations[locale] = description
  self
end

#localized_name(locale, name) ⇒ Object

Set localized name

Parameters:

  • locale (String)

    Locale code (e.g., ‘en-US’, ‘pt-BR’)

  • name (String)

    Localized name



222
223
224
225
# File 'lib/discord_rda/interactions/application_command.rb', line 222

def localized_name(locale, name)
  @name_localizations[locale] = name
  self
end

#mentionable(name, description:, required: false) ⇒ Object

Add a mentionable option (user or role)

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required



313
314
315
316
# File 'lib/discord_rda/interactions/application_command.rb', line 313

def mentionable(name, description:, required: false)
  @options << build_option(9, name, description, required: required)
  self
end

#nsfw(nsfw = true) ⇒ Object

Set NSFW flag

Parameters:

  • nsfw (Boolean) (defaults to: true)

    Whether command is age-restricted



388
389
390
391
# File 'lib/discord_rda/interactions/application_command.rb', line 388

def nsfw(nsfw = true)
  @nsfw = nsfw
  self
end

#number(name, description:, required: false, choices: nil, min_value: nil, max_value: nil, autocomplete: false) ⇒ Object

Add a number (float) option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required

  • choices (Array<Hash>) (defaults to: nil)

    Predefined choices

  • min_value (Float) (defaults to: nil)

    Minimum value

  • max_value (Float) (defaults to: nil)

    Maximum value



325
326
327
328
329
330
331
332
333
# File 'lib/discord_rda/interactions/application_command.rb', line 325

def number(name, description:, required: false, choices: nil, min_value: nil, max_value: nil, autocomplete: false)
  option = build_option(10, name, description, required: required)
  option[:choices] = choices if choices
  option[:min_value] = min_value if min_value
  option[:max_value] = max_value if max_value
  option[:autocomplete] = true if autocomplete
  @options << option
  self
end

#role(name, description:, required: false) ⇒ Object

Add a role option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required



304
305
306
307
# File 'lib/discord_rda/interactions/application_command.rb', line 304

def role(name, description:, required: false)
  @options << build_option(8, name, description, required: required)
  self
end

#string(name, description:, required: false, choices: nil, min_length: nil, max_length: nil, autocomplete: false) ⇒ Object

Add a string option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required

  • choices (Array<Hash>) (defaults to: nil)

    Predefined choices

  • min_length (Integer) (defaults to: nil)

    Minimum length

  • max_length (Integer) (defaults to: nil)

    Maximum length

  • autocomplete (Boolean) (defaults to: false)

    Enable autocomplete



243
244
245
246
247
248
249
250
251
# File 'lib/discord_rda/interactions/application_command.rb', line 243

def string(name, description:, required: false, choices: nil, min_length: nil, max_length: nil, autocomplete: false)
  option = build_option(3, name, description, required: required)
  option[:choices] = choices if choices
  option[:min_length] = min_length if min_length
  option[:max_length] = max_length if max_length
  option[:autocomplete] = true if autocomplete
  @options << option
  self
end

#subcommand(name, description) {|CommandBuilder| ... } ⇒ Object

Add a subcommand

Parameters:

  • name (String)

    Subcommand name

  • description (String)

    Subcommand description

Yields:



348
349
350
351
352
353
354
# File 'lib/discord_rda/interactions/application_command.rb', line 348

def subcommand(name, description, &block)
  builder = CommandBuilder.new(name, description)
  builder.instance_variable_set(:@type, 1) # sub_command type
  block.call(builder) if block
  @options << builder.to_h
  self
end

#to_hHash

Convert to hash for API

Returns:

  • (Hash)

    Command hash



402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/discord_rda/interactions/application_command.rb', line 402

def to_h
  {
    name: @name,
    name_localizations: @name_localizations.empty? ? nil : @name_localizations,
    description: @description,
    description_localizations: @description_localizations.empty? ? nil : @description_localizations,
    options: @options.empty? ? nil : @options,
    default_member_permissions: @default_member_permissions,
    dm_permission: @dm_permission,
    nsfw: @nsfw
  }.compact
end

#user(name, description:, required: false) ⇒ Object

Add a user option

Parameters:

  • name (String)

    Option name

  • description (String)

    Option description

  • required (Boolean) (defaults to: false)

    Whether required



283
284
285
286
# File 'lib/discord_rda/interactions/application_command.rb', line 283

def user(name, description:, required: false)
  @options << build_option(6, name, description, required: required)
  self
end