Jump to content

[HELP] Anti spam command script


y453bh

Recommended Posts

Posted (edited)

hello!

 

I don't want it to kick people, just that it doesn't allow you to do a command more than 1 time in a certain time, thanks!

Edited by y453bh
Posted (edited)
Just now, Patrick said:

Just replace kickPlayer function to your outputChatBox.

It doesnt work already tried it, also I told you that i want some commands, not all, i dont know how to do it

Edited by y453bh
  • Moderators
Posted
1 minute ago, y453bh said:

It doesnt work already tried it, also I told you that i want some commands, not all, i dont know how to do it

local CMD_INTERVAL        = 200 --// The interval that needs to pass before the player can execute another cmd
local KICK_AFTER_INTERVAL = 50  --// Kick the player if they execute more than 20 cmds/sec

local limitedCommands = {
	["something"] = true,
	["othercommand"] = true,
	-- list of limited commands
}

local executions = setmetatable({}, { --// Metatable for non-existing indexes
    __index = function(t, player)
        return getTickCount()-CMD_INTERVAL
    end
})

addEventHandler("onPlayerCommand", root,
    function(command)
    	if limitedCommands[command] then
        	if (executions[source]-getTickCount()<=CMD_INTERVAL) then
            	if (executions[source]-getTickCount()<=KICK_AFTER_INTERVAL) then 
                	outputChatBox("Don't flood", source)
            	end 
            	cancelEvent()
        	end
       		executions[source] = getTickCount()
      	end
    end
)

 

Posted
Just now, Patrick said:

local CMD_INTERVAL        = 200 --// The interval that needs to pass before the player can execute another cmd
local KICK_AFTER_INTERVAL = 50  --// Kick the player if they execute more than 20 cmds/sec

local limitedCommands = {
	["something"] = true,
	["othercommand"] = true,
	-- list of limited commands
}

local executions = setmetatable({}, { --// Metatable for non-existing indexes
    __index = function(t, player)
        return getTickCount()-CMD_INTERVAL
    end
})

addEventHandler("onPlayerCommand", root,
    function(command)
    	if limitedCommands[command] then
        	if (executions[source]-getTickCount()<=CMD_INTERVAL) then
            	if (executions[source]-getTickCount()<=KICK_AFTER_INTERVAL) then 
                	outputChatBox("Don't flood", source)
            	end 
            	cancelEvent()
        	end
       		executions[source] = getTickCount()
      	end
    end
)

 

The message appear but I still can make the command

  • Moderators
Posted

 

Interesting solution @Patrick , I am hardly using metatables so I have always trouble to get around the default table logic.

btw I am wondering is the rawset function not required to set the value on line 26?

 

  • Moderators
Posted
4 minutes ago, IIYAMA said:

Interesting solution @Patrick , I am hardly using metatables so I have always trouble to get around the default table logic.

This code from onPlayerCommand's wiki page, I just edited and put the command name check inside it.
 

6 minutes ago, IIYAMA said:

btw I am wondering is the rawset function not required to set the value on line 26?

Hmm, looks like unnecessary.

  • Like 1
  • Moderators
Posted (edited)
13 minutes ago, y453bh said:

Help @Patrick it still let me do the command

Here is my solution, I hope its good for you.

local LIMITED_COMMANDS = {
    -- ["COMMAND_NAME"] = TIME_LIMIT_IN_SEC
    ["something"] = 5,  -- Can use this command only once in every 5 sec
    ["spam"]      = 10, -- Can use this command only once in every 10 sec
}

local EXEC = {}

addEventHandler("onPlayerCommand", root, function(cmd)
    local limit_sec = LIMITED_COMMANDS[cmd]
    if not limit_sec then return end

    local tick = getTickCount()

    if not EXEC[source]      then EXEC[source] = {}     end
    if not EXEC[source][cmd] then EXEC[source][cmd] = 0 end

    if EXEC[source][cmd] + (limit_sec * 1000) > tick then
        cancelEvent()
        return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every "..limit_sec.." sec.", source, 255, 255, 255, true)
    end
    EXEC[source][cmd] = tick
end)

addEventHandler("onPlayerQuit", root, function()
    EXEC[source] = nil
end)

 

Edited by Patrick
Posted
7 hours ago, Patrick said:

Here is my solution, I hope its good for you.


local LIMITED_COMMANDS = {
    -- ["COMMAND_NAME"] = TIME_LIMIT_IN_SEC
    ["something"] = 5,  -- Can use this command only once in every 5 sec
    ["spam"]      = 10, -- Can use this command only once in every 10 sec
}

local EXEC = {}

addEventHandler("onPlayerCommand", root, function(cmd)
    local limit_sec = LIMITED_COMMANDS[cmd]
    if not limit_sec then return end

    local tick = getTickCount()

    if not EXEC[source]      then EXEC[source] = {}     end
    if not EXEC[source][cmd] then EXEC[source][cmd] = 0 end

    if EXEC[source][cmd] + (limit_sec * 1000) > tick then
        cancelEvent()
        return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every "..limit_sec.." sec.", source, 255, 255, 255, true)
    end
    EXEC[source][cmd] = tick
end)

addEventHandler("onPlayerQuit", root, function()
    EXEC[source] = nil
end)

 

Thanks for your help, but I added my script command and still can use it, also added the "me" command and I can't use it, the same message everytime ...

  • Moderators
Posted
12 hours ago, y453bh said:

Thanks for your help, but I added my script command and still can use it, also added the "me" command and I can't use it, the same message everytime ...

First of all... stop spamming...
And provide more informations about what are you doing, because it has to work.

Posted (edited)

Can you give me the lines for antispam only?, so I can add them to all my scripts?

Because this way isnt't working, please @Patrick

Edited by y453bh
  • Moderators
Posted (edited)
38 minutes ago, y453bh said:

Can you give me the lines for antispam only?, so I can add them to all my scripts?

Because this way isnt't working, please @Patrick

Oh.. you can't cancel client sided commands.


Then, here is one way, how to limit commands on client side:
 

local EXEC = {}

function canUse(cmd, limit_sec)
    if not EXEC[cmd] then EXEC[cmd] = 0 end
	
    local lastuse = EXEC[cmd]
    local tick    = getTickCount()
      
    if lastuse + ((limit_sec or 2) * 1000) > tick then
        return false
    end

    EXEC[cmd] = tick
    return true
end


-- EXAMPLE

function yourFunction(cmd)
    -- Need to add this part to your functions
    if not canUse(cmd, 2) then
        return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every 2 sec!")
    end

    -- your code ...
end
addCommandHandler("asd", yourFunction)

 

Edited by Patrick
Posted
37 minutes ago, Patrick said:

Oh.. you can't cancel client sided commands.


Then, here is one way, how to limit commands on client side:
 


local EXEC = {}

function canUse(cmd, limit_sec)
    if not EXEC[cmd] then EXEC[cmd] = 0 end
	
    local lastuse = EXEC[cmd]
    local tick    = getTickCount()
      
    if lastuse + ((limit_sec or 2) * 1000) > tick then
        return false
    end

    EXEC[cmd] = tick
    return true
end


-- EXAMPLE

function yourFunction(cmd)
    -- Need to add this part to your functions
    if not canUse(cmd, 2) then
        return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every 2 sec!")
    end

    -- your code ...
end
addCommandHandler("asd", yourFunction)

 

That's what I needed, thanks a lot @Patrick

Posted

Here is another one... you need just add the cmd to "cmdAntiSPAM" table like the example:

local cmdAntiSPAM = {
     -- {cmd,usageLimit,time,dataName},
    {"say",5,3000,"CMD_SAY"},
    {"help",1,3000,"CMD_HELP"},
}
function preventCommandSpam(command)
    for i,v in ipairs(cmdAntiSPAM) do
        if v[1] == command then
        local data = getElementData(source,v[4])
            if data then
            local spam = data+1
                if spam >= v[2] then
                    cancelEvent()
                    outputChatBox ("You need to wait before using this command again!", source, 255, 0, 0, false)
                else
                    setElementData(source,v[4],spam)
                end
            else
                setElementData(source,v[4],0)
                setTimer(removeSPAMData,v[3],1,source,v[4])
            end
        end
    end
end
addEventHandler("onPlayerCommand", getRootElement(), preventCommandSpam)

function removeSPAMData(player,data)
    if isElement(player) then
        removeElementData(player,data)
    end
end

I hope this can help you.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...