I know some of you have added commands to the B3 bot by making code hacks to admin.py/other files. Whilst this is extremely easy, it also increases the difficulty of updates (since you'd need to transfer your modifications between versions), and would also be hard to share, should you want to (Download and Install plugin is easier than 'On line 234 of admin.py, insert this data, then in admin.xml do this, then in punkbuster.py do this, etc etc).
In this short example, I will be converting the poke command into a plugin.
def cmd_poke(self, data, client=None, cmd=None):
"""\
- Notify a player that he needs to move
"""
m = self.parseUserCmd(data)
if not m:
client.message('^7Invalid parameters, you must supply a player name')
return False
if m[0] == 'b3':
self.warnClient(client, 'Do not poke b3!', None, False, '', 1)
else:
sclient = self.findClientPrompt(m[0], client)
if sclient:
self.console.say('^7%s %s^7!' % (random.choice(('Wake up', '*poke*', 'Attention', 'Get up', 'Go', 'Move out')), sclient.exactName))
That is the b3 code for !poke, found in admin.py.
This is what I did to make it a plugin:
__version__ = '1.0'
__author__ = 'Bakes'
import b3, re
import b3.events
# Import the necessary libaries you need here, for example, I need random for the randomization of answers part of it.
import random
#--------------------------------------------------------------------------------------------------
#This lot doesn't need to be changed for simple commands, it gets the admin plugin and registers commands.
class PokePlugin(b3.plugin.Plugin):
_adminPlugin = None
def startup(self):
"""\
Initialize plugin settings
"""
# get the admin plugin so we can register commands
self._adminPlugin = self.console.getPlugin('admin')
if not self._adminPlugin:
# something is wrong, can't start without admin plugin
self.error('Could not find admin plugin')
return False
# register our commands (you can ignore this bit)
if 'commands' in self.config.sections():
for cmd in self.config.options('commands'):
level = self.config.get('commands', cmd)
sp = cmd.split('-')
alias = None
if len(sp) == 2:
cmd, alias = sp
func = self.getCmd(cmd)
if func:
self._adminPlugin.registerCommand(self, cmd, level, func, alias)
self.debug('Started')
def getCmd(self, cmd):
cmd = 'cmd_%s' % cmd
if hasattr(self, cmd):
func = getattr(self, cmd)
return func
return None
#--------------------------------------------------------------------
# Your commands go under here
def cmd_poke(self, data, client=None, cmd=None):
"""\
<player> - Notify a player that he needs to move
"""
m = self._adminPlugin.parseUserCmd(data)
if not m:
client.message('^7Invalid parameters, you must supply a player name')
return False
if m[0] == 'b3':
self._adminPlugin.warnClient(client, 'Do not poke b3!', None, False, '', 1)
else:
sclient = self._adminPlugin.findClientPrompt(m[0], client)
if sclient:
self.console.say('^7%s %s^7!' % (random.choice(('Wake up', '*poke*', 'Attention', 'Get up', 'Go', 'Move out')), sclient.exactName))
<configuration plugin="banter">
<settings name="commands">
<set name="poke">2</set>
</settings>
</configuration>
Key:
green: replace this with your plugin name. (the plugin file (poke.py in this case) should be similar)
red: This is stuff I changed between the two code pieces, because they relate to, in this case, the admin plugin (though you can change that to your liking, could be the punkbuster plugin, etc.). For example, self._adminPlugin.warn calls the part of admin.py that deals with warnings.
orange: These parts are the parts that deal with importing other plugins. They import the admin plugin, but you could use them for punkbuster, poweradmin, etc. Why would I need to do this? One server has a '!dirtyhacker' command, that takes a pbss, pm's them 'You Dirty Hacker, why would you do such a thing' and blows them up.
As you can see, _adminPlugin is a variable, in theory (but I wouldn't ever do it, it's bad practice), I could call the admin plugin as _cheese, and use _cheese.warn, it's all up to you.
So, that really is all you need to know to make a plugin out of code modification. This is, of course, providing that you have only modified a plugin, rather than actual b3 code; If you have actually improved the code, it would be nice to see your results
