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



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
43
44
45
46
47
48
49
# File 'lib/slackbot_frd/initializer/bot_starter.rb', line 16

def self.start_bots(errors_file, token, botdir, enabled_bots)
  bot_enabled = ->(bot) do
    enabled_bots.empty? ||
    enabled_bots.include?(bot) ||
    enabled_bots.include?(bot.gsub('-', '_').camelize)
  end

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

  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