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) ⇒ String

Calls this command and executes the code inside.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/discordrb/commands/parser.rb', line 53

def call(event, arguments, chained = false)
  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

  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

  @block.call(event, *arguments)
rescue LocalJumpError # occurs when breaking
  nil
end