Anti-Spam onplayertext

Started by Matite, May 23, 2023, 05:45 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Matite




new spamCount[MAX_PLAYERS];
new spamInterval = 5; // Spam interval in seconds

public OnPlayerText(playerid, text[])
{
    if (spamCount[playerid] >= 3)
    {
        SendClientMessage(playerid, COLOR_RED, "Spamming is not allowed! You cannot send messages for a while.");
        return 0;
    }

    spamCount[playerid]++;

    SetTimerEx("ResetSpamCount", spamInterval * 1000, false, "d", playerid);

    return 1;
}

public ResetSpamCount(playerid)
{
    spamCount[playerid] = 0;
    return 1;
}


Explanation:

We declare an array called spamCount to keep track of the spam count for each player. It has a maximum size of MAX_PLAYERS, which represents the maximum number of players on the server.
We set the spamInterval variable to determine the time interval in seconds during which the spam count is checked.
In the OnPlayerText function, we check if the spam count for the player exceeds or equals 3. If it does, we send a message to the player indicating that spamming is not allowed, and we return 0 to prevent the message from being sent.
If the spam count is below 3, we increment the spam count for the player.
We use the SetTimerEx function to set a timer that will call the ResetSpamCount function after the specified spam interval (converted to milliseconds) to reset the spam count for the player.
The ResetSpamCount function simply sets the spam count for the player back to 0.
This code implements a basic anti-spam mechanism by tracking the number of messages sent by each player within a certain interval. If the spam limit is reached, further messages are blocked for a while. You can customize the spam interval and the anti-spam message according to your specific needs.