Module: Discordrb::Commands::CommandContainer

Includes:
RateLimiter
Included in:
CommandBot
Defined in:
lib/discordrb/commands/container.rb

Overview

This module holds a collection of commands that can be easily added to by calling the #command function. Other containers can be included into it as well. This allows for modularization of command bots.

Instance Method Summary collapse

Methods included from RateLimiter

#bucket, #clean, #include_buckets, #rate_limited?

Instance Method Details

#command(name, attributes = {}) {|event| ... } ⇒ Command

Adds a new command to the container.

Parameters:

  • name (Symbol)

    The name of the command to add.

  • attributes (Hash) (defaults to: {})

    The attributes to initialize the command with.

Options Hash (attributes):

  • :permission_level (Integer)

    The minimum permission level that can use this command, inclusive. See Discordrb::Commands::CommandBot#set_user_permission and Discordrb::Commands::CommandBot#set_role_permission.

  • :chain_usable (true, false)

    Whether this command is able to be used inside of a command chain or sub-chain. Typically used for administrative commands that shouldn't be done carelessly.

  • :help_available (true, false)

    Whether this command is visible in the help command. See the :help_command attribute of Discordrb::Commands::CommandBot#initialize.

  • :description (String)

    A short description of what this command does. Will be shown in the help command if the user asks for it.

  • :usage (String)

    A short description of how this command should be used. Will be displayed in the help command or if the user uses it wrong.

  • :min_args (Integer)

    The minimum number of arguments this command should have. If a user attempts to call the command with fewer arguments, the usage information will be displayed, if it exists.

  • :max_args (Integer)

    The maximum number of arguments the command should have.

  • :rate_limit_message (String)

    The message that should be displayed if the command hits a rate limit. None if unspecified or nil. %time% in the message will be replaced with the time in seconds when the command will be available again.

  • :bucket (Symbol)

    The rate limit bucket that should be used for rate limiting. No rate limiting will be done if unspecified or nil.

Yields:

  • The block is executed when the command is executed.

Yield Parameters:

  • event (CommandEvent)

    The event of the message that contained the command.

Returns:

  • (Command)

    The command that was added.



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

def command(name, attributes = {}, &block)
  @commands ||= {}
  if name.is_a? Array
    new_command = nil

    name.each do |e|
      new_command = Command.new(e, attributes, &block)
      @commands[e] = new_command
    end

    new_command
  else
    @commands[name] = Command.new(name, attributes, &block)
  end
end

#include!(container) ⇒ Object

Includes another container into this one.

Parameters:

  • container (Module)

    An EventContainer or CommandContainer that will be included if it can.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/discordrb/commands/container.rb', line 71

def include!(container)
  container_modules = container.singleton_class.included_modules

  # If the container is an EventContainer and we can include it, then do that
  if container_modules.include?(Discordrb::EventContainer) && respond_to?(:include_events)
    include_events(container)
  end

  if container_modules.include? Discordrb::Commands::CommandContainer
    include_commands(container)
    include_buckets(container)
  elsif !container_modules.include? Discordrb::EventContainer
    raise "Could not include! this particular container - ancestors: #{container_modules}"
  end
end

#include_commands(container) ⇒ Object

Adds all commands from another container into this one. Existing commands will be overwritten.

Parameters:



61
62
63
64
65
66
67
# File 'lib/discordrb/commands/container.rb', line 61

def include_commands(container)
  handlers = container.instance_variable_get '@commands'
  return unless handlers

  @commands ||= {}
  @commands.merge! handlers
end

#remove_command(name) ⇒ Object

Removes a specific command from this container.

Parameters:

  • name (Symbol)

    The command to remove.



54
55
56
57
# File 'lib/discordrb/commands/container.rb', line 54

def remove_command(name)
  @commands ||= {}
  @commands.delete name
end