MVpybot: Update 2
Monday, January 18th, 2010I just did another major update to MVpybot. Listeners have been implemented. A listener is a function that gets called whenever the bot receives data, or it can be set to only be called when a certain type (privmsg, join, part, etc) is received. As usual, the source is at the SVN repo.
Now for the technical stuff. A listener is just a python file in the listeners folder, with functions defined in it. A function’s name should be botfunction_type, where type is the type of message to listen for (privmsg, join, part, etc) or it can be ‘any’ for all data received by the bot. Here is an example logger function:
#!/usr/bin/python import time enabled=1 logfile=open('log','a') timestamp=time.strftime('[ Session starting at %y-%m-%d %H:%M:%S ]') logfile.write(timestamp+'\n') logfile.close() def botfunction_any(): logfile=open('log','a') timestamp=time.strftime('[%y-%m-%d %H:%M:%S] ') logfile.write(timestamp+line+'\n') logfile.close()
As you can see from the example, plugins can be quickly enabled or disabled by toggling the ‘enabled’ flag. Also, ‘line’ is passed to the plugin. Not that anything outside of function definitions will be run when the bot starts.
Listeners can use the ‘socket’ variable to send data to the server, as shown in this example:
#!/usr/bin/python enabled=0 def botfunction_privmsg(): print "called" out="PRIVMSG %s :Botfunction_privmsg called" %(channel) print out socket.send(out+'\n')
Note that in the example, it uses botfunction_privmsg. To make listeners easier to write, the main program will automatically figure out these variables from privmsgs and pass them to the function:
info #user info msg #the message channel #the channel the message was from (set to the sender for private messages) sender #the sender of the message senderstuff #info of the sender isprivate #whether or not the message was a private message as opposed to a channel message.
This only applies to privmsgs. All other events only get ‘socket’ and ‘line’ passed and have to figure out everything from ‘line’
That’s all for now.