Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: . on Mar 12, 2015, 04:38 PM

Title: [Snippet] Simple censor function!
Post by: . on Mar 12, 2015, 04:38 PM
Someone asked me today about something like this because he had trouble implementing one so I decided to make a simple implementation for anyone who needs it. The code is very simple and should be fairly straight forward.

Snippet:
local _CENSOR_WORDS = [
    "poop", "cunt",
    "jerk", "crap",
    "shit", "faggot"
    // More words follow ...
];

local _CENSOR_MASK = "****************************************************************";

function Censor(text)
{
    if (typeof text != "string") return "";
   
    local str = text, pos = null;

    foreach (word in _CENSOR_WORDS)
    {
        while (pos = str.find(word)) {
            str = format(@"%s%s%s",
                str.slice(0, pos),
                _CENSOR_MASK.slice(0, word.len()),
                str.slice(pos + word.len()));
        }
    }
    return str;
}

Example:
print( Censor("Test for jerk, cunt, shit and then for some poop, faggot, crap!\n") );
print( Censor("Clean string!") );
print( Censor("") ); // Empty
print( Censor(32) ); // Invalid

Output:
Test for ****, ****, **** and then for some ****, ******, ****!
Clean string!

NOTE: Sorry for the words in the code but I'd expect you to understand that they are just for example.
Title: Re: [Snippet] Simple censor function!
Post by: Thijn on Mar 12, 2015, 05:05 PM
If this is going to be used for onPlayerChat I suggest you return what's given if it isn't a string.
Title: Re: [Snippet] Simple censor function!
Post by: DizzasTeR on Mar 12, 2015, 05:09 PM
Quote from: S.L.C on Mar 12, 2015, 04:38 PMSomeone asked me today about something like this because he had trouble implementing on.

meh... anyway i learned something new from this.
Title: Re: [Snippet] Simple censor function!
Post by: . on Mar 12, 2015, 05:15 PM
Quote from: Thijn on Mar 12, 2015, 05:05 PMIf this is going to be used for onPlayerChat I suggest you return what's given if it isn't a string.

I did that for the sake of consistency. Because the function should be always (strong guarantee) return a string (empty or not). But yeah, I made the update. It should be easy to change it back in case anyone needs it.
Title: Re: [Snippet] Simple censor function!
Post by: Sebastian on Mar 12, 2015, 05:32 PM
Heh, really useful. Good job !

@Doom_Killer, see ? It is possible. haha. 8)
Title: Re: [Snippet] Simple censor function!
Post by: Kratos_ on Mar 12, 2015, 05:44 PM

Nice . Converting text to lowercase will make it case-insensitive .
Title: Re: [Snippet] Simple censor function!
Post by: DizzasTeR on Mar 12, 2015, 05:47 PM
Quote from: Kratos_ on Mar 12, 2015, 05:44 PMNice . Converting text to lowercase will make it case-insensitive .

Just take it tolower....
Title: Re: [Snippet] Simple censor function!
Post by: . on Mar 12, 2015, 06:05 PM
Updated to support word duplicates like:
print( Censor("Just shit as shit in shit!") );Output:
Just **** as **** in ****!
I forgot about those. Next update will be about case insensitivity.
Title: Re: [Snippet] Simple censor function!
Post by: FinchDon on Sep 19, 2015, 04:58 PM
Sorry for Bump I add them Above ScriptLoad

and add this onPlayerChat

Censor( text ); but when i type faggot etc it show faggot not ****** why
Title: Re: [Snippet] Simple censor function!
Post by: KAKAN on Sep 19, 2015, 05:34 PM
READ what Thijn has posted^^
Quote from: Thijn on Mar 12, 2015, 05:05 PMIf this is going to be used for onPlayerChat I suggest you return what's given if it isn't a string.
Title: Re: [Snippet] Simple censor function!
Post by: . on Sep 19, 2015, 05:37 PM
Have you added the word in the _CENSOR_WORDS array? If you haven't , then how is the script supposed to know about it. Can you post the whole onPlayerChat fuction? Im sure that you forgot to block the original message and send the censored one instead.
Title: Re: [Snippet] Simple censor function!
Post by: KAKAN on Sep 20, 2015, 10:14 AM
Ahh wait. A bug
Try using print(Censor("jerk"));
And see it won't work, it doesn't work for the 1st word, but it works in other part of the sentence
Example:-
print(Censor("jerk"));Output:-[SCRIPT]jerk
Try this:-
print(Censor("Test for jerk"));Output:-[SCRIPT]Test for ****
Title: Re: [Snippet] Simple censor function!
Post by: KAKAN on Sep 20, 2015, 10:18 AM
And yes how can we do it in .tolower() form? I use it in onplayerchat, and I don't want the messages in .tolower() form, so any way to do it?
Title: Re: [Snippet] Simple censor function!
Post by: FinchDon on Sep 20, 2015, 10:55 AM
function onPlayerChat( player, text )
{
Censor(" " + text + " " );
Censor(text);
print( " " + player.Name + " : " + text + "." );
local spammer = (GetTickCount()-MSNCount[player.ID])/MSNxSEC;
    MSNPlayer[player.ID]+=spammer-1;
    if(MSNPlayer[player.ID]>LIMIT_REP_SPAM)MSNPlayer[player.ID]=LIMIT_REP_SPAM-1;
    if(MSNPlayer[player.ID]<0)MSNPlayer[player.ID]= -1;
        MSNCount[player.ID]=GetTickCount();
        if(MSNPlayer[player.ID]<0) {
        Message(">> Auto-Kick " + player.Name + " "+ "Reason: [ Spam ].");
      player.Kick();
  }
EchoMessage( "** [" + player.ID + "] " + player.Name + "  " + text + "." );
if(text[0].tochar().tostring()=="!") { local texto=split(text,"! "); local momo=""; for(local i=1;i<texto.len();i++){momo=momo+texto[i]+" ";} onPlayerCommand2(player,texto[0],momo); }
if ( pstats[ player.ID ].Muted == true )
{
PrivMessage( player, "You are currently muted so don't spam wait until 30 seconds");
return 0;
}
return 1;
}

Here you go...
Title: Re: [Snippet] Simple censor function!
Post by: DizzasTeR on Sep 20, 2015, 10:57 AM
Here is the fixed one.
function Censor(text)
{
    if (typeof text != "string") return "";
   
    local str = text, pos = null;

    foreach (word in _CENSOR_WORDS)
    {
        while (str.find(word, 0) != null) {
        pos = str.find(word);
        str = format(@"%s%s%s",
        str.slice(0, pos),
        _CENSOR_MASK.slice(0, word.len()),
        str.slice(pos + word.len()));
        }
    }
    return str;
}

Sample:
print( Censor( "shit this shit works" ) );

// Output:

[SCRIPT] **** this **** works

Edit: @FinchDon, You are just passing the chat, the function does its job but you are storing the string which it returns, you need to do it like this:
function onPlayerChat( player, text )
{
    local r_string = Censor( text );
    Message( format( "%s: %s", player.Name, r_string ) );
    return 0;
}
Title: Re: [Snippet] Simple censor function!
Post by: KAKAN on Sep 20, 2015, 11:40 AM
what about .tolower?
Title: Re: [Snippet] Simple censor function!
Post by: EK.IceFlake on Sep 20, 2015, 11:56 AM
@Doom_Killer
this better
function onPlayerChat( player, text )
{
    local r_string = Censor( text );
    ClientMessageToAll( format( "%s: [#FFFFFF]%s", player.Name, r_string ), player.Color.r, player.Color.g, player.Color.b );
    return 0;
}
this will allow for team colors in chat.