Class: DiscordRDA::Subcommand

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

Overview

Represents a subcommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, options:, permissions:, handler:, parent:) ⇒ Subcommand

Returns a new instance of Subcommand.



329
330
331
332
333
334
335
336
# File 'lib/discord_rda/interactions/command_system.rb', line 329

def initialize(name:, description:, options:, permissions:, handler:, parent:)
  @name = name.to_s
  @description = description
  @options = options
  @permissions = permissions
  @handler = handler
  @parent = parent
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



327
328
329
# File 'lib/discord_rda/interactions/command_system.rb', line 327

def description
  @description
end

#handlerObject (readonly)

Returns the value of attribute handler.



327
328
329
# File 'lib/discord_rda/interactions/command_system.rb', line 327

def handler
  @handler
end

#nameObject (readonly)

Returns the value of attribute name.



327
328
329
# File 'lib/discord_rda/interactions/command_system.rb', line 327

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



327
328
329
# File 'lib/discord_rda/interactions/command_system.rb', line 327

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



327
328
329
# File 'lib/discord_rda/interactions/command_system.rb', line 327

def parent
  @parent
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



327
328
329
# File 'lib/discord_rda/interactions/command_system.rb', line 327

def permissions
  @permissions
end

Instance Method Details

#check_permissions(context) ⇒ Object



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

def check_permissions(context)
  return true if permissions.empty?
  return true unless context.member

  permissions.all? { |perm| context.member.permissions&.send("#{perm}?") }
end

#execute(context) ⇒ Object

Execute subcommand

Parameters:



340
341
342
343
344
345
346
347
# File 'lib/discord_rda/interactions/command_system.rb', line 340

def execute(context)
  # Check permissions
  unless check_permissions(context)
    return context.respond(content: "You don't have permission to use this subcommand.", ephemeral: true)
  end

  handler.call(context)
end

#to_option_formatObject



356
357
358
359
360
361
362
363
# File 'lib/discord_rda/interactions/command_system.rb', line 356

def to_option_format
  {
    type: 1, # SUB_COMMAND
    name: name,
    description: description,
    options: options
  }
end