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)


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

def attributes
  @attributes
end

#nameSymbol (readonly)


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.


76
77
78
79
80
81
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
# File 'lib/discordrb/commands/parser.rb', line 76

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

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

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

  raise exception
end