Class: Raibo::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#docsObject

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/raibo/bot.rb', line 50

def alive?
  @thread.alive? if @thread
end

#helpObject



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

#resetObject



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

#stopObject



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