[Release?] Anti chat spam / Mute players

Hello!

Some time in your life as a user on Fivem you have probably encountered people spamming the chat with aids and you wanted to kill them so hard that you left the server because it got annoying.

Good news for you! I’ve made some simple but useful code that will stop players from spamming the chat with aids…

Please look under the “Mute” command if you want to add a permission system or something to stop random people from muting everyone and being a dick about it.

And if you want to change the delay between messages you can change the “max_delay” variable to your desired time.

local current_messages = {}
local max_delay = 5
local muted_players = {}


function Get_Messagedata(s)
    if current_messages[s] == nil then
        return 6
    end
    return (os.time() - current_messages[s]), max_delay + (current_messages[s] - os.time())
end


function Set_UserMute(user, time)
    muted_players[user] = os.time() + (time * 60)
end

function Get_UserMute(user)
    if muted_players[user] == nil then
        return 0
    end
    return muted_players[user] - os.time()
end

function timeCount(numSec)
    local nSeconds = numSec
    if nSeconds == 0 then
        return 0
    else
        local	nHours = math.floor(nSeconds/3600);
        local	nMins = math.floor(nSeconds/60 - (nHours*60));
        local	nSecs = math.floor(nSeconds - nHours*3600 - nMins *60);
        
        return nHours,nMins,nSecs
        
    end
end

function checkIfUserUserIsOnCoolown(author)
    if not WasEventCanceled() then
        local Msg_time, time_left = Get_Messagedata(author)
        if Msg_time <= max_delay then
            TriggerClientEvent("chatMessage", author, "^1Please wait ".. time_left.. " seconds before sending another message!")
            CancelEvent()
        else
            current_messages[author] = os.time()
        end
    end
end

function checkIfUserIsMuted(author)
    if not WasEventCanceled() then
        local user = GetPlayerIdentifiers(author)
        for i=1,10 do 
            if user[i] then
                thing = string.find(user[i], "license:")
                if thing then
                    local time_left = Get_UserMute(thing)
                    if time_left ~= 0 then
                        local hour, min, sec = timeCount(time_left)
                        if hour > 0 then
                            TriggerClientEvent("chatMessage", author, "^1You are muted! Try again in "..hour.." hour(s) and "..min.." minute(s) and "..sec.." second(s)")
                            CancelEvent()
                            return true
                        elseif min > 0 then
                            TriggerClientEvent("chatMessage", author, "^1You are muted! Try again in "..min.." minute(s) and "..sec.." second(s)")
                            CancelEvent()
                            return true
                        else
                            TriggerClientEvent("chatMessage", author, "^1You are muted! Try again in "..sec.." seconds(s)")
                            CancelEvent()
                            return true
                        end
                    end
                end
            end
        end
    end
end


RegisterCommand("mute", function(s,a,r)
    local license = ""
    local user = GetPlayerIdentifiers(a[1]) local time = a[2]
    for i=1,10 do 
        if user[i] then
            thing = string.find(user[i], "license:")
            if thing then
                license = thing
            end
        end
    end
    -- change the true to your permission system check thing
    if true then
        if Get_UserMute(license) ~= 0 then
            TriggerClientEvent("chatMessage", s, "^1User is already muted!")
        else
            Set_UserMute(license, time)
        end
    end
end, false)

AddEventHandler('chatMessage', function(author, color, text)
    if checkIfUserIsMuted(author) ~= true then
        checkIfUserUserIsOnCoolown(author)
    end
end)

The mute code download : server.lua (3.4 KB)
For you lazy bastards that doesn’t want to create resources and just want to download and put it in your resources,
chatmute.zip (1.4 KB)

Yes I’m not the greatest coder but it works :slight_smile:

/DeOnyx (aka Z3phyrn because I couldn’t change my name on the forum)

10 Likes

Huh, cool looking script. I love the idea, nice work. I might try it out later, not sure yet. :slight_smile:

Thanks :slight_smile:

1 Like

No problem. I hope to see some more releases from you in the near future!

Idk what with you… but it’s only text… not really works.
image

I wanted to check if it really worked, it turned out to be just a non-helpful text.
When you try to put mute into someone it makes an impression on all the people on the server who are on mute.

I think you haven’t worked on that correctly.
By the way, I can flood as much as I want.

1 Like

Then something is wrong on your side.


because it works 100% perfect when i try it.

(the message sends the “Please wait seconds before sending another message!” client sided)
Basically only you can see the "Please wait seconds before sending another message!”

1 Like
TriggerClientEvent("chatMessage", author, "^1Please wait ".. time_left.. " seconds before sending another message!")

The “author” is the users source.

1 Like

Nice script! Could be awesome if there was a “Vote” system to this so players Can Vote about muting a person

2 Likes

Yeah, that could be useful. I might do it later im currently working on a basic ui but sure

1 Like

nice script i am going to put in my server well done

1 Like

where i put this in the server.lua in chat ?
?

1 Like

You can put the file anywhere in a resource. It doesn’t need to be in the chat resource.

1 Like

I’m having the same problem, everybody gets muted and people can still chat.

1 Like

There seems to be issues when using rolesFX. Im investigating atm

2 Likes

By any chance, any fix on when you try to do /mute ?

You need to start the script as server side.

well,it is in resource.lua

server_script 'server.lua'

Amazing script works great, do you have an idea how can I put the permission system check for vRP framework?

    -- change the true to your permission system check
    if vRP.hasPermission({user_id,"mute.permission"}) then
        if Get_UserMute(license) ~= 0 then
            TriggerClientEvent("chatMessage", s, "^1User is already muted!")
        else
            Set_UserMute(license, time)
        end
    end
end, false)

I did this method but it’s not working with me?
Screenshot:

have you defined user_id?

the thing is, user id needs to be defined as “local user_id = vRP.getUserId({s})”