I would like to create an admin command to turn off the chat. Can this be done? What will it take?
It will take that you either look around in the forums or make one your own.
If you don’t have anything to say. You shouldn’t say anything at all.
In F8 Console type “stop chat”
you will also have to set admins in your server.cfg
I highly don’t recommend stopping the chat to “turn it off”. as this will stop the chat from functioning for everyone as well as resources depending on commands will no longer work.
EDIT 2:
the “solution” below is broken, I’ve created a new PR on the github repository to improve the changes.
If that gets accepted, you’ll need to create a resource that cancels the chat:cancelChatMessage
event. I’ll add an example here if the PR gets accepted.
old broken 'solution'
EDIT:
This “solution” is not perfect at all but it’ll allow resources to disable chat per client.
Change your chat’s cl_chat.lua
file content for this:
local chatInputActive = false
local chatInputActivating = false
RegisterNetEvent('chatMessage')
RegisterNetEvent('chat:addTemplate')
RegisterNetEvent('chat:addMessage')
RegisterNetEvent('chat:addSuggestion')
RegisterNetEvent('chat:removeSuggestion')
RegisterNetEvent('chat:clear')
-- internal events
RegisterNetEvent('__cfx_internal:serverPrint')
RegisterNetEvent('_chat:messageEntered')
--deprecated, use chat:addMessage
AddEventHandler('chatMessage', function(author, color, text)
if not WasEventCanceled() then
local args = { text }
if author ~= "" then
table.insert(args, 1, author)
end
SendNUIMessage({
type = 'ON_MESSAGE',
message = {
color = color,
multiline = true,
args = args
}
})
end
end)
AddEventHandler('__cfx_internal:serverPrint', function(msg)
print(msg)
SendNUIMessage({
type = 'ON_MESSAGE',
message = {
color = { 0, 0, 0 },
multiline = true,
args = { msg }
}
})
end)
AddEventHandler('chat:addMessage', function(message)
SendNUIMessage({
type = 'ON_MESSAGE',
message = message
})
end)
AddEventHandler('chat:addSuggestion', function(name, help, params)
SendNUIMessage({
type = 'ON_SUGGESTION_ADD',
suggestion = {
name = name,
help = help,
params = params or nil
}
})
end)
AddEventHandler('chat:removeSuggestion', function(name)
SendNUIMessage({
type = 'ON_SUGGESTION_REMOVE',
name = name
})
end)
AddEventHandler('chat:addTemplate', function(id, html)
SendNUIMessage({
type = 'ON_TEMPLATE_ADD',
template = {
id = id,
html = html
}
})
end)
AddEventHandler('chat:clear', function(name)
SendNUIMessage({
type = 'ON_CLEAR'
})
end)
RegisterNUICallback('chatResult', function(data, cb)
chatInputActive = false
SetNuiFocus(false)
if not data.canceled then
local id = PlayerId()
--deprecated
local r, g, b = 0, 0x99, 255
if data.message:sub(1, 1) == '/' then
ExecuteCommand(data.message:sub(2))
else
TriggerServerEvent('_chat:messageEntered', GetPlayerName(id), { r, g, b }, data.message)
end
end
cb('ok')
end)
RegisterNUICallback('loaded', function(data, cb)
TriggerServerEvent('chat:init');
cb('ok')
end)
Citizen.CreateThread(function()
SetTextChatEnabled(false)
SetNuiFocus(false)
while true do
Wait(0)
if not chatInputActive then
if IsControlPressed(0, 245) --[[ INPUT_MP_TEXT_CHAT_ALL ]] then
chatInputActive = true
chatInputActivating = true
SendNUIMessage({
type = 'ON_OPEN'
})
end
end
if chatInputActivating then
if not IsControlPressed(0, 245) then
SetNuiFocus(true)
chatInputActivating = false
end
end
end
end)
Then make a client script that does something like the following (just create a new resource for it):
local chatEnabled = true
AddEventHandler('chatMessage', function()
if not chatEnabled then
CancelEvent()
end
end)
RegisterCommand('chat', function()
chatEnabled = not chatEnabled
if not chatEnabled then
TriggerEvent('chatMessage', '', {255,255,255}, 'Chat has been ^1disabled^0.')
else
TriggerEvent('chatMessage', '', {255,255,255}, 'Chat has been ^2enabled^0.')
end
end, false)
Hmm, seems to tell me that the chat is disabled but I still see chat.
You can see chat, but you shouldn’t receive any more messages…
Well, when I do the /chat it tells me its disabled but I can still see when people type to me and in OOC. I have actually fixed my issue with this -
https://github.com/TheStonedTurtle/cfx-server-data/blob/54404fbab5f3738183c0b265525fca6727d1865c/resources/[system]/chat/cl_chat.lua
And if I am not wrong, this means this will be added to the base FiveM in the near future.
That PR request is so old I doubt they’ll ever approve it lol.
Works for my server.
perhaps because it’s an undesired implementation, having an API/(external?) keybind for it rather than a command would’ve gotten approved instantly
that, and 5 commits for a single feature including some commits reverting the exact same commits previously in the branch? wow
Why exactly is binding this to a key better than using a chat command? Couldn’t people unintentionally toggle it without knowing what caused it?
Also, why wasn’t i told this on the PR topic months ago so I could make the necessary changes? I’m no mind reader.
I figured updating the same branch would be preferable to creating a second Pull Request for the same feature, especially since i updated it per one of the contributors suggestion.
I’m no Github expert and i didn’t see anything wrong with reverting the changes to the default chat source and then making the changes to accommodate his request. I also don’t see how the amount of commits it took me to get to the end result is even relevant when reviewing a pull request. I don’t know anyone who reviews each commit in a Pull Request, they review the combined differences from all commits.
Either way I’ve closed the Pull Request and Deleted my forked copy since as you mentioned its a undesired implementation
, maybe someone else will recreate it “correctly”
I’ve recreated my “allow resources to disable chat per client” PR with the requested changes. If that gets reviewed/accepted you’ll need to create a client script that manages the disabling of the chat. I’ll add an example if this PR gets accepted.
He’s clearing being a complete asshole about it, if it was an
undesired implementation
then why would people be asking for it? Use your brain before you bash others work.