Class: Raibo::Bot
- Inherits:
-
Object
- Object
- Raibo::Bot
- Defined in:
- lib/raibo/bot.rb
Instance Attribute Summary collapse
-
#docs ⇒ Object
Returns the value of attribute docs.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #help ⇒ Object
-
#initialize(*args) ⇒ Bot
constructor
A new instance of Bot.
- #load_config_file(filename) ⇒ Object
- #reset ⇒ Object
- #run(async = false) ⇒ Object
- #stop ⇒ Object
- #use(handler = nil, &block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Bot
Returns a new instance of Bot.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/raibo/bot.rb', line 5 def initialize(*args) if args.first.is_a?(Raibo::CampfireConnection) or args.first.is_a?(Raibo::IrcConnection) @connection = args.shift elsif args.first == 'campfire' args.shift @connection = Raibo::CampfireConnection.new(*args) elsif args.first == 'irc' args.shift @connection = Raibo::IrcConnection.new(*args) end reset end |
Instance Attribute Details
#docs ⇒ Object
Returns the value of attribute docs.
3 4 5 |
# File 'lib/raibo/bot.rb', line 3 def docs @docs end |
Instance Method Details
#alive? ⇒ Boolean
50 51 52 |
# File 'lib/raibo/bot.rb', line 50 def alive? @thread.alive? if @thread end |
#help ⇒ Object
58 59 60 61 62 |
# File 'lib/raibo/bot.rb', line 58 def help width = [15, @docs.map(&:first).map(&:length).max].max @connection.say(*@docs.map { |cmd, desc| "%-#{width}s %s" % [cmd, desc] }) end |
#load_config_file(filename) ⇒ Object
54 55 56 |
# File 'lib/raibo/bot.rb', line 54 def load_config_file(filename) @dsl.instance_eval(IO.read(filename), filename) end |
#reset ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/raibo/bot.rb', line 19 def reset @handlers = [] @docs = [] @dsl = Raibo::DSL.new(self, @connection) use do |msg| if msg.body == '!help' @bot.help end end end |
#run(async = false) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/raibo/bot.rb', line 36 def run(async=false) if async @thread = Thread.new { run_sync } @thread.abort_on_exception = true else run_sync end end |
#stop ⇒ Object
45 46 47 48 |
# File 'lib/raibo/bot.rb', line 45 def stop @thread.kill if @thread @connection.close end |
#use(handler = nil, &block) ⇒ Object
31 32 33 34 |
# File 'lib/raibo/bot.rb', line 31 def use(handler=nil, &block) @handlers.push(handler || block) @docs.concat(handler.docs) if handler.respond_to?(:docs) end |