Class: Daneel::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bot

Returns a new instance of Bot.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/daneel/bot.rb', line 10

def initialize(options = {})
  @logger = options[:logger] || Daneel::Logger.new
  @name = options[:name] || "daneel"
  @full_name = options[:full_name] || options[:name] || "R. Daneel Olivaw"
  @debug_mode = options[:verbose] && options[:adapter] && options[:adapter] != "shell"

  @data = Data.new
  logger.debug "Data source #{data.class}"

  Script.files.each{|file| try_require file }
  # TODO add script priorities to replicate this
  list = Script.list
  list.push list.delete(Scripts::ImageSearch)
  list.push list.delete(Scripts::Chatty)
  @scripts = list.map{|s| s.new(self) }
  logger.debug "Booted with scripts: #{@scripts.map(&:class).inspect}"

  @adapter = Adapter.named(options[:adapter] || "shell").new(self)
  logger.debug "Using the #{adapter.class} adapter"
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/daneel/bot.rb', line 7

def adapter
  @adapter
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/daneel/bot.rb', line 7

def data
  @data
end

#debug_modeObject

Returns the value of attribute debug_mode.



8
9
10
# File 'lib/daneel/bot.rb', line 8

def debug_mode
  @debug_mode
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



7
8
9
# File 'lib/daneel/bot.rb', line 7

def full_name
  @full_name
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/daneel/bot.rb', line 7

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/daneel/bot.rb', line 7

def name
  @name
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



7
8
9
# File 'lib/daneel/bot.rb', line 7

def scripts
  @scripts
end

Instance Method Details

#inspectObject



68
69
70
# File 'lib/daneel/bot.rb', line 68

def inspect
  %|#<#{self.class}:#{object_id} @name="#{name}" @adapter=#{adapter.class}>|
end

#receive(room, message, user) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/daneel/bot.rb', line 31

def receive(room, message, user)
  logger.debug "[room #{room.id}] #{user.name}: #{message.text}"
  message.command = command_from(message.text)

  scripts.each do |script|
    next unless script.accepts?(room, message, user)
    script.receive(room, message, user)
    break if message.done
  end
  message
rescue => e
  msg = %|#{e.class}: #{e.message}\n  #{e.backtrace.join("\n  ")}|
  logger.error msg
  adapter.announce "crap, something went wrong. :(", msg if @debug_mode
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/daneel/bot.rb', line 47

def run
  # TODO add i18n so that people can customize their bot's attitude
  # http://titusd.co.uk/2010/03/04/i18n-internationalization-without-rails/
  # TODO add Confabulator processing so the bot can be chatty without being static

  # Heroku cycles every process at least once per day by sending it a TERM
  trap(:TERM) do
    @adapter.announce "asked to stop, brb"
    exit
  end

  @adapter.announce "hey guys"
  @adapter.run
rescue Interrupt
  adapter.leave
end

#userObject



64
65
66
# File 'lib/daneel/bot.rb', line 64

def user
  @adapter.me
end