Graalians

Graalians (https://www.graalians.com/forums/index.php)
-   Creative Corner (https://www.graalians.com/forums/forumdisplay.php?f=17)
-   -   Script: Swear filter (https://www.graalians.com/forums/showthread.php?t=8604)

Emera 07-17-2012 05:58 PM

Script: Swear filter
 
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_PATH0);
  }
}

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 (
temp.filter) {
          
temp.word f.tokenize(":")[0];
          
temp.rule f.tokenize(":")[1];
          if (!({
temp.wordtemp.rulein temp.rules)) {
            
temp.rules.add({temp.wordtemp.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.0temp.this.filterRule.size(); temp.++) {
    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.badWordtemp.fullSentence);
      } else {
        
// For weaker language, the word is just replaced
        // with the rule defined in the rules.txt document
        
ChatBar.text replaceText(ChatBar.texttemp.badWordtemp.replacementWord);
      }
    }
  }
}

//FUNCTIONS

function loadFilterRules() {
  
triggerServer("gui"name"loadFilterRules");
}

//Dusty's replace text function
function replaceText(txtab) {
  if (
txt.pos(a) < 0) return txt;
  
temp.txtpos txt.positions(a);
  
temp.newtxt txt.substring(0txtpos[0]);
  for (
temp.0txtpos.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!

Dusty 07-17-2012 06:22 PM

In my opinion censoring should be done clientside with the option to turn it on or off. Therefor it should be applied to OTHER player chat rather than your own(using I think it's onRemotePlayerChats() {}). This is because it discourages swear evasion. Those who feel comfortable with cursing can see/do it without feeling censored, so no need to evade it. Thus those who don't like cursing can have other chat censored and since they don't need to evade it remains far more accurate.

Also it'd probably be better to just load them from the serveroptions rather than a file.

Higbey 07-17-2012 06:25 PM

also if you are going to make it so there is no option make it so the word is replaced or censored, not the entire pm or text.

Emera 07-17-2012 06:27 PM

Quote:

Posted by Dusty (Post 163027)
In my opinion censoring should be done clientside with the option to turn it on or off. Therefor it should be applied to OTHER player chat rather than your own(using I think it's onRemotePlayerChats() {}). This is because it discourages swear evasion. Those who feel comfortable with cursing can see/do it without feeling censored, so no need to evade it. Thus those who don't like cursing can have other chat censored and since they don't need to evade it remains far more accurate.

Also it'd probably be better to just load them from the serveroptions rather than a file.

I've never used onRemotePlayerChats before, so I'll look into it. I'll also add an option to enable/disable it. I'll also work on adding functionality to use the serveroptions to store words too. Thanks for the feedback (and your replaceText() function)

Quote:

Posted by Higbey (Post 163028)
also if you are going to make it so there is no option make it so the word is replaced or censored, not the entire pm or text.

I pointed out that it removes the word from the chat, and doesn't replace the whole chat text (unless the ERASE rule is defined next to it in the text document)

Update

I've added onRemotePlayerChats() and the ability to turn it on and off (thanks Dusty for the feedback)
I don't currently have access to anywhere where I can play around with the server options (other than Delteria, but that's a no-go) so I'll have to find somewhere so I can add filter via server ops. I've also removed sending a message to RC and loggin vulgar language, since there's really no need for it in my opinion.

PHP Code:

const RULES_PATH "storage/filter/rules.txt";

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 (
temp.filter) {
          
temp.word f.tokenize(":")[0];
          
temp.rule f.tokenize(":")[1];
          if (!({
temp.wordtemp.rulein temp.rules)) {
            
temp.rules.add({temp.wordtemp.rule});
          }
        }
        
triggerClient("gui"name"loadedFilterRules"temp.rules);
      }
      break;
    }
  }
}

//#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() {
  if (
ChatBar.text == "/filter") {
    
client.swearFilter = !client.swearFilter;
    if (
client.swearFilter) {
      
ChatBar.text "Swear filter has been enabled!";
    } else {
      
ChatBar.text "Swear filter has been disabled!";
    }
  }
}

function 
onPlayerChats() {
  
// Also notifies the player that they've used
  // bad words when their filter is on.
  
onRemotePlayerChats(playerplayer.chat);
}

function 
onRemotePlayerChats(objchat) {
  if (
client.swearFilter != true) return;
  for (
temp.0temp.this.filterRule.size(); temp.++) {
    if (
obj.chat.pos(this.filterRule[temp.i][0]) >= 0) {
      if (
this.filterRule[temp.i][1] == "ERASE") {
        
obj.chat REPLACEMENT_TEXT;
      } else {
        
obj.chat replaceText(obj.chatthis.filterRule[temp.i][0], this.filterRule[temp.i][1]);
      }
    }
  }
}

//FUNCTIONS

function loadFilterRules() {
  
triggerServer("gui"name"loadFilterRules");
}

//Dusty's replace text function
function replaceText(txtab) {
  if (
txt.pos(a) < 0) return txt;
  
temp.txtpos txt.positions(a);
  
temp.newtxt txt.substring(0txtpos[0]);
  for (
temp.0txtpos.size(); i++) {
    
newtxt @= b;
    
newtxt @= txt.substring(txtpos[i] + a.length(), txt.substring(txtpos[i] + a.length()).pos(a));
  }
  return 
newtxt;


Bugs, errors or improvements, let me know.

Dusty 07-17-2012 06:31 PM

Enabling/Disabling it won't work in this instance as it's only filtering your own text so it'll end up like this:

PlayerA has censoring disabled.
PlayerB has censoring enabled.

PlayerA says <expletive>, but because he doesn't have censoring onPlayerChat() doesn't change his curse.

PlayerB DOES have it enabled, but because it's not his chat, he still sees the player cursing.

This is where onRemotePlayerChats() is needed.

Warp 07-17-2012 07:20 PM

Ok so this solves all of these words:

-****
-****
-****
-Balls
-*****
-Knob
-****
-*****
-****

and so on :3

Higbey 07-17-2012 07:31 PM

Quote:

Posted by Warp (Post 163052)
Ok so this solves all of these words:

-****
-****
-****
-Balls
-*****
-Knob
-****
-*****
-****

and so on :3

balls and knob shouldnt be censored as they have commonly used other meanings as well,

callimuc 07-17-2012 07:34 PM

Quote:

Posted by Warp (Post 163052)
Ok so this solves all of these words:

-****
-****
-****
-Balls
-*****
-Knob
-****
-*****
-****

and so on :3

/filter
*Swear filter has been disabled!*

Clown 07-24-2012 09:11 PM

*looks through scripts desperately to see swear words, it's like p/o/r/n to kids with very strict parents*

Jester Lapse 07-25-2012 10:00 AM

Quote:

Posted by Warp
Ok so this solves all of these words:

-****
-****
-****
-Balls
-*****
-Knob
-****
-*****
-****

and so on :3

Since when is **** a bad word? It's just stars :(

Emera 07-25-2012 12:20 PM

Quote:

Posted by Jester Lapse (Post 167321)
Since when is **** a bad word? It's just stars :(

Man don't use that word here! It's the worst of the worst.


All times are GMT. The time now is 10:01 PM.

Powered by vBulletin/Copyright ©2000 - 2025, vBulletin Solutions Inc.