Class: Daneel::Bot
- Inherits:
-
Object
- Object
- Daneel::Bot
- Defined in:
- lib/daneel/bot.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#debug_mode ⇒ Object
Returns the value of attribute debug_mode.
-
#full_name ⇒ Object
readonly
Returns the value of attribute full_name.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Bot
constructor
A new instance of Bot.
- #inspect ⇒ Object
- #receive(room, message, user) ⇒ Object
- #run ⇒ Object
- #user ⇒ Object
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( = {}) @logger = [:logger] || Daneel::Logger.new @name = [:name] || "daneel" @full_name = [:full_name] || [:name] || "R. Daneel Olivaw" @debug_mode = [:verbose] && [:adapter] && [: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([:adapter] || "shell").new(self) logger.debug "Using the #{adapter.class} adapter" end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/daneel/bot.rb', line 7 def adapter @adapter end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/daneel/bot.rb', line 7 def data @data end |
#debug_mode ⇒ Object
Returns the value of attribute debug_mode.
8 9 10 |
# File 'lib/daneel/bot.rb', line 8 def debug_mode @debug_mode end |
#full_name ⇒ Object (readonly)
Returns the value of attribute full_name.
7 8 9 |
# File 'lib/daneel/bot.rb', line 7 def full_name @full_name end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/daneel/bot.rb', line 7 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/daneel/bot.rb', line 7 def name @name end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
7 8 9 |
# File 'lib/daneel/bot.rb', line 7 def scripts @scripts end |
Instance Method Details
#inspect ⇒ Object
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, , user) logger.debug "[room #{room.id}] #{user.name}: #{.text}" .command = command_from(.text) scripts.each do |script| next unless script.accepts?(room, , user) script.receive(room, , user) break if .done end rescue => e msg = %|#{e.class}: #{e.}\n #{e.backtrace.join("\n ")}| logger.error msg adapter.announce "crap, something went wrong. :(", msg if @debug_mode end |
#run ⇒ Object
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 |
#user ⇒ Object
64 65 66 |
# File 'lib/daneel/bot.rb', line 64 def user @adapter.me end |