Skip to content

Commit

Permalink
update Python plugin templates and tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaribz committed Jan 28, 2021
1 parent 18e970c commit bcae1c6
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 12 deletions.
4 changes: 2 additions & 2 deletions plugins/templates/commented/myconfigurableplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# We define one global setting "MyGlobalSetting" and one preset setting "MyPresetSetting".
# Both are of type "notNull", which means any non-null value is allowed
# (check %paramTypes hash in SpadsConf.pm for a complete list of allowed setting types)
globalPluginParams = { 'MyGlobalSetting': ['notNull'] };
presetPluginParams = { 'MyPresetSetting': ['notNull'] };
globalPluginParams = { 'MyGlobalSetting': ['notNull'] }
presetPluginParams = { 'MyPresetSetting': ['notNull'] }


# This is how SPADS gets our version number (mandatory callback)
Expand Down
8 changes: 4 additions & 4 deletions plugins/templates/commented/mynewcommandplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# - commandsFile: name of the plugin commands rights configuration file (located in etc dir, same syntax as commands.conf)
# - helpFile: name of plugin commands help file (located in plugin dir, same syntax as help.dat)
globalPluginParams = { 'commandsFile': ['notNull'],
'helpFile': ['notNull'] };
'helpFile': ['notNull'] }
presetPluginParams = None


Expand Down Expand Up @@ -52,10 +52,10 @@ def __init__(self,context):
def onUnload(self,reason):

# We remove our new command handler
spads.removeSpadsCommandHandler(['myCommand']);
spads.removeSpadsCommandHandler(['myCommand'])

# We log a notice message when the plugin is unloaded
spads.slog("Plugin unloaded",3);
spads.slog("Plugin unloaded",3)



Expand All @@ -79,4 +79,4 @@ def hMyCommand(source,user,params,checkOnly):
paramsString = ','.join(params)

# We log the command call as notice message
spads.slog("User %s called command myCommand with parameter(s) \"%s\"" % (user,paramsString),3);
spads.slog("User %s called command myCommand with parameter(s) \"%s\"" % (user,paramsString),3)
4 changes: 2 additions & 2 deletions plugins/templates/raw/myconfigurableplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
pluginVersion='0.1'
requiredSpadsVersion='0.12.29'

globalPluginParams = { 'MyGlobalSetting': ['notNull'] };
presetPluginParams = { 'MyPresetSetting': ['notNull'] };
globalPluginParams = { 'MyGlobalSetting': ['notNull'] }
presetPluginParams = { 'MyPresetSetting': ['notNull'] }


def getVersion(pluginObject):
Expand Down
8 changes: 4 additions & 4 deletions plugins/templates/raw/mynewcommandplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
requiredSpadsVersion='0.12.29'

globalPluginParams = { 'commandsFile': ['notNull'],
'helpFile': ['notNull'] };
'helpFile': ['notNull'] }
presetPluginParams = None


Expand All @@ -27,8 +27,8 @@ def __init__(self,context):
spads.slog("Plugin loaded (version %s)" % pluginVersion,3)

def onUnload(self,reason):
spads.removeSpadsCommandHandler(['myCommand']);
spads.slog("Plugin unloaded",3);
spads.removeSpadsCommandHandler(['myCommand'])
spads.slog("Plugin unloaded",3)


def hMyCommand(source,user,params,checkOnly):
Expand All @@ -38,4 +38,4 @@ def hMyCommand(source,user,params,checkOnly):
for i in range(len(params)):
params[i]=spads.fix_string(params[i])
paramsString = ','.join(params)
spads.slog("User %s called command myCommand with parameter(s) \"%s\"" % (user,paramsString),3);
spads.slog("User %s called command myCommand with parameter(s) \"%s\"" % (user,paramsString),3)
106 changes: 106 additions & 0 deletions plugins/tutorials/forbiddenwords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Import the perl module so we can call the SPADS Plugin API
import perl

# Import the regular expression module to check for forbidden words
import re

# perl.ForbiddenWords is the Perl representation of the ForbiddenWords plugin module
# We will use this object to call the plugin API
spads=perl.ForbiddenWords

# This is the first version of the plugin
pluginVersion='0.1'

# This plugin requires a SPADS version which supports Python plugins
# (only SPADS versions >= 0.12.29 support Python plugins)
requiredSpadsVersion='0.12.29'

# We define one global setting "words" and one preset setting "immuneLevel".
# "words" has no type associated (no restriction on allowed values)
# "immuneLevel" must be an integer or an integer range
# (check %paramTypes hash in SpadsConf.pm for a complete list of allowed
# setting types)
globalPluginParams = { 'words': [] }
presetPluginParams = { 'immuneLevel': ['integer','integerRange'] }


# This is how SPADS gets our version number (mandatory callback)
def getVersion(pluginObject):
return pluginVersion

# This is how SPADS determines if the plugin is compatible (mandatory callback)
def getRequiredSpadsVersion(pluginName):
return requiredSpadsVersion

# This is how SPADS finds what settings we need in our configuration file (mandatory callback for configurable plugins)
def getParams(pluginName):
return [ globalPluginParams , presetPluginParams ]



# This is the class implementing the plugin
class ForbiddenWords:

# This is our constructor, called when the plugin is loaded by SPADS (mandatory callback)
def __init__(self,context):

# We call the API function "slog" to log a notice message (level 3) when the plugin is loaded
spads.slog("Plugin loaded (version %s)" % pluginVersion,3)

# We set up a lobby command handler on SAIDBATTLE
spads.addLobbyCommandHandler({'SAIDBATTLE': hLobbySaidBattle})


# This callback is called each time we (re)connect to the lobby server
def onLobbyConnected(self,lobbyInterface):

# When we are disconnected from the lobby server, all lobby command
# handlers are automatically removed, so we (re)set up our command
# handler here.
spads.addLobbyCommandHandler({'SAIDBATTLE': hLobbySaidBattle})


# This callback is called when the plugin is unloaded
def onUnload(self,reason):

# We remove our lobby command handler when the plugin is unloaded
spads.removeLobbyCommandHandler(['SAIDBATTLE'])


# This is the handler we set up on SAIDBATTLE lobby command.
# It is called each time a player says something in the battle lobby.
# command is the lobby command name (SAIDBATTLE)
# user is the name of the user who said something in the battle lobby
# message is the message said in the battle lobby
def hLobbySaidBattle(command,user,message):

# First we "fix" strings received from Perl in case
# the Inline::Python module transmits them as byte strings
(user,message)=spads.fix_string(user,message)

# Here we check it's not a message from SPADS (so we don't kick ourself)
spadsConf = spads.getSpadsConf()
if user == spadsConf['lobbyLogin']:
return

# Then we check the user isn't a privileged user
# (autohost access level >= immuneLevel)
pluginConf = spads.getPluginConf()
if int(spads.getUserAccessLevel(user)) >= int(pluginConf['immuneLevel']):
return

# We put the forbidden words in a list
forbiddenWords = pluginConf['words'].split(';')

# We test each forbidden word
for forbiddenWord in forbiddenWords:

# If the message contains the forbidden word (case insensitive)
if re.search(r'\b' + re.escape(forbiddenWord) + r'\b',message,re.IGNORECASE):

# Then we kick the user from the battle lobby
spads.sayBattle("Kicking %s from battle (watch your language!)" % user)
spads.queueLobbyCommand(["KICKFROMBATTLE",user])

# We quit the foreach loop (no need to test other forbidden word)
break
54 changes: 54 additions & 0 deletions plugins/tutorials/helloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Import the perl module so we can call the SPADS Plugin API
import perl

# perl.HelloWorld is the Perl representation of the HelloWorld plugin module
# We will use this object to call the plugin API
spads=perl.HelloWorld


# This is the first version of the plugin
pluginVersion='0.1'

# This plugin requires a SPADS version which supports Python plugins
# (only SPADS versions >= 0.12.29 support Python plugins)
requiredSpadsVersion='0.12.29'


# This is how SPADS gets our version number (mandatory callback)
def getVersion(pluginObject):
return pluginVersion

# This is how SPADS determines if the plugin is compatible (mandatory callback)
def getRequiredSpadsVersion(pluginName):
return requiredSpadsVersion



# This is the class implementing the plugin
class HelloWorld:

# This is our constructor, called when the plugin is loaded by SPADS (mandatory callback)
def __init__(self,context):

# We call the API function "slog" to log a notice message (level 3) when the plugin is loaded
spads.slog("Plugin loaded (version %s)" % pluginVersion,3)

# This is the callback called each time SPADS receives a private message
# self is the plugin object (first parameter of all plugin callbacks)
# userName is the name of the user sending the private message
# message is the message sent by the user
def onPrivateMsg(self,userName,message):

# Here we "fix" strings received from Perl in case
# the Inline::Python module transmits them as byte strings
(userName,message)=spads.fix_string(userName,message)

# We check the message sent by the user is "Hello"
if message == 'Hello':

# We send our wonderful Hello World message
spads.sayPrivate(userName,'Hello World')

# We return 0 because we don't want to filter out private messages
# for other SPADS processing
return 0
79 changes: 79 additions & 0 deletions plugins/tutorials/timeplugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Import the perl module so we can call the SPADS Plugin API
import perl

# Import the datetime module so we can get current time for our !time command
import datetime

# perl.TimePlugin is the Perl representation of the TimePlugin plugin module
# We will use this object to call the plugin API
spads=perl.TimePlugin

# This is the first version of the plugin
pluginVersion='0.1'

# This plugin requires a SPADS version which supports Python plugins
# (only SPADS versions >= 0.12.29 support Python plugins)
requiredSpadsVersion='0.12.29'

# We define 2 global settings (mandatory for plugins implementing new commands):
# - commandsFile: name of the plugin commands rights configuration file (located in etc dir, same syntax as commands.conf)
# - helpFile: name of plugin commands help file (located in plugin dir, same syntax as help.dat)
globalPluginParams = { 'commandsFile': ['notNull'],
'helpFile': ['notNull'] }
presetPluginParams = None


# This is how SPADS gets our version number (mandatory callback)
def getVersion(pluginObject):
return pluginVersion

# This is how SPADS determines if the plugin is compatible (mandatory callback)
def getRequiredSpadsVersion(pluginName):
return requiredSpadsVersion

# This is how SPADS finds what settings we need in our configuration file (mandatory callback for configurable plugins)
def getParams(pluginName):
return [ globalPluginParams , presetPluginParams ]



# This is the class implementing the plugin
class TimePlugin:

# This is our constructor, called when the plugin is loaded by SPADS (mandatory callback)
def __init__(self,context):

# We declare our new command and the associated handler
spads.addSpadsCommandHandler({'time': hSpadsTime})

# We call the API function "slog" to log a notice message (level 3) when the plugin is loaded
spads.slog("Plugin loaded (version %s)" % pluginVersion,3)


# This is the callback called when the plugin is unloaded
def onUnload(self,reason):

# We remove our new command handler
spads.removeSpadsCommandHandler(['time'])

# We log a notice message when the plugin is unloaded
spads.slog("Plugin unloaded",3)



# This is the handler for our new command
def hSpadsTime(source,user,params,checkOnly):

# checkOnly is true if this is just a check for callVote command, not a real command execution
if checkOnly :

# time is a basic command, we have nothing to check in case of callvote
return 1

# We get current time using "now" function of datetime class from datetime module
current_time = datetime.datetime.now()
current_time_string = current_time.strftime("%H:%M:%S")

# We call the API function "answer" to send back the response to the user who called the command
# using same canal as he used (private message, battle lobby, in game message...)
spads.answer("Current local time: %s" % current_time_string)

0 comments on commit bcae1c6

Please sign in to comment.