Class: CommandBot::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/command_bot/bot.rb

Overview

Main class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier: nil, logger: nil, log_level: Logger::INFO, command_not_found: nil) ⇒ Bot

Initialize new bot.

Parameters:

  • logger (Logger) (defaults to: nil)
  • log_level (Logger::Severity) (defaults to: Logger::INFO)
  • command_not_found (Proc) (defaults to: nil)

    process to execute when command can’t be identified. Accepts single argument of CommandCall. Defaults to returning nil.



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

#commandsArray<Command> (readonly)

Returns:



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.

Parameters:



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?

Parameters:

  • name (String)

Returns:



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.

Parameters:

  • text (String)

    message text.

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

    additional data which should be passed to handler procedure.

Returns:

  • (void, nil)

    result depends on handler of command.



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.

Parameters:

Returns:

  • (void, nil)

    result depends on handler of command.



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.

Parameters:



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