Class: Sponge::Plugins

Inherits:
Object
  • Object
show all
Defined in:
lib/sponge/plugins.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ Plugins

Returns a new instance of Plugins.



9
10
11
12
13
# File 'lib/sponge/plugins.rb', line 9

def initialize(bot)
  @bot = bot
  @path = File.expand_path('plugins')
  @list = {}
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



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

def bot
  @bot
end

#pathObject (readonly)

The full path to our plugins directory



7
8
9
# File 'lib/sponge/plugins.rb', line 7

def path
  @path
end

Instance Method Details

#add(plugin) ⇒ Object

Add a plugin, this method invokes Plugin::new and passes our Sponge::Bot instance so we can access it in all of our plugins



17
18
19
# File 'lib/sponge/plugins.rb', line 17

def add(plugin)
  @list[plugin.command] = plugin.new(bot)
end

#commandsObject

Return a list of our plugin commands



34
35
36
# File 'lib/sponge/plugins.rb', line 34

def commands
  @list.keys
end

#dispatch_listen(command, message) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sponge/plugins.rb', line 38

def dispatch_listen(command, message)
  if include?(command)
    plugin = get(command)

    if plugin.respond_to?(:listen)
      plugin.send(:listen, message)
    else
      raise(ArgumentError, "Unable to dispatch to #{command}, does the #{plugin} plugin have a `listen' method?")
    end
  else
    raise(ArgumentError, "Attempting to dispatch to unknown plugin")
  end
end

#dispatch_reply(command, message, args) ⇒ Object Also known as: reply



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sponge/plugins.rb', line 52

def dispatch_reply(command, message, args)
  if include?(command)
    plugin = get(command)
    
    # We don't raise if a plugin hasn't implemented a reply method
    # as plugins can *just* be listeners
    if plugin.respond_to?(:reply)
      if plugin.requires_auth?
        return unless bot.auth.logged_in?(message.nick)
      end
      plugin.send(:reply, message, args)
    end
  else
    raise(ArgumentError, "Attempting to dispatch to unknown plugin")
  end
end

#get(plugin) ⇒ Object Also known as: []

Grab a plugin



22
23
24
# File 'lib/sponge/plugins.rb', line 22

def get(plugin)
  @list[plugin]
end

#help(command) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/sponge/plugins.rb', line 70

def help(command)
  if include?(command)
    plugin = get(command)
    
    plugin.help
  end
end

#include?(command) ⇒ Boolean Also known as: has_plugin?

Check if a plugin exists

Returns:

  • (Boolean)


28
29
30
# File 'lib/sponge/plugins.rb', line 28

def include?(command)
  @list.key?(command)
end