Anti-Command Spam Protection pawn

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

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Matite

new commandCount[MAX_PLAYERS];
new commandInterval = 10; // Command interval in seconds

public OnPlayerCommandText(playerid, cmdtext[])
{
    if (commandCount[playerid] >= 3)
    {
        SendClientMessage(playerid, COLOR_RED, "You have entered too many commands! You cannot use commands for a while.");
        return 0;
    }

    commandCount[playerid]++;

    SetTimerEx("ResetCommandCount", commandInterval * 1000, false, "d", playerid);

    return 1;
}

public ResetCommandCount(playerid)
{
    commandCount[playerid] = 0;
    return 1;
}


Explanation:

We declare an array called commandCount to keep track of the command 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 commandInterval variable to determine the time interval in seconds during which the command count is checked.
In the OnPlayerCommandText function, we check if the command count for the player exceeds or equals 3. If it does, we send a message to the player indicating that they have entered too many commands, and we return 0 to prevent the command from being processed.
If the command count is below 3, we increment the command count for the player.
We use the SetTimerEx function to set a timer that will call the ResetCommandCount function after the specified command interval (converted to milliseconds) to reset the command count for the player.
The ResetCommandCount function simply sets the command count for the player back to 0.
This code implements a basic anti-command spam protection by tracking the number of commands entered by each player within a certain interval. If the command limit is reached, further commands are blocked for a while. You can customize the command interval and the anti-spam message according to your specific needs.