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

Constructor Details

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

Returns a new instance of Command.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/discordrb/commands/parser.rb', line 6

def initialize(name, attributes = {}, &block)
  @name = name
  @attributes = {
    # The lowest permission level that can use the command
    permission_level: attributes[:permission_level] || 0,

    # Whether this command is usable in a command chain
    chain_usable: attributes[:chain_usable].nil? ? true : attributes[:chain_usable],

    # Whether this command should show up in the help command
    help_available: attributes[:help_available].nil? ? true : attributes[:help_available],

    # Description (for help command)
    description: attributes[:description] || nil,

    # Usage description (for help command and error messages)
    usage: attributes[:usage] || nil,

    # Minimum number of arguments
    min_args: attributes[:min_args] || 0,

    # Maximum number of arguments (-1 for no limit)
    max_args: attributes[:max_args] || -1
  }

  @block = block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/discordrb/commands/parser.rb', line 4

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/discordrb/commands/parser.rb', line 4

def name
  @name
end

Instance Method Details

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/discordrb/commands/parser.rb', line 34

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
  @block.call(event, *arguments)
end