Class: CommandBot::Bot
- Inherits:
-
Object
- Object
- CommandBot::Bot
- Defined in:
- lib/command_bot/bot.rb
Overview
Main class.
Instance Attribute Summary collapse
- #commands ⇒ Array<Command> readonly
Instance Method Summary collapse
-
#add_commands(*commands) ⇒ Object
Add new commands.
- #find_command(name) ⇒ Command?
-
#handle(text, data = {}) ⇒ void?
Handle message.
-
#handle_command_call(command_call) ⇒ void?
Hadnle CommandCall.
-
#initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil) ⇒ Bot
constructor
Initialize new bot.
-
#remove_commands(*commands) ⇒ Object
Remove commands.
Constructor Details
#initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil) ⇒ Bot
Initialize new bot.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/command_bot/bot.rb', line 14 def initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil) @identifier = identifier || CommandIdentifier.default @command_not_found = command_not_found || proc { |_command_call| nil } @commands = [] log_formatter = proc do |severity, datetime, progname, msg| "[#{datetime}] #{severity} - #{progname || object_id}:\t #{msg}\n" end @logger = logger || Logger.new(STDOUT, formatter: log_formatter) @logger.level = log_level end |
Instance Attribute Details
#commands ⇒ Array<Command> (readonly)
28 29 30 |
# File 'lib/command_bot/bot.rb', line 28 def commands @commands end |
Instance Method Details
#add_commands(*commands) ⇒ Object
Add new commands.
39 40 41 42 43 |
# File 'lib/command_bot/bot.rb', line 39 def add_commands(*commands) logger.debug "Adding #{commands.size} new commands" all_aliases_array = all_aliases commands.each { |c| add_command_unless_alias_is_in_array(c, all_aliases_array) } end |
#find_command(name) ⇒ Command?
32 33 34 35 |
# File 'lib/command_bot/bot.rb', line 32 def find_command(name) logger.debug "Searching for command `#{name}`" commands.find { |c| c.name_matches?(name) } end |
#handle(text, data = {}) ⇒ void?
Handle message.
57 58 59 60 61 62 63 |
# File 'lib/command_bot/bot.rb', line 57 def handle(text, data = {}) logger.debug "Handling text: `#{text}` with additional data: #{data}" command_call = identify_command_call(text, data) return nil if command_call.nil? # Not a command call, so not handling. handle_command_call(command_call) end |
#handle_command_call(command_call) ⇒ void?
Hadnle CommandCall.
68 69 70 71 72 73 74 75 |
# File 'lib/command_bot/bot.rb', line 68 def handle_command_call(command_call) logger.debug "Hadnling command call: #{command_call}" if command_call.command execute_command_call(command_call) else command_not_found(command_call) end end |
#remove_commands(*commands) ⇒ Object
Remove commands.
47 48 49 50 51 |
# File 'lib/command_bot/bot.rb', line 47 def remove_commands(*commands) logger.debug "Removing #{commands.size} commands" command_names = commands.map { |c| c.is_a?(String) ? c : c.name } @commands.reject! { |ec| ec.name_matches?(*command_names) } end |