Class: Sponge::Plugins
- Inherits:
-
Object
- Object
- Sponge::Plugins
- Defined in:
- lib/sponge/plugins.rb
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#path ⇒ Object
readonly
The full path to our plugins directory.
Instance Method Summary collapse
-
#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.
-
#commands ⇒ Object
Return a list of our plugin commands.
- #dispatch_listen(command, message) ⇒ Object
- #dispatch_reply(command, message, args) ⇒ Object (also: #reply)
-
#get(plugin) ⇒ Object
(also: #[])
Grab a plugin.
- #help(command) ⇒ Object
-
#include?(command) ⇒ Boolean
(also: #has_plugin?)
Check if a plugin exists.
-
#initialize(bot) ⇒ Plugins
constructor
A new instance of Plugins.
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.('plugins') @list = {} end |
Instance Attribute Details
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
4 5 6 |
# File 'lib/sponge/plugins.rb', line 4 def bot @bot end |
#path ⇒ Object (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 |
#commands ⇒ Object
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, ) if include?(command) plugin = get(command) if plugin.respond_to?(:listen) plugin.send(:listen, ) 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:
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, , 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?(.nick) end plugin.send(:reply, , 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
28 29 30 |
# File 'lib/sponge/plugins.rb', line 28 def include?(command) @list.key?(command) end |