Skip to content
theepicsnail edited this page Aug 14, 2012 · 9 revisions

Plugins require a class with the same name as the filename.

Filename.py

from Hook import *
from Logging import LogFile
log = LogFile("Filename")

@requires("ServiceName")
@prefers("ServiceName")
class Filename:
    @dedicated()
    def alwaysRunning(self,response):
        pass
    @bindFunction(message="Some.*Regex"):
        pass

Additional parameters a plugin can use:

response

any capture groups (numbered or named)
    @bindFunction(message="(?P<foo>...)")
    def func(self,foo): #foo will be the first 3 characters of any message.

    @bindFunction(message="(.*):(.*)")
    def func(self,message0,message1): # message0 will be the first capture, message1 will be the 2nd

anything provided by a service that was successfully retrieved through the use of require/prefers

Core variables
    event - the event from the connector
    pm - SB2's Plugin Manager
    pd - Plugin Dispatcher
    ro - Response Object (Same as response)
    c - Connector
    core - Core
    config - Config object

    Be careful with these, you can create deadlocks, and race conditions very easily using these. 
    Plugins are called from a separate thread than where these objects are running.

Superbot2IRC connector parameters:

nickname
command
prefix
target
message

Superbot2IRC response functions:

msg(user,message)
me(channel,message)
say(channel,message) 
join(channel)
part(channel,message=None)
kick(channel,user,message=None)
topic(channel,message=None)
notice(user,message)
away(message="")
back()
setNick(nick)
quit(message)

Superbot2IRC Service args: These are parameters a plugin can use if it requires/prefers this service.

IRCArgs
    nick
    toMe
Colors
    colorize(msg)

Logging

See the example to learn how to create a logging object. Loggers have an array of functions to help dump data.

These are just the normal logging functions, they accept any number of arguments.
log.debug(*args,**kwargs)
log.note(*args,**kwargs)
log.info(*args,**kwargs)
log.warning(*args,**kwargs)
log.error(*args,**kwargs)
log.critical(*args,**kwargs)

Here are a couple special logging functions
log.dict(d,*args)
    This will dump the dict in a key=>value format. Mapping both keys and values through str()
    Logs at DEBUG level.
log.exception(*args)
    This will log the stack trace of whatever the current exception is.
    Logs at ERROR level.
log.log(level,*args,**kwargs)
    This allows for custom levels (it's the base function that all the other functions call).
    Logs at the level specified. This is an int, lower level = lower priority. 
    debug=10, note=info=20, warning=30, error=40, critical=50

Helpful commands

rm Logs/*; python Core.py

python LogViewer.py Logs/* | less

python LogViewer.py Logs/Exceptions* | less


My plugin isn't loading!

USE LOGGING. This will help you greatly. Log everything you do, you can always remove unnecessary logging later once you have it running.

Is it loaded? (Case matters)

/notice BotName plugin load PluginName

Is there an exception?

python LogViewer.py Logs/Exceptions* | less

Did you forget to use the bindFunction decorators on your functions (See example.)

Make sure your bindFunction arguments actually are working.

Change your bindFunction calls to just bindFunction() and make sure your regular expressions
actually match the values coming in.

If you want it to automatically start with the bot, make sure you put it in the Autoload.cfg file.


Best practices

USE LOGGING

Your dedicated functions should return/exit often. The bot can only exit cleanly when it has control over the dedicated threads. If you hold onto that thread the bot can't shut down.

Make your bindFunction parameters as strict as possible. (Helps prevent multiple plugins from firing on the same command)

Only put something in a service if other plugins could use it.

Clone this wiki locally