Class: Cogs::Command

Inherits:
Discordrb::Commands::Command
  • Object
show all
Defined in:
lib/commands.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, name, attributes = {}, &block) ⇒ Command

Returns a new instance of Command.



5
6
7
8
9
# File 'lib/commands.rb', line 5

def initialize(bot, name, attributes = {}, &block)
  super(name=name, attributes=attributes, &block=block)
  @bot = bot
  @cog = attributes[:cog] || "None"
end

Instance Attribute Details

#cogObject (readonly)

Returns the value of attribute cog.



11
12
13
# File 'lib/commands.rb', line 11

def cog
  @cog
end

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/commands.rb', line 13

def call(event, arguments, chained = false, check_permissions = true)
  return unless @bot::cached_cogs.include?(@cog) || @cog == "None"
  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]
    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
      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