Class: DiscordRDA::CommandContext

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

Overview

Context for command execution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interaction, system) ⇒ CommandContext

Returns a new instance of CommandContext.



398
399
400
401
402
403
404
405
# File 'lib/discord_rda/interactions/command_system.rb', line 398

def initialize(interaction, system)
  @interaction = interaction
  @system = system
  @user = interaction.user
  @member = interaction.member
  @guild = nil # Would need to fetch from cache
  @channel = nil # Would need to fetch from cache
end

Instance Attribute Details

#channelChannel (readonly)

Returns The channel where the command was invoked.

Returns:

  • (Channel)

    The channel where the command was invoked



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

def channel
  @channel
end

#guildGuild? (readonly)

Returns The guild where the command was invoked.

Returns:

  • (Guild, nil)

    The guild where the command was invoked



393
394
395
# File 'lib/discord_rda/interactions/command_system.rb', line 393

def guild
  @guild
end

#interactionInteraction (readonly)

Returns The interaction.

Returns:



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

def interaction
  @interaction
end

#memberMember? (readonly)

Returns The member who invoked the command.

Returns:

  • (Member, nil)

    The member who invoked the command



390
391
392
# File 'lib/discord_rda/interactions/command_system.rb', line 390

def member
  @member
end

#systemCommandSystem (readonly)

Returns The command system.

Returns:



384
385
386
# File 'lib/discord_rda/interactions/command_system.rb', line 384

def system
  @system
end

#userUser (readonly)

Returns The user who invoked the command.

Returns:

  • (User)

    The user who invoked the command



387
388
389
# File 'lib/discord_rda/interactions/command_system.rb', line 387

def user
  @user
end

Instance Method Details

#channel_idString

Get channel ID

Returns:

  • (String)

    Channel ID



448
449
450
# File 'lib/discord_rda/interactions/command_system.rb', line 448

def channel_id
  interaction.channel_id&.to_s
end

#defer(ephemeral: false) ⇒ Object

Defer the response

Parameters:

  • ephemeral (Boolean) (defaults to: false)

    Whether to make response ephemeral



461
462
463
# File 'lib/discord_rda/interactions/command_system.rb', line 461

def defer(ephemeral: false)
  interaction.defer(ephemeral: ephemeral)
end

#dm?Boolean

Check if this is a DM context

Returns:

  • (Boolean)

    True if in DM



480
481
482
# File 'lib/discord_rda/interactions/command_system.rb', line 480

def dm?
  guild_id.nil?
end

#followup(content = nil, **options, &block) ⇒ Object

Send a followup message

Parameters:

  • content (String) (defaults to: nil)

    Message content

  • options (Hash)

    Message options



468
469
470
# File 'lib/discord_rda/interactions/command_system.rb', line 468

def followup(content = nil, **options, &block)
  interaction.followup(content, **options, &block)
end

#guild?Boolean

Check if this is a guild context

Returns:

  • (Boolean)

    True if in guild



474
475
476
# File 'lib/discord_rda/interactions/command_system.rb', line 474

def guild?
  !guild_id.nil?
end

#guild_idString?

Get guild ID

Returns:

  • (String, nil)

    Guild ID



442
443
444
# File 'lib/discord_rda/interactions/command_system.rb', line 442

def guild_id
  interaction.guild_id&.to_s
end

#option(name) ⇒ Object

Get a specific option value

Parameters:

  • name (String)

    Option name

Returns:

  • (Object)

    Option value



416
417
418
# File 'lib/discord_rda/interactions/command_system.rb', line 416

def option(name)
  options[name.to_s]
end

#optionsHash

Get command options

Returns:

  • (Hash)

    Option name to value mapping



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

def options
  interaction.options || {}
end

#respond(content = nil, **options, &block) ⇒ Object

Respond to the interaction

Parameters:

  • content (String) (defaults to: nil)

    Message content

  • options (Hash)

    Response options



455
456
457
# File 'lib/discord_rda/interactions/command_system.rb', line 455

def respond(content = nil, **options, &block)
  interaction.respond(content, **options, &block)
end

#subcommandString?

Get subcommand name

Returns:

  • (String, nil)

    Subcommand name



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

def subcommand
  data = interaction.command_data
  return nil unless data && data['options']

  sub = data['options'].find { |opt| opt['type'] == 1 }
  sub&.dig('name')
end

#subcommand_groupString?

Get subcommand group name

Returns:

  • (String, nil)

    Group name



432
433
434
435
436
437
438
# File 'lib/discord_rda/interactions/command_system.rb', line 432

def subcommand_group
  data = interaction.command_data
  return nil unless data && data['options']

  group = data['options'].find { |opt| opt['type'] == 2 }
  group&.dig('name')
end