Class: Discordrb::Commands::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/commands/parser.rb

Overview

Command that can be called in a chain

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesHash (readonly)

Returns the attributes the command was initialized with.

Returns:

  • (Hash)

    the attributes the command was initialized with



7
8
9
# File 'lib/discordrb/commands/parser.rb', line 7

def attributes
  @attributes
end

#nameSymbol (readonly)

Returns the name of this command.

Returns:

  • (Symbol)

    the name of this command



10
11
12
# File 'lib/discordrb/commands/parser.rb', line 10

def name
  @name
end

Instance Method Details

#call(event, arguments, chained = false, check_permissions = true) ⇒ String

Calls this command and executes the code inside.

Parameters:

  • event (CommandEvent)

    The event to call the command with.

  • arguments (Array<String>)

    The attributes for the command.

  • chained (true, false) (defaults to: false)

    Whether or not this command is part of a command chain.

  • check_permissions (true, false) (defaults to: true)

    Whether the user’s permission to execute the command (i.e. rate limits) should be checked.

Returns:

  • (String)

    the result of the execution.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/discordrb/commands/parser.rb', line 82

def call(event, arguments, chained = false, check_permissions = true)
  if arguments.length < @attributes[:min_args]
    response = "Too few arguments for command `#{name}`!"
    response += "\nUsage: `#{@attributes[:usage]}`" if @attributes[:usage]
    event.respond(response)
    return
  end
  if @attributes[:max_args] >= 0 && arguments.length > @attributes[:max_args]
    response = "Too many arguments for command `#{name}`!"
    response += "\nUsage: `#{@attributes[:usage]}`" if @attributes[:usage]
    event.respond(response)
    return
  end
  unless @attributes[:chain_usable] && !chained
    event.respond "Command `#{name}` cannot be used in a command chain!"
    return
  end

  if check_permissions
    rate_limited = event.bot.rate_limited?(@attributes[:bucket], event.author)
    if @attributes[:bucket] && rate_limited
      event.respond @attributes[:rate_limit_message].gsub('%time%', rate_limited.round(2).to_s) if @attributes[:rate_limit_message]
      return
    end
  end

  result = @block.call(event, *arguments)
  event.drain_into(result)
rescue LocalJumpError => e # occurs when breaking
  result = e.exit_value
  event.drain_into(result)
rescue StandardError => e # Something went wrong inside our @block!
  rescue_value = @attributes[:rescue] || event.bot.attributes[:rescue]
  if rescue_value
    event.respond(rescue_value.gsub('%exception%', e.message)) if rescue_value.is_a?(String)
    rescue_value.call(event, e) if rescue_value.respond_to?(:call)
  end

  raise e
end