Class: Sponge::Bot

Inherits:
IRC::Client show all
Defined in:
lib/sponge/bot.rb

Instance Attribute Summary collapse

Attributes inherited from IRC::Client

#config, #irc, #listeners

Instance Method Summary collapse

Methods inherited from IRC::Client

#connect, #on, #process, #quit, #run, #set_nick

Constructor Details

#initialize(server = nil, options = {}, &blk) ⇒ Bot

Returns a new instance of Bot.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sponge/bot.rb', line 8

def initialize(server=nil, options={}, &blk)
  @plugins = Plugins.new(self)
  @auth = Auth.new(self, options[:users] || {})
  
  @command_prefix = options[:prefix] || '!'
  @channels = options[:channels] || []
  
  setup_plugins
  
  super(server, options, &blk)
  init_listeners
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



6
7
8
# File 'lib/sponge/bot.rb', line 6

def auth
  @auth
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



4
5
6
# File 'lib/sponge/bot.rb', line 4

def plugins
  @plugins
end

Instance Method Details

#add_plugin(plugin) ⇒ Object

Should only be used by #setup_plugins (for now..) Sugar for Sponge::Plugins#add



48
49
50
# File 'lib/sponge/bot.rb', line 48

def add_plugin(plugin)
  plugins.add(plugin)
end

#init_listenersObject

This probably isn’t a good idea, I generally don’t like seeing any eval lines, as it’s a nightmare to debug. It’ll do for now, though.



23
24
25
26
27
28
# File 'lib/sponge/bot.rb', line 23

def init_listeners
  path = File.expand_path('../listeners', __FILE__)
  Dir[path + '/*.rb'].each do |listener|
    instance_eval(File.read(listener))
  end
end

#register(command, message) ⇒ Object

Registers an IRC command ready to dispatch to any plugin who are listening



54
55
56
57
58
59
60
# File 'lib/sponge/bot.rb', line 54

def register(command, message)
  if Plugin.has_listener?(command)
    Plugin::LISTENERS[command.to_s.downcase].each do |cmd|
      plugins.dispatch_listen(cmd, message)
    end
  end
end

#setup_pluginsObject

Looks through the ‘plugins’ directory, require all plugins found, and add them into Sponge::Plugins



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

def setup_plugins
  unless File.directory?(@plugins.path)
    raise(LoadError, "No plugins directory found. Did you run `sponge init'?")
  end
  
  Dir[@plugins.path + '/*.rb'].each do |plugin|
    require plugin
  end
  
  Plugin::LIST.each do |plugin|
    add_plugin(plugin)
  end
end