-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update Python plugin templates and tutorials
- Loading branch information
Showing
7 changed files
with
251 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |