Chatbox Console Manipulation

Hi there,

I am attempting to build a chatbox and I would like to modify what the server console says when a chat goes through (for administration), however I’m not sure which function to use. Right now, I am overriding GM:PlayerSay, but the message I get in console still consists of the player’s nick and the message they sent, even though what I’m returning in PlayerSay is different. Thanks for your help!

Could you elaborate by posting your code?

Not home, so I’ll post something similar.



function GM:PlayerSay(ply, text, team)
     return “[CUSTOM] “ .. ply:Nick() .. “: “ .. text
end


You would expect that when somebody chats now, in the console it should say “[CUSTOM] playername: whatever” but it still does “playername: message”

You want to use this instead:

GM:OnPlayerChat

This is what the hook looks like in the default base gamemode:

[lua]
–[[---------------------------------------------------------
Name: gamemode:OnPlayerChat()
Process the player’s chat… return true for no default
-----------------------------------------------------------]]
function GM:OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )

--
-- I've made this all look more complicated than it is. Here's the easy version
--
-- chat.AddText( player, Color( 255, 255, 255 ), ": ", strText )
--

local tab = {}

if ( bPlayerIsDead ) then
	table.insert( tab, Color( 255, 30, 40 ) )
	table.insert( tab, "*DEAD* " )
end

if ( bTeamOnly ) then
	table.insert( tab, Color( 30, 160, 40 ) )
	table.insert( tab, "(TEAM) " )
end

if ( IsValid( player ) ) then
	table.insert( tab, player )
else
	table.insert( tab, "Console" )
end

table.insert( tab, Color( 255, 255, 255 ) )
table.insert( tab, ": " .. strText )

chat.AddText( unpack(tab) )

return true

end
[/lua]

You can see that the OnPlayerChat hook is what determines the transition between what the player types, and how it is displayed on screen. PlayerSay is only for changing the message itself, not how it is displayed.

Thanks for the quick reply, James. This works quite well for the clientside, however this does not fix my serverside issue. The server console still displays the text in the same fashion: “playername: message”.

I have even overridden the hook for PlayerSay and returned “” (which worked), but then the client does not receive the intented chat message.

If you use GM:Player say, then you will need to use some networking to make it work clientside as it is serverside, whilst OnPlayerChat is clientside only. So in your way I think it’s best to use GM:PlayerSay and then use some networking.

I’ve thought of this, however the default chat system is already networked. Whatever you return through PlayerSay comes as the “text” parameter in OnPlayerChat. My last resort is to just override all of the default chat networking and do it myself.

Could you perhaps put the code in a pastebin?

I understand. The chat messages printing in the console are handled by Source IIRC, so you’ll either have to put up with it or work around it.

One posibility is to return an empty string in

GM:PlayerSay, after having broadcast the the player entity, the text string and the team bool using the Net library, and printed the message in the console as you see fit. You could then receive that net message, and call the

GM:OnPlayerChat hook manually using

hook.Run, where the 4th argument is determined by

Player:Alive after the “not” opperator