Scripts:BadWords

From BF2 Technical Information Wiki
Jump to navigation Jump to search

Introduction

I own a cybercafe / game center where the most popular game is BF2. Most of our customers are teenagers, but we get quite a few younger and older gamers, too. The parents of the younger ones are naturally concerned about some of the language used in chat messages in online games; even quite a few older gamers find some of racist and related language used by some online gamers to be offensive. To help keep our servers feeling friendly to everyone, I wrote this script.

The basic idea is that when the server starts, the script reads a configuration file that contains words you want to prohibit players from using in their names and chat messages; the format of the file is just a list of "bad" words, one per line. Every time a player connects, the script scans the players name for the presence of any of bad words; every time a player sends a chat message, the chat message is similarly scanned. If the script finds any of the bad words in a name or chat message, the player is immediately kicked using the PunkBuster kick facility (so that they can be given a meaningful reason why they were kicked), other players are notified about what happened to them, and a log message is printed.

The name of the file containing the list of bad words, and the length of time they will be kicked for, are set in variables at the beginning of the script. NOTE: be careful with your choices of words for the bad words file--the script is written to catch these words occuring in any kind of substring in chat messages and names, so if you're not careful you can find your server is kicking people who are using perfectly innocent names.

It's probably also a good idea to warn people if you're using software like this on your server: use a script like this or this, for example, to tell people they will be automatically kicked if they use inappropriate language.

To install the script, just import it from one of the main BF2 scripts, such as Battlefield 2 Server/admin/default.py

Code

 '''badWords: Module to kick players for using "bad" words.
   
    Version 0.01, 2005 August 3, Forrest Thiessen'''
 
 #Copyright (c) 2005 Cyberscape Enterprises, Inc., www.cyberscapearena.com
 #
 #Permission is hereby granted, free of charge, to any person obtaining a
 #copy of this software and associated documentation files (the "Software"),
 #to deal in the Software without restriction, including without limitation
 #the rights to use, copy, modify, merge, publish, distribute, sublicense,
 #and/or sell copies of the Software, and to permit persons to whom the
 #Software is furnished to do so, subject to the following conditions:
 #
 #The above copyright notice and this permission notice shall be included
 #in all copies or substantial portions of the Software.
 #
 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 #OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 #FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 #DEALINGS IN THE SOFTWARE.
 #
 #Battlefield 2 is a trademark of Digital Illusions CE AB. Electronic Arts,
 #EA, EA GAMES and the EA GAMES logo are trademarks or registered trademarks
 #of Electronic Arts Inc. in the U.S. and/or other countries. EA GAMES(tm) is
 #an Electronic Arts(tm) brand.
 
 import host
 import bf2
 import time
 
 badWordsFileName = "admin/BadWords.con"  # File containing list of bad words, one per line
 kickTime = 10   # Default kick time in seconds
 
 
 badWords = []
 
 
 # Open the file, read the bad word list, and register handlers
 try:
     badWordsFile = open(badWordsFileName, 'r')
     # Read the file into a list, each element being one bad word
     badWords = [word.strip() for word in badWordsFile.readlines()]
    
     # Register handlers to receive chat messages and player connections
     host.registerHandler('ChatMessage', onChatMessage, 1)
     host.registerHandler('PlayerConnect', onPlayerConnect, 1)
    
     print 'BadWords: BadWords module initialized with', len(badWords), 'words.'
 
 except IOError:
     print "BadWords: Couldn't open bad words file", fileName
 
 
 # As players connect to the server, check if their names contain any of
 # the bad words, and kick them if they do.
 def onPlayerConnect(playerObject):
     global badWords, kickTime
    
     name = playerObject.getName()
    
     for word in badWords:
         if name.find(word) == -1: continue
        
         host.rcon_invoke('game.sayall "Player %s kicked for having an inappropriate name"'
                          % playerName)
         host.rcon_invoke('pb_sv_kick "%s" %i "Inappropriate name"'
                          % (playerName, kickTime))
         print ( 'BadWords: Player %s kicked for having inappropriate name at %s'
                 % (playerName, time.strftime("%a %d-%b-%Y %H:%M")) )
        
         break
 
 
 # Whenever a chat message is sent on any channel, check it for bad words; if
 # there are any, kick the player who sent the message.
 def onChatMessage(playerID, msgText, channel, flags):
     global badWords, kickTime
    
     if playerID < 0: return  # The server operator can be a potty-mouth if he wants
 
     for word in badWords:
         if msgText.find(word) == -1: continue
 
         playerName = bf2.playerManager.getPlayerByIndex(playerID).getName()
         host.rcon_invoke('game.sayall "Player %s kicked for using inappropriate language"'
                          % playerName)
         host.rcon_invoke('pb_sv_kick "%s" %i "Using inappropriate language"'
                          % (playerName, kickTime))
         print ( 'BadWords: Player %s kicked for saying "%s" at %s'
                % (playerName, word, time.strftime("%a %d-%b-%Y %H:%M")) )
        
         break

Discussion

There are lots of variations possible on this script: you could ban players instead of kicking them; you could warn them three times first, then kick them; you could save their profile ID or CD key for future action if they repeat their offense, etc. This script was written to be both simple and useful, but you could also extend the basic concept shown here to use regular expressions (to avoid kicking player "gilbert_grape", for example, if the word "rape" was on your bad words list), at the expense of complicating both the script and the word list.

It should also be pointed out that PunkBuster has a similar facility for kicking players based on bad words in their player names; I've found it to be a pain to configure, however, and it does nothing for chat messages, which is where the larger problem is. . . plus it leaves a lot of the "bad" words on your server console screen every time it loads its word list (not good for tours).