-
Notifications
You must be signed in to change notification settings - Fork 0
Creating plugins
There are three methods used for creating basil plugins.
A responder will only be used on messages that are to basil:
Basil.respond_to(/a regex/) do
# logic that either returns a Message or nil
end
Within the block, you have access to two very useful instance variables:
-
@msg
is the Message that the plugin is currently responding to -
@match_data
contains the MatchData object that resulted from applying your regex
You've also got all the methods from Basil::Utils
and Basil::ChatHistory
.
Watchers are exactly like responders except that they can be triggered on messages to anyone. One example might be to show titles for any urls mentioned in the chat:
Basil.watch_for(/https?:\/\/\S+/) do
if get_html(@match_data[0]) =~ /<title>(.*?)<\/title>/
says "Title: #{$1}"
end
end
Recently, basil gained the ability to check email. What basil does with these emails depends on the plugins defined in the following way:
class AnObject
def self.create_message(email)
# this method must accept a Basil::Email::Mail and (possibly) return
# a Basil::Message. If it does, the message will be handed to your
# server's broadcast_message method.
end
end
Basil.check_email(AnObject)
Email is polled (on the interval set in your config file) if and only if your server actually responds to the broadcast_message
method
- Don't write plugins that can be accomplished via factoids:
# bad
Basil.respond_to('beer') do
says "have a beer!"
end
The same can be setup in chat, by anyone:
# good
> basil, beer is <say>have a beer!
Ta-da!
- No exception handling.
In order to provide consistent behavior, it's best if any exceptions are handled by the dispatcher. rescue
s are permitted only if it greatly simplifies the method.
# bad
Basil.respond_to(/.../) do
begin
resp = get_html("http://...")
reply resp if resp
rescue
reply "error accessing site!"
end
end
# good
Basil.respond_to(/.../) do
reply get_html("http://...")
end
# OK
Basil.respond_to(/.../) do
resp = Object.any.method.could.be.nil rescue nil
reply resp if resp
end
-
If a plugin requires a lot of logic, put the logic in a class (or classes) defined in the plugin file, then use the classes from within your plugin. Defining the classes to be within the Basil module is discouraged.
-
Do group related plugins in a single file and do not group unrelated plugins in a single file.