class Discordrb::Events::ApplicationCommandEventHandler

Event handler for ApplicationCommandEvents.

Attributes

subcommands[R]

@return [Hash]

Public Class Methods

new(attributes, block) click to toggle source

@!visibility private

Calls superclass method Discordrb::Events::EventHandler::new
# File lib/discordrb/events/interactions.rb, line 239
def initialize(attributes, block)
  super

  @subcommands = {}
end

Public Instance Methods

call(event) click to toggle source

@!visibility private @param event [Event]

# File lib/discordrb/events/interactions.rb, line 271
def call(event)
  return unless matches?(event)

  if event.subcommand_group
    unless (cmd = @subcommands.dig(event.subcommand_group, event.subcommand))
      Discordrb::LOGGER.debug("Received an event for an unhandled subcommand `#{event.command_name} #{event.subcommand_group} #{event.subcommand}'")
      return
    end

    cmd.call(event)
  elsif event.subcommand
    unless (cmd = @subcommands[event.subcommand])
      Discordrb::LOGGER.debug("Received an event for an unhandled subcommand `#{event.command_name} #{event.subcommand}'")
      return
    end

    cmd.call(event)
  else
    @block.call(event)
  end
end
group(name) { |builder| ... } click to toggle source

@param name [Symbol, String] @yieldparam [SubcommandBuilder] @return [ApplicationCommandEventHandler]

# File lib/discordrb/events/interactions.rb, line 248
def group(name)
  raise ArgumentError, 'Unable to mix subcommands and groups' if @subcommands.any? { |_, v| v.is_a? Proc }

  builder = SubcommandBuilder.new(name)
  yield builder

  @subcommands.merge!(builder.to_h)
  self
end
matches?(event) click to toggle source

@!visibility private

# File lib/discordrb/events/interactions.rb, line 294
def matches?(event)
  return false unless event.is_a? ApplicationCommandEvent

  [
    matches_all(@attributes[:name], event.command_name) do |a, e|
      a.to_sym == e.to_sym
    end
  ].reduce(true, &:&)
end
subcommand(name, &block) click to toggle source

@param name [String, Symbol] @yieldparam [SubcommandBuilder] @return [ApplicationCommandEventHandler]

# File lib/discordrb/events/interactions.rb, line 261
def subcommand(name, &block)
  raise ArgumentError, 'Unable to mix subcommands and groups' if @subcommands.any? { |_, v| v.is_a? Hash }

  @subcommands[name.to_sym] = block

  self
end