Class: Muzang::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bot

Returns a new instance of Bot.



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

def initialize(options = {})
  @options  = default_options.merge(options)
  @channels = @options.delete(:channels)
  @plugins  = {}
  @bot      = Coffeemaker::Bot.new(@options)

  @bot.irc.tap do |connection|
    connection.on_message = Proc.new do |m|
      @plugins.each do |plugin, instance|
        instance.call(connection, m) rescue nil
      end
    end

    connection.on_connect = Proc.new do
      @channels.each { |channel| connection.join(channel) }
    end
  end
end

Instance Attribute Details

#botObject

Returns the value of attribute bot.



9
10
11
# File 'lib/muzang/bot.rb', line 9

def bot
  @bot
end

#channelsObject

Returns the value of attribute channels.



9
10
11
# File 'lib/muzang/bot.rb', line 9

def channels
  @channels
end

#connectionObject

Returns the value of attribute connection.



9
10
11
# File 'lib/muzang/bot.rb', line 9

def connection
  @connection
end

#pluginsObject

Returns the value of attribute plugins.



9
10
11
# File 'lib/muzang/bot.rb', line 9

def plugins
  @plugins
end

Instance Method Details

#register_plugin(plugin) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/muzang/bot.rb', line 34

def register_plugin(plugin)
  klass = case plugin
  when Class
    plugin
  when String, Symbol
    klass_name = ActiveSupport::Inflector.classify(plugin.to_s)
    klass      = ActiveSupport::Inflector.constantize(klass_name)
  end
  @plugins[klass] = klass.new(self)
end

#startObject



30
31
32
# File 'lib/muzang/bot.rb', line 30

def start
  @bot.start
end