[HOWTO] Send Discord messages with CFX-Server

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 :wink:

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 !

31 Likes

Great tutorials and works perfect! Thank you so much!

Awesome job thank you for sharing this.

Also can’t wait for your gamemode that is in my opinion the best thing I’ve seen in awhile…

Thanks

IDK why, I can’t use it…no message sent in the channel and no error too

You can send me your code in PM if you want I can try to help

Just tried this…

I put the code into my chat/chat_server.lua and i get an error

https://gyazo.com/57591c8256ee67a78c535621bf9cea3f

AddEventHandler('chatMessage', function(source, name, message)
  PerformHttpRequest('https://discordapp.com/api/webhooks/325587327856672770/ST7WRHRZILCPWua7WWtn6L3PC5Blp1yJ-4-XXXXXXXXXXXXXXXXXXXXX', function(err, text, headers) end, 'POST', json.encode({username = name, content = message}), { ['Content-Type'] = 'application/json' })
end

The XXXXXX is to hide my information

Huu yeah I think it’s missing the last parenthesis, I fixed it in the topic :slight_smile:

1 Like

Thank you :slight_smile: 20CHAR

[EDIT] Works :smiley:

https://gyazo.com/3c11b3d2974b876b4184bec21f631c5a

How would i add the player join and player left? :slight_smile:
Sorry hehe

[EDIT] Nevermind was simple, Thank you Eliakoh

Do you think its possible to do it backwards? So i can use the discord to force messages to the server?

[edit] i would also make sure in your description that only use of the server owner should see the chat log, Unless you have a system in your server that makes it so they can accept there privacy being seen by other admins etc, I hope this makes sense

Yes that should be possible using the Discord API, but you’ll have to check the doc and do that by yourself :slight_smile:

I think I see what you mean, but as the server is public too, I’m not sure it’s a big deal. I mean you’re not supposed to tell private stuff on public servers anyway

1 Like

This is nice, but i dont want it to display commands, like /ban /kick, all commands really

Works like a charm on an Fx-Server ! Would it work on a Citizen.MP Server ?

I can’t get this to work, no matter what I do. Plus, the tutorial is a bit vague as to where to put the webhook text.

I posted it in both the game chat and in the console, neither worked :confused:

Hey sorry for the late answer but the script was initially made for the Citizen version, I’m glad to hear it works on the new version too.

I am not sure what you mean but the code must be placed in the server script.

works great! thx

can you put too each 15 min how much gamers they are on the server ?

Great thank you
how to send message from Discord to chat server ??

yes, how to send msgs from discord to server

You should be able to do that by triggering an event from a client with a timer, but you’ll have to do that by yourself.

Check this thread :

and specially that answer from Havoc :

by the way your playeractivated is not working for me i had to do it this way and it worked.

AddEventHandler('playerConnecting', function()
  sendToDiscord('system', GetPlayerName(source) .. ' joined.')
end)
1 Like

which URL?
should i use