Class: DiscordRDA::CommandBuilder
- Inherits:
-
Object
- Object
- DiscordRDA::CommandBuilder
- Defined in:
- lib/discord_rda/interactions/application_command.rb
Overview
Builder for creating application commands with DSL
Instance Method Summary collapse
-
#attachment(name, description:, required: false) ⇒ Object
Add an attachment option.
-
#boolean(name, description:, required: false) ⇒ Object
Add a boolean option.
-
#build ⇒ ApplicationCommand
Build and return ApplicationCommand.
-
#channel(name, description:, required: false, channel_types: nil) ⇒ Object
Add a channel option.
- #circuit_breaker(failures:, cooldown:) ⇒ Object
-
#default_permissions(permissions) ⇒ Object
Set default member permissions.
-
#dm_allowed(allowed = true) ⇒ Object
Set DM permission.
- #execution_policy(**policy) ⇒ Object
-
#group(name, description) {|CommandBuilder| ... } ⇒ Object
Add a subcommand group.
-
#handler {|Interaction| ... } ⇒ Object
Define the handler block.
-
#initialize(name, description = nil, type: 1) ⇒ CommandBuilder
constructor
A new instance of CommandBuilder.
-
#integer(name, description:, required: false, choices: nil, min_value: nil, max_value: nil, autocomplete: false) ⇒ Object
Add an integer option.
-
#localized_description(locale, description) ⇒ Object
Set localized description.
-
#localized_name(locale, name) ⇒ Object
Set localized name.
- #max_concurrency(value) ⇒ Object
-
#mentionable(name, description:, required: false) ⇒ Object
Add a mentionable option (user or role).
-
#nsfw(nsfw = true) ⇒ Object
Set NSFW flag.
-
#number(name, description:, required: false, choices: nil, min_value: nil, max_value: nil, autocomplete: false) ⇒ Object
Add a number (float) option.
-
#role(name, description:, required: false) ⇒ Object
Add a role option.
-
#string(name, description:, required: false, choices: nil, min_length: nil, max_length: nil, autocomplete: false) ⇒ Object
Add a string option.
-
#subcommand(name, description) {|CommandBuilder| ... } ⇒ Object
Add a subcommand.
- #timeout(seconds) ⇒ Object
-
#to_h ⇒ Hash
Convert to hash for API.
-
#type(type) ⇒ self
Set command type.
-
#user(name, description:, required: false) ⇒ Object
Add a user option.
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
355 356 357 358 |
# File 'lib/discord_rda/interactions/application_command.rb', line 355 def (name, description:, required: false) @options << build_option(11, name, description, required: required) self end |
#boolean(name, description:, required: false) ⇒ Object
Add a boolean option
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 |
#build ⇒ ApplicationCommand
Build and return ApplicationCommand
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
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
384 385 386 387 388 389 390 391 |
# File 'lib/discord_rda/interactions/application_command.rb', line 384 def () @default_member_permissions = if .is_a?(Array) .map { |p| ApplicationCommand::PERMISSIONS[p] || p }.reduce(:|).to_s else .to_s end self end |
#dm_allowed(allowed = true) ⇒ Object
Set DM permission
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
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
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
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
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
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)
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
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
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
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
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
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_h ⇒ Hash
Convert to hash for API
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
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
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 |