MVpybot
This is the page for my Python IRC bot.
SVN here.
Using the Bot
- First, configure everything in options.py. Then, to run the bot, simply execute newbot.py. Make sure the bot can connect, as some servers seem to confuse the bot by sending an early ping.
- Configure users in the users file. Just put a username, a password, and the level. The level can either be user or admin. More users can register later with the register command.
- Put plugins in the botplugins/ folder. The plugins should have a .py extension. .pyc files are compiled modules, and the .py files should be used instead.
Writing Plugins
A plugin is just a Python module that is automatically imported when the bot starts if it is in the functions directory. To write a module, simply create a python script that will return the message to be sent. Here is the ping script that is included with the bot:
#!/usr/bin/python
def ping():
outstuff=''
print "ping called"
if (len(cmd)<2):
return('PRIVMSG %s :Incorrect usage. Syntax: ping (4|6)
address.' %(channel))
else:
if cmd[1]=='4' or cmd[1]=='6':
if cmd[1]=='4':
output=syscmd(['ping','-c','5','-i','0.2',cmd[2]])
outsplit=output.split('\n')
outparts=outsplit[-3:-1]
for part in outparts:
outstuff+='PRIVMSG %s :%s\n' %(channel,part)
if cmd[1]=='6':
output=syscmd(['ping6','-c','5','-i','0.2',cmd[2]])
outsplit=output.split('\n')
outparts=outsplit[-3:-1]
for part in outparts:
outstuff+='PRIVMSG %s :%s\n' %(channel,part)
return(outstuff)
else:
return('PRIVMSG %s :Error: protocol must be either 4 or 6' %channel)
The script can use most of the variables used in the main program (channel, sender, nick, etc) and must return a value to be passed to the socket, which will usually be a PRIVMSG.
The other type of plugin is an eventhandler, which is called when the bot receives a certain type of message. Instead of being defined as functionname(), it is defined as botfunction_type(), where type is a type of message, such as ‘privmsg’ or ‘join’, or is ‘any’ to catch all data. Here is an example of an eventhandler function:
#!/usr/bin/python enabled=0 def botfunction_privmsg(): print "called" out="PRIVMSG %s :Botfunction_privmsg called" %(channel) print out socket.send(out+'\n')
Here is an example of a logger:
#!/usr/bin/python
import time
import __builtin__
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 '+__builtin__.host+'] ')
logfile.write(timestamp+line+'\n')
logfile.close()

January 18th, 2010 at 00:31
[...] know (probably most of you), MVpybot is an IRC bot written in python. It’s main page is here, and the post about the latest update is here. Download it here (click ‘Download GNU [...]