This is a swear filter you can use on your Graal server to stop players chatting offensive or vulgar words. The words and rules are stored in a text document on your server.
Features- Text file filter storage
- Easily customizable settings to suit your server
- Easy way to add and edit filters via the text file
- You can set a rule to either censor the particular bad word, or clear the whole sentence
Setting up
1) Upload the script to your server.
2) Edit all of the constants in the script. Here's what you can edit, and what effect they will have on the script.
- RULES_PATH - Change this to where you want the rules file to be saved.
- VULGAR_WARNING - If set to true, any words that are chatted and marked with an ERASE rule will send a message to RC and will be logged in a log file.
- REPLACEMENT_TEXT - This is the player will chat when they chat a word marked with the ERASE rule.
3) Open the rights of the NPC-Server and yourself, and add yourself rw rights to the file path you decided upon (RULES_PATH). For example, the default is
rw storage/filter/rules.txt.
4) Set up the rules.txt file. The server will have automatically created a rules.txt and put it in the folder you chose (RULES_PATH) To add a bad word to the file, add a new line and on it put
badword:replacement. Obviously, you'll need to change badword and replacement to what you want it to be. To completely clear the sentence, set the rule to ERASE.
Code
Place this in a weapon NPC.
PHP Code:
const RULES_PATH = "storage/filter/rules.txt";
const VULGAR_WARNING = true;
function onCreated() {
if (fileExists(RULES_PATH) != true) {
echo("[Filter]: Creating rules file (" @ RULES_PATH @ ")");
temp.lines = {
"badword:replacementtext"
};
temp.lines.saveLines(RULES_PATH, 0);
}
}
function onActionServerside() {
switch(params[0]) {
case "loadFilterRules": {
temp.path = RULES_PATH;
if (fileExists(temp.path) != true) {
echo("[Filter]: Could not find chat filter file!");
} else {
temp.filter.loadLines(temp.path);
for (f : temp.filter) {
temp.word = f.tokenize(":")[0];
temp.rule = f.tokenize(":")[1];
if (!({temp.word, temp.rule} in temp.rules)) {
temp.rules.add({temp.word, temp.rule});
}
}
triggerClient("gui", name, "loadedFilterRules", temp.rules);
}
break;
}
case "vulgarWordBlocked": {
savelog2("badwords.txt", player.account SPC "used vulgar word '" @ params[1] @ "' in chat '" @ params[2] @ "'");
// Set VULGAR_WARNING to true to send an alert to
// RC when a player uses a vulgar message
if (VULGAR_WARNING == true) {
echo("[Filter]: Player '" @ player.account @ "' used vulgar word '" @ params[1] @ "' in chat '" @ params[2] @ "'");
}
}
}
}
//#CLIENTSIDE
const REPLACEMENT_TEXT = "Don't use this kind of language!";
function onCreated() {
loadFilterRules();
}
function onActionClientside() {
switch(params[0]) {
case "loadedFilterRules": {
this.filterRule = params[1];
break;
}
}
}
function ChatBar.onAction() {
for (temp.i = 0; temp.i < this.filterRule.size(); temp.i ++) {
if (ChatBar.text.pos(this.filterRule[temp.i][0]) >= 0) {
temp.fullSentence = ChatBar.text;
temp.badWord = this.filterRule[temp.i][0];
temp.replacementWord = this.filterRule[temp.i][1];
// Used to completely omit vulgar language from the text
if (temp.replacementWord == "ERASE") {
ChatBar.text = REPLACEMENT_TEXT;
triggerServer("gui", name, "vulgarWordBlocked", temp.badWord, temp.fullSentence);
} else {
// For weaker language, the word is just replaced
// with the rule defined in the rules.txt document
ChatBar.text = replaceText(ChatBar.text, temp.badWord, temp.replacementWord);
}
}
}
}
//FUNCTIONS
function loadFilterRules() {
triggerServer("gui", name, "loadFilterRules");
}
//Dusty's replace text function
function replaceText(txt, a, b) {
if (txt.pos(a) < 0) return txt;
temp.txtpos = txt.positions(a);
temp.newtxt = txt.substring(0, txtpos[0]);
for (temp.i = 0; i < txtpos.size(); i++) {
newtxt @= b;
newtxt @= txt.substring(txtpos[i] + a.length(), txt.substring(txtpos[i] + a.length()).pos(a));
}
return newtxt;
}
As per usual, if you find any bugs or want me to improve something, leave a post in this thread and I'll address it as soon as possible. Also, thank you Dusty for his replaceText() function. Enjoy!