Profanity filter
Posted: January 11th, 2012, 12:40 am
Swearing on servers can be annoying, so I thought I'd share this.
Its a simple profanity filter that I wrote. It works by replacing any bad words with a word of your choosing.
The filter works by replacing full words, that way people can still say words like "Scunthorpe"
1. Add a permission called "Swear" in the Permissions.cs
2. Go to chat.cs. Add these two at the top of the page.
Add just above public static bool SendGlobal:
Then you can add this into SendGlobal (Caution: contains swears)
3. You can replace string CensoredText with whatever word you like, so if i swear it will now say "block off you block". Feel free to use color codes too, as long as the string ends with %F or has '+"&F";' at the end.
4. In the configGUI, go to ranks and then you can tick "Swear" to each rank that you would like to be able to swear on the server. (Note the filter only works in general chat, players can still swear in staffchat and pm's.
5. When you start the server, when someone firsts talks, a new file in the root of your server will be made called "SwearWords.txt". When the server is shutdown, you can edit this file adding new swear words that you wish to be filtered. Make sure these are on new lines.
Since the filter works using full words, make sure you add things like f**k and f**king (ignore the asterixis). A list of default words will be added when the file is first made.
Its a simple profanity filter that I wrote. It works by replacing any bad words with a word of your choosing.
The filter works by replacing full words, that way people can still say words like "Scunthorpe"
1. Add a permission called "Swear" in the Permissions.cs
2. Go to chat.cs. Add these two at the top of the page.
Code: Select all
using System.Text.RegularExpressions;
using System.IO;
Code: Select all
public static List<string> Swears = new List<string>();
public static IEnumerable<Regex> badWordMatchers;
Code: Select all
if (!player.Can(Permission.Swear))
{
if (!File.Exists("SwearWords.txt"))
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("#This txt file should be filled with bad words that you want to be filtered out");
sb.AppendLine("#I have included some examples, excuse my language :P");
sb.AppendLine("fuck");
sb.AppendLine("fucking");
sb.AppendLine("fucked");
sb.AppendLine("dick");
sb.AppendLine("bitch");
sb.AppendLine("shit");
sb.AppendLine("shitting");
sb.AppendLine("shithead");
sb.AppendLine("cunt");
sb.AppendLine("nigger");
sb.AppendLine("wanker");
sb.AppendLine("wank");
sb.AppendLine("wanking");
sb.AppendLine("piss");
File.WriteAllText("SwearWords.txt", sb.ToString());
}
string CensoredText = Color.ReplacePercentCodes("&CBlock&F"); //you can change this. Always end in &F though
const string PatternTemplate = @"\b({0})(s?)\b";
const RegexOptions Options = RegexOptions.IgnoreCase;
if (Swears.Count == 0)
{
Swears.AddRange(File.ReadAllLines("SwearWords.txt").Where(line => line.StartsWith("#") == false || line.Trim().Equals(String.Empty)));
}
if (badWordMatchers == null)
{
badWordMatchers = Swears.
Select(x => new Regex(string.Format(PatternTemplate, x), Options));
}
string output = badWordMatchers.
Aggregate(rawMessage, (current, matcher) => matcher.Replace(current, CensoredText));
rawMessage = output;
}
}
4. In the configGUI, go to ranks and then you can tick "Swear" to each rank that you would like to be able to swear on the server. (Note the filter only works in general chat, players can still swear in staffchat and pm's.
5. When you start the server, when someone firsts talks, a new file in the root of your server will be made called "SwearWords.txt". When the server is shutdown, you can edit this file adding new swear words that you wish to be filtered. Make sure these are on new lines.
Since the filter works using full words, make sure you add things like f**k and f**king (ignore the asterixis). A list of default words will be added when the file is first made.