[Tutorial] Simple and effective Anti-Spam
#1

hi SAMPers

Due to some requests via pm and the need of an anti-spam script myself i decided to make one and also post it as a tutorial as it seems there wouldnt be any good anti spam scripts released.
I dont like any additional timers in my gamemode, so i didnt use any in this script as well. (except the automatic unmute timer)

So, what is this tutorial about?

This tutorial shows you how to make a simple and effective anti spam for your server chat (you can use the same method/variables also for a command anti spam ofc ) without any timers.

Difficulity: Easy

So lets get started:

pawn Code:
//Definings:
new SpamCount[MAX_PLAYERS];
new ChatSpamTime[MAX_PLAYERS][3];
new muted[MAX_PLAYERS];

//How long it takes a player gets unmuted automatically (in minutes):
#define AutoUnmuteTime 2 //You can adjust this to your needs ofc ;)
pawn Code:
public OnPlayerText(playerid, text[])//This Callback is called as soon as a player types smth. into the chat.
{

    if(muted[playerid] == 1)//If the player has been muted he will get this message:
    {
        SendClientMessage(playerid, 0xFF0000FF, "You have been muted!");
        return 0;
    }
    else
    {
        if(Player[playerid][AdminLevel]==0)//To prevent admins beeing muted. Ofc you have to adjust this!
        {
            AntiSpam(playerid);//A stock function we will set up later...
        }
        //Your other chat codes here...
    }
    return 1;
}
pawn Code:
stock AntiSpam(playerid)//stock function
{
    SpamCount[playerid]++;//Everytime a player types smth. in the chat this variable gets increased by one.
    switch (SpamCount[playerid])//switch between the SpamCount variable.
    {
        case 1://If a player types smth. into the chat for the first time...
        {
            ChatSpamTime[playerid][0]=gettime();//Get the unix timestamp and save it into a variable.
            //ChatSpamTime[playerid][0] this variable is our first variable. The array is "0" for the first one not "1"!
        }
        case 2://If the player types smth. into the chat the second time...
        {
            if((gettime()-ChatSpamTime[playerid][0])<4)//We check if the Time between the players first and
            {
                //second chat is more or less than 4 seconds. In case the player chatted faster than 4 seconds
                //We will give him his first warning ...
                SendClientMessage(playerid,0xFF0000FF,"You have received a warning (1/3)! (Reason: Spam)");
                ChatSpamTime[playerid][1]=gettime();//and we save his chat time again to another variable.

                //Lets take a closer look at this: gettime()-ChatSpamTime[playerid][0].
                //We use gettime() to get the current unix timestamp and we simply subtract the time we
                //saved before (as the player chatted the first time). Always make sure to subtract the older
                //timestamp from the newer one as the newer timestamp is always bigger!
            }
            else SpamCount[playerid]=0;//If the player needed longer than 4 seconds to send another
            //chat message we reset his SpamCount variable bac to 0. (as he is obviously not spamming or a very slow spammer ;) )
        }
        case 3://If the player types smth. into the chat the third time...
        {
            if((gettime()-ChatSpamTime[playerid][1])<4)//We check how long it took the player to send another message after the 2nd one.
            {
                //If the player send another message faster than 4 seconds he is obviously spamming and we give
                //him a 2nd warning as we dont want to be that cruel to spammers right? ;)
                SendClientMessage(playerid,0xFF0000FF,"You have received a warning (2/3)! (Reason: Spam)");
                ChatSpamTime[playerid][2]=gettime();//Again save his chat time for this message to another variable.
            }
            else SpamCount[playerid]=0;//In case the player calmed down, reset his SpamCount variable.
        }
        case 4..50://In case the certain player gets the idea to spam again...
        {
            new string[128],name[24];
            GetPlayerName(playerid,name,24);//Save the spammers name into the string variable "name".
            if((gettime()-ChatSpamTime[playerid][2])<4)//We get sure again if it took the player less than 4 secs.
            {
                //If so then ...
                format(string,sizeof(string),"Player %s has been muted for 2 minutes! (Reason: Spam!)",name);
                SendClientMessageToAll(0xFF0000FF,string);
                //We send a message visible for all players...
                SendClientMessage(playerid,0xFF0000FF,"You have received your final warning (3/3)! (Reason: Spam)");
                //Send the player another warning messsage ;)
                muted[playerid]=1;//we mute him
                SetTimerEx("AutoUnMute",AutoUnmuteTime*60000,false,"i",playerid);//And finally set the timer for the automatic unmute.
//==> 60000 milliseconds = 1 minute. So AutoUnmuteTime that has been defined on top of the script can be used
//with minutes (you dont have to enter the time in milliseconds but in minutes)
            }
            //Lets take a closer look at this: case 4..50:
            //A player could possibly get a higher SpamCount variable than 4, in this case he wouldnt be muted!
            //But its impossible to chat 50 times within 4 seconds right? ;)
        }
    }
    return 1;
}
pawn Code:
public OnPlayerConnect(playerid)
{
    SpamCount[playerid]=0;//Reset the SpamCount variable so no player can get muted wrongly after connect!
    muted[playerid]=0;//same for mute variable!
    return 1;
}
pawn Code:
forward AutoUnMute(pID);//forward our timer.
public AutoUnMute(pID)//This is our timer that unmutes the muted player automatically.
{
    muted[pID] = 0;
    SpamCount[pID]=0;
    //Unmute the player and inform him about the new circumstance ;)
    SendClientMessage(pID, 0xFF0000FF, "You have been unmuted automatically!");
    return 1;
}
Thats pretty much it! You wont see any spammers on your server anymore

Sometimes i suck at explaining so in case you didnt understand smth. i would be glad to help you.
Comments and suggestions are appreciated as well

I hope this will be helpfull to some of you.


Best regards, wolf.
Reply
#2

Good job wolf, this looks very good.
Reply
#3

thx tanush
Reply
#4

Nice tutorial, well explained, but I think it would be easier for noobs to explain why you used "AutoUnMuteTime*60000" (I am talking about the bold part) and maybe also say whether its in milliseconds, seconds or minutes
Reply
#5

thx.
Ive added a short comment about that as well
Reply
#6

Good Tutorial, Seems very effective.
Thanks for the tutorial mate
Reply
#7

Quote:
Originally Posted by BlackWolf120
View Post
as it seems there wouldnt be any good anti spam scripts released.
You base that on what? A simple search brings dozens of anti-spam scripts. Some being better than yours.
Reply
#8

THANKS WOLF!
Reply
#9

Great work, I really needed something like this.

Although I could barely get it in, my other developer got it working. Thank you.

Legit.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)