https://mindescape.eu/img/brv_banner.png
Hello everyone,
For my Battle Royale server, I wanted to see the in-game chat without being in the game.
I thought about Discord and webhooks, so I checked how it was supposed to work, and it was very easy !
I will release the full code of my gamemode, but some people asked me about how to do that, so here we go : how to send messages from your FiveM server to your Discord server ?
Obviously, you need to own a Discord server for this to work
First, go to your server settings :
(yeah, baguette and dark theme)
Then, click on Webhooks and Create Webhook. :
You can see that I have already a webhook on this screen.
Give it a default name (that will be used if you don’t specify a name in your code), select the channel where the messages will be send, an icon if you want, and copy the webhook URL
Click on Save
You just created a Discord webhook !
If you want to send a message everytime a player is sending a chat message (and if you’re using the default chat resource), you can use this code in your server script :
AddEventHandler('chatMessage', function(source, name, message)
PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, content = message}), { ['Content-Type'] = 'application/json' })
end)
Replace DISCORD_URL by the webhook URL you got earlier.
Now every chat message will be send to your Discord server, in the channel you specified
Congratulations, it works !
You can remove the username = name,
and the message will be posted with the default webhook name you picked.
There are other options, like overriding the avatar or add a file, I’ll let you check the official doc : https://discordapp.com/developers/docs/resources/webhook#execute-webhook
You may see errors in your server console :
2017-06-14 20:28:39.4119|WARN|CitizenMP.Server|Web request to https://discordapp.com/api/webhooks/XXXXXXXXXXXXX failed: The remote server returned an error: (400) BAD REQUEST.
This is probably someone sending an empty chat message. Discord disallow empty messages, so it returns an error but we will fix that.
If you want to send other type of messages, you can put the PerformHttpRequest
inside a function (notice the if
, now we won’t send nil or empty messages to Discord anymore) :
-- This function could be a lot better, by adding a test on the name var, using other Discord options, etc.
function sendToDiscord(name, message)
if message == nil or message == '' then return FALSE end
PerformHttpRequest('DISCORD_URL', function(err, text, headers) end, 'POST', json.encode({username = name, content = message}), { ['Content-Type'] = 'application/json' })
end
And then call the function inside the right events, for example :
AddEventHandler('playerActivated', function()
sendToDiscord('SYSTEM', GetPlayerName(source) .. ' joined.')
end)
AddEventHandler('playerDropped', function(reason)
sendToDiscord('SYSTEM', GetPlayerName(source) .. ' left (' .. reason .. ')')
end)
(again, server script !)
Now you’ll also get the players joining and leaving in your Discord channel.
Neat !
I hope you enjoyed this little HowTo, thanks for reading and happy coding !