Class: Sponge::Plugin
- Inherits:
-
Object
- Object
- Sponge::Plugin
- Defined in:
- lib/sponge/plugin.rb
Overview
Plugin Example
class SayPlugin < Sponge::Plugin
def reply(m, arg)
m.reply arg
end
end
Simple as that, the reply method will get invoked when the say command is used. You can also have plugins ‘listen’ for stuff, for example if you’d like a plugin to print PRIVMSG’s to your terminal you can have a plugin which listens for them.
class ShowMessage < Sponge::Plugin
listen_to :privmsg
def listen(m)
if m.public?
puts "[#{m.channel}]<#{m.nick}> #{m.text}"
end
end
end
And that’s it, the listen method will be invoked whenever the bot see’s a PRIVMSG and it’ll send the Sponge::IRC::Message applicable for this command.
All plugins should be placed into a ‘plugins’ directory. All plugins are loaded on start and cannot be ‘unloaded’ although im sure this feature will be implemented at some time.
Constant Summary collapse
- LIST =
Our list of plugins
[]
- AUTH =
For plugins which require authentication
[]
- LISTENERS =
A hash containing IRC commands as keys whose values are Arrays containing a list of commands who are interested in ‘listening’ to them
:privmsg => ['log', 'memo', 'seen'] :join => ['log']
{}
Class Method Summary collapse
-
.command ⇒ Object
This works ok, but I should probably add a better way of checking at some point..
-
.has_listener?(cmd) ⇒ Boolean
Check if an IRC command has any plugins ‘listening’ for it.
-
.inherited(plugin) ⇒ Object
Throw our plugin straight into the LIST.
-
.listen_to(*commands) ⇒ Object
Allows a specific plugin to ‘listen’ to irc messages making the plugins a little more interactive.
-
.requires_auth ⇒ Object
Plugin requires the nick to be authenticated.
Instance Method Summary collapse
-
#help ⇒ Object
The default help if a specific plugin lacks it.
-
#initialize(bot) ⇒ Plugin
constructor
Create a new Plugin instance This is done for every command, if you want to reference the Sponge::Bot instance and also want to create add variables in your plugins constructor then make sure you either call super() or assign @bot in your plugins.
- #requires_auth? ⇒ Boolean
-
#usage ⇒ Object
The default usage if a specific plugin lacks it.
Constructor Details
#initialize(bot) ⇒ Plugin
Create a new Plugin instance This is done for every command, if you want to reference the Sponge::Bot instance and also want to create add variables in your plugins constructor then make sure you either call super() or assign @bot in your plugins
Probably a bad idea to use ::new on every Plugin, perhaps create a ‘setup’ method for controlling per plugin options and data in the future
Need to implement a way of setting plugin specific options through class methods without using constants that just store the plugin name (ie AUTH)
90 91 92 |
# File 'lib/sponge/plugin.rb', line 90 def initialize(bot) @bot = bot end |
Class Method Details
.command ⇒ Object
This works ok, but I should probably add a better way of checking at some point.. heh. Returns ‘say’ from SayPlugin, etc
52 53 54 |
# File 'lib/sponge/plugin.rb', line 52 def self.command name.downcase.sub(/plugin$/, '') end |
.has_listener?(cmd) ⇒ Boolean
Check if an IRC command has any plugins ‘listening’ for it
57 58 59 |
# File 'lib/sponge/plugin.rb', line 57 def self.has_listener?(cmd) LISTENERS.key?(cmd.to_s.downcase) end |
.inherited(plugin) ⇒ Object
Throw our plugin straight into the LIST
45 46 47 |
# File 'lib/sponge/plugin.rb', line 45 def self.inherited(plugin) LIST << plugin end |
.listen_to(*commands) ⇒ Object
Allows a specific plugin to ‘listen’ to irc messages making the plugins a little more interactive
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sponge/plugin.rb', line 69 def self.listen_to(*commands) commands.each do |cmd| cmd = cmd.to_s.downcase if LISTENERS.key?(cmd) LISTENERS[cmd] << command else LISTENERS[cmd] = [command] end end end |
Instance Method Details
#help ⇒ Object
The default help if a specific plugin lacks it
95 96 97 |
# File 'lib/sponge/plugin.rb', line 95 def help "No help for this plugin" end |
#requires_auth? ⇒ Boolean
104 105 106 |
# File 'lib/sponge/plugin.rb', line 104 def requires_auth? AUTH.include? self.class.command end |
#usage ⇒ Object
The default usage if a specific plugin lacks it
100 101 102 |
# File 'lib/sponge/plugin.rb', line 100 def usage "No usage for this plugin" end |