Jump to content

[Question] How to fake player chats and ignore color codes in outputChatBox


kieran

Recommended Posts

I have made a simple script to stop people advertising servers, it also converts all caps chat to lower case chat...  There is of course many problems with this.

Firstly, I need to use outputChatBox and fake the players chats when I convert upper case to lower case, so I need to get their name and output it with the text...  This works okay, but I've noticed the original chat script is a little more complex, it gets the players nametag color and name, and outputs them...  

Is there a native event to output chat like the player sent it such as: "triggerServerEvent('outputChatBox', localPlayer, some-text)" or do I need to do it the way in the below script?  (see line 25)

Also when the player uses lower case hexadecimal codes, it thinks that it is part of the text and not a color code, is their a way of ignoring the hex code?  So could I remove the string but also check it's a hex code and not something stupid like '#lol'? 

My script is below and it is server side for now, but I think it would work better client side (if their is a custom event for outputting chat)

strings = {
	--try entering "mtasa" or "server" in game and see what happens!
	{'mtasa://', 'DO NOT ADVERTISE IP!'}, --string_to_find[1], message_to_show_player[2]
	{'SERVER', 'DO NOT ADVERTISE SERVERS!'}, 
}										

function checkText( message, messageType )
    if messageType == 0 then
		for i=1,#strings do
			string_to_find = strings[i][1]
			message_to_show_player = strings[i][2]
			findstring = string.find(tostring(string.upper(message)),tostring(string.upper(string_to_find)))
			if findstring then
				cancelEvent()
				outputChatBox(tostring(message_to_show_player), source, 255, 0, 0)
				return false --stops here if player is advertising, otherwise it will check for caps in chat.
			end
		end
	--Below is anti-caps script
	capsfree = string.match(tostring(message), '%l')
	local name = getPlayerName ( source )
	
	if not capsfree then 
		cancelEvent()
		outputChatBox(tostring(name)..': #E7D9B0'..string.lower(message), _,231,217,176,true) --HERE is where I want to make it as if the player sent the chat
	end --anti-caps ends here
	end
end

addEventHandler("onPlayerChat", root, checkText)

Thanks for any help in advance. :)

Link to comment
local BlockedWords = {
    "mtasa",
    "second",
    "third",
    "addmore",
}

addEventHandler("onPlayerChat",getRootElement(), function(message, type)
    cancelEvent()
    if (type == 1) or (type == 2) then
        cancelEvent()
    end
    if (type == 0) then
        local new = ""
        local counter = 0
        for word in message:gmatch("%S+") do
            counter = counter + 1
            for i, swr in ipairs(BlockedWords) do
                local src = word:lower():gsub("%s", "")
                local src = src:gsub("#%x%x%x%x%x%x", "")
                local src = src:gsub("%c", "")
                local src = src:gsub("%p", "")
                local pat = swr:lower():gsub("%s", "")
                if src:find(pat) then
                    local replaceString = ""
                    for x=1,word:gsub("#%x%x%x%x%x%x",""):len() do
                        replaceString = replaceString.."*"
                    end
                    word = word:gsub(word, replaceString)
                end
            end
            if counter == 1 and word:len() > 2 then
                word = word:gsub("%a", string.upper, 0)
            end
            new = new..word.." "
        end
        if new ~= "" then message = new end
        outputChatBox(getPlayerName(source)..":#FFFFFF "..message, root, 255, 255, 255, true)
    end
end)

 

  • Thanks 1
Link to comment

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...