Class: BotStarter

Inherits:
Object
  • Object
show all
Defined in:
lib/slackbot_frd/initializer/bot_starter.rb

Class Method Summary collapse

Class Method Details

.start_bots(errors_file, token, botdir, enabled_bots) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slackbot_frd/initializer/bot_starter.rb', line 15

def self.start_bots(errors_file, token, botdir, enabled_bots)
  bot_enabled = ->(bot) { enabled_bots.empty? || enabled_bots.include?(bot) }

  # Create a new Connection to pass to the bot classes
  slack_connection = SlackbotFrd::SlackConnection.new(token, errors_file)

  load_bot_files(botdir)

  bots = []
  # instantiate them, and then call their add_callbacks method
  ObjectSpace.each_object(Class).select do |klass|
    if klass != SlackbotFrd::Bot && klass.ancestors.include?(SlackbotFrd::Bot) && bot_enabled.call(klass.name)
      SlackbotFrd::Log.debug("Instantiating and adding callbacks to class '#{klass.to_s}'")
      b = klass.new
      b.add_callbacks(slack_connection)
      bots.push(b)
    end
  end

  if bots.count == 0
    SlackbotFrd::Log.error("Not starting: no bots found")
    File.append(errors_file, "Not starting: no bots found")
  else
    SlackbotFrd::Log.debug("Starting SlackConnection")
    slack_connection.start
    SlackbotFrd::Log.debug("Connection closed")
  end
end