Hello!
I am trying to develop a script that if the player types the command “help” on the FiveM console, a message will be sent on the Discord server. Do I need to do this through a Discord bot? Or do I need to do some configuration/code between the Discord and FiveM servers?
I may not have explained myself correctly, but it was the simplest way I found to explain what I want to do.
I hope you can help me.
Thanks in advance!
What you are looking for is a simple webhook.
Check if this bot works out ^^
DevNaka
February 28, 2022, 9:05am
3
Hey there, here’s some code I wrote down and tested and gave me the result I think you’re looking for.
Preview:
You can place this code in any server file and it should work fine:
local WebHook = 'INSERT_DISCORD_WEBHOOK'
RegisterCommand('help', function(source, args, msg)
local xPlayer = ESX.GetPlayerFromId(source)
local identifier = GetPlayerIdentifiers(source)[1]
local message = msg
sendToDiscord(16753920, "Help Me", 'Player in need of help: '..message, "Identifier: "..identifier)
end)
function sendToDiscord(color, name, message, footer)
local embed = {
{
['color'] = color,
['title'] = '**'..name..'**',
['description'] = message,
['footer'] = {
['text'] = footer,
},
}
}
PerformHttpRequest(WebHook, function(err, text, headers) end, 'POST', json.encode({username = name, embeds = embed}), { ['Content-Type'] = 'application/json' })
end
Just create a webhook in discord and replace the WebHook
variable with your new discord webhook.
Hope this helps.
Yes, that’s exactly what I want. I tried, but it gave me an error
I copy-pasted your code and changed the WebHook variable as you said.
ERROR
SCRIPT ERROR @DiscordCommands /commands/help.lua:12: attempt to call a nil value (filed ‘GetPlayerFromId’)
I made a script just for these types of commands. Here is my fxmanifest.lua file:
fx_version 'cerulean'
game 'gta5'
description 'Discord Commands'
version 'beta'
client_scripts {
'commands/*.lua'
}
What am I missing?
DevNaka
February 28, 2022, 7:08pm
5
You’re placing this in a client file. This has to be inside of a server file.
move your help.lua file and change your fxmanifest:
fx_version 'cerulean'
game 'gta5'
description 'Discord Commands'
version 'beta'
client_scripts {
'commands/*.lua'
}
server_script 'help.lua'
Yup, that’s it! It has to be a server-side file. But I have some changes to make to your code.
The RegisterCommand for server-side requires a boolean:
REGISTER_COMMAND(char* commandName, func handler, BOOL restricted);
It needs the boolean at the end to work properly. At least according to the native documentation!
Thank you!
And thanks @ItzCelyrian for trying to help me with that bot! I haven’t tried it, but I’m sure it works too, but what I wanted was more of an option like the one @DevNaka gave. But anyway, thanks to both of you!