Module: Discordrb::Commands::CommandContainer
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 Attribute Summary collapse
-
#commands ⇒ Hash<Symbol, Command>
readonly
Hash of command names and commands this container has.
Instance Method Summary collapse
-
#command(name, attributes = {}) {|event| ... } ⇒ Command
Adds a new command to the container.
-
#include!(container) ⇒ Object
Includes another container into this one.
-
#include_commands(container) ⇒ Object
Adds all commands from another container into this one.
-
#remove_command(name) ⇒ Object
Removes a specific command from this container.
Methods included from RateLimiter
#bucket, #clean, #include_buckets, #rate_limited?
Instance Attribute Details
#commands ⇒ Hash<Symbol, Command> (readonly)
Returns hash of command names and commands this container has.
13 14 15 |
# File 'lib/discordrb/commands/container.rb', line 13 def commands @commands end |
Instance Method Details
#command(name, attributes = {}) {|event| ... } ⇒ Command
Adds a new command to the container.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/discordrb/commands/container.rb', line 50 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.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/discordrb/commands/container.rb', line 85 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.
75 76 77 78 79 80 81 |
# File 'lib/discordrb/commands/container.rb', line 75 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.
68 69 70 71 |
# File 'lib/discordrb/commands/container.rb', line 68 def remove_command(name) @commands ||= {} @commands.delete name end |