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, type: 1) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/discord_rda/interactions/application_command.rb', line 213

def initialize(name, description = nil, type: 1)
  @name = name
  @description = description || ''
  @type = type
  @options = []
  @name_localizations = {}
  @description_localizations = {}
  @default_member_permissions = nil
  @dm_permission = true
  @nsfw = false
  @handler = nil
  @execution_policy = {}
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



355
356
357
358
# File 'lib/discord_rda/interactions/application_command.rb', line 355

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



290
291
292
293
# File 'lib/discord_rda/interactions/application_command.rb', line 290

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

#buildApplicationCommand

Build and return ApplicationCommand

Returns:



453
454
455
456
457
458
459
# File 'lib/discord_rda/interactions/application_command.rb', line 453

def build
  cmd = ApplicationCommand.new(to_h).dup
  cmd.handler = @handler
  cmd.instance_variable_set(:@execution_policy, @execution_policy.dup)
  cmd.freeze
  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



309
310
311
312
313
314
# File 'lib/discord_rda/interactions/application_command.rb', line 309

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

#circuit_breaker(failures:, cooldown:) ⇒ Object



424
425
426
427
428
# File 'lib/discord_rda/interactions/application_command.rb', line 424

def circuit_breaker(failures:, cooldown:)
  @execution_policy[:failure_threshold] = failures.to_i
  @execution_policy[:cooldown_seconds] = cooldown.to_f
  self
end

#default_permissions(permissions) ⇒ Object

Set default member permissions

Parameters:

  • permissions (Integer, Array<Symbol>)

    Permission bits or symbols



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

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



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

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

#execution_policy(**policy) ⇒ Object



430
431
432
433
# File 'lib/discord_rda/interactions/application_command.rb', line 430

def execution_policy(**policy)
  @execution_policy.merge!(policy)
  self
end

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

Add a subcommand group

Parameters:

  • name (String)

    Group name

  • description (String)

    Group description

Yields:



375
376
377
378
379
380
# File 'lib/discord_rda/interactions/application_command.rb', line 375

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

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

Define the handler block

Yields:



409
410
411
412
# File 'lib/discord_rda/interactions/application_command.rb', line 409

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



276
277
278
279
280
281
282
283
284
# File 'lib/discord_rda/interactions/application_command.rb', line 276

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



246
247
248
249
# File 'lib/discord_rda/interactions/application_command.rb', line 246

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



238
239
240
241
# File 'lib/discord_rda/interactions/application_command.rb', line 238

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

#max_concurrency(value) ⇒ Object



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

def max_concurrency(value)
  @execution_policy[:max_concurrency] = value.to_i
  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



329
330
331
332
# File 'lib/discord_rda/interactions/application_command.rb', line 329

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



402
403
404
405
# File 'lib/discord_rda/interactions/application_command.rb', line 402

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



341
342
343
344
345
346
347
348
349
# File 'lib/discord_rda/interactions/application_command.rb', line 341

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



320
321
322
323
# File 'lib/discord_rda/interactions/application_command.rb', line 320

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



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

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:



364
365
366
367
368
369
# File 'lib/discord_rda/interactions/application_command.rb', line 364

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

#timeout(seconds) ⇒ Object



414
415
416
417
# File 'lib/discord_rda/interactions/application_command.rb', line 414

def timeout(seconds)
  @execution_policy[:timeout_seconds] = seconds.to_f
  self
end

#to_hHash

Convert to hash for API

Returns:

  • (Hash)

    Command hash



437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/discord_rda/interactions/application_command.rb', line 437

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,
    type: @type,
    nsfw: @nsfw
  }.compact
end

#type(type) ⇒ self

Set command type

Parameters:

  • type (Integer, Symbol)

    Discord command type

Returns:

  • (self)


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

def type(type)
  @type = type.is_a?(Symbol) ? ApplicationCommand::TYPES.fetch(type) : type
  self
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



299
300
301
302
# File 'lib/discord_rda/interactions/application_command.rb', line 299

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