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.
-
#default_permissions(permissions) ⇒ Object
Set default member permissions.
-
#dm_allowed(allowed = true) ⇒ Object
Set DM permission.
-
#group(name, description) {|CommandBuilder| ... } ⇒ Object
Add a subcommand group.
-
#handler {|Interaction| ... } ⇒ Object
Define the handler block.
-
#initialize(name, description = nil) ⇒ 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.
-
#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.
-
#to_h ⇒ Hash
Convert to hash for API.
-
#user(name, description:, required: false) ⇒ Object
Add a user option.
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
339 340 341 342 |
# File 'lib/discord_rda/interactions/application_command.rb', line 339 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
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 |
#build ⇒ ApplicationCommand
Build and return ApplicationCommand
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
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
370 371 372 373 374 375 376 377 |
# File 'lib/discord_rda/interactions/application_command.rb', line 370 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
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
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
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
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
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
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)
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
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
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
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
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
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_h ⇒ Hash
Convert to hash for API
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
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 |