Skip to content

Commit

Permalink
Version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Clink1234 committed May 19, 2022
1 parent 7653a66 commit 930c196
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 66 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

## Version 0.2.0

- Added automatic job removal if a user no longer has the required roles
- Added exports to be used in other scripts
- Added config option to disable the chat commands (only exports can then be used)

## Version 0.1.1

- Initial Release
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ESX Discord Jobs - Created By Clink123
[![Discord](https://img.shields.io/discord/710320434872320010.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2)](https://discord.gg/gsMTu3q)

FiveM ESX script that allows players to take jobs, using Discord roles to whitelist these jobs.
FiveM ESX script that allows players to take jobs, using Discord roles to whitelist these jobs. Also can take away jobs (and set the player back to a default job) if they lose the required Discord roles.

## Prerequisites

Expand All @@ -15,6 +15,27 @@ This resource was built and designed for [ESX Legacy](https://github.com/esx-fra
- Edit `config.lua` with your desired jobs and roles.
- Add `start esx_discordjobs` to your server.cfg.

## Use In Other Resources

Includes two exports that can be used in other resources as shown below...

```lua
-- Check Job Permissions - returns true if they have permission to have the job, false otherwise
exports['esx_discordjobs']:checkJobPermissions(playerIdentifier, esxJobName, esxJobGrade, function(result)
print(result)
end)
```

```lua
-- Set Job - sets the player job checking that they have permission to do so
-- Returns true if they had permission for and now have the job, false otherwise
exports['esx_discordjobs']:setJob(playerIdentifier, esxJobName, esxJobGrade, function(result)
print(result)
end)
```

Note: Remember that the jobs and grades must be defined in `Config.jobRoles` for these to work.

## License

This resource/script/modification is provided free of charge. No warranty is provided in any form. Any responsibility for damages caused by this resource rest solely with the user. The author(s) of this script accept no liability. By using this resource, you agree to the following terms...
Expand Down
18 changes: 10 additions & 8 deletions client.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Citizen.CreateThread(function()
TriggerEvent('chat:addSuggestion', "/"..Config.command, 'Take a job.', {{name = 'Job Name', help = 'The name of the job you want to take.'}})
end)
if Config.enableCommands then
Citizen.CreateThread(function()
TriggerEvent('chat:addSuggestion', "/"..Config.command, 'Take a job.', {{name = 'Job Name', help = 'The name of the job you want to take.'}})
end)

AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
TriggerEvent('chat:removeSuggestion', "/"..Config.command)
end
end)
AddEventHandler('onResourceStop', function(resource)
if resource == GetCurrentResourceName() then
TriggerEvent('chat:removeSuggestion', "/"..Config.command)
end
end)
end
22 changes: 19 additions & 3 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@

Config = {}

-- Players will be able to select this job without any roles (as long as it has a command defined)
-- To do so, be sure to set it in the jobRoles config without any required roles!
Config.defaultJob = 'unemployed'
Config.defaultGrade = 0

-- Enable the chat commands (otherwise only the exports can be used in other resources)
Config.enableCommands = true

-- Job Name with associated list of discord IDs
-- {'Command Name', 'ESX Job Name', {'discordID', 'discordID'}, requireAll, JobGrade}
-- requireAll means that the user must have all the roles in the list. Otherwise they only need one of the roles in the list.
-- Note: You must list all of your jobs here for players to select them.
Config.jobRoles = {
{'Unemployed', 'unemployed', {'736414176863453206'}, false, 0},
{'Police', 'police', {'736824407598694432', '743005394187714671'}, false, 0}
{'Unemployed', 'unemployed', {}, false, 0},
{'Police', 'police', {'743005894517850144', '743005811730808832'}, false, 0}
}

-- RGB Colors of the confirmation and error messages
Config.messageColor = {255, 171, 25}
Config.errorColor = {255, 51, 51}

-- The command a player uses to take a job
Config.command = 'takejob'
Config.command = 'takejob'

-- Check periodically to see if players have the appropriate permissions for their job
-- Checks every Config.checkTime milliseconds if the player has the correct roles for their job
-- Config.timeBetweenPlayers is the amount of time between each player being checked (prevents discord api rate limits)
-- If they no longer have the required roles, it sets their job to Config.defaultJob (esx job name) with Config.defaultGrade (int)
Config.enableRoleChecks = true
Config.checkTime = 60000
Config.timeBetweenPlayers = 50
4 changes: 1 addition & 3 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ game 'gta5'

author 'Clink123'

version '0.1.1'
version '0.2.0'

description 'ESX Discord Jobs'

version 'legacy'

shared_script '@es_extended/imports.lua'

server_scripts {
Expand Down
206 changes: 155 additions & 51 deletions server.lua
Original file line number Diff line number Diff line change
@@ -1,65 +1,169 @@
RegisterCommand(Config.command, function(source, args, rawCommand)
if source and source ~= 0 then
local requestedJob = args[1]
local xPlayer = ESX.GetPlayerFromId(source)
local requiredRoleList = false
local requireAll = false
local requestedESXJob = false
local jobGrade = false

for index, jobRoleEntry in ipairs(Config.jobRoles) do
if jobRoleEntry[1] == requestedJob then
requestedESXJob = jobRoleEntry[2]
requiredRoleList = jobRoleEntry[3]
requireAll = jobRoleEntry[4]
jobGrade = jobRoleEntry[5]
if Config.enableCommands then
RegisterCommand(Config.command, function(source, args, rawCommand)
if source and source ~= 0 then
local requestedJob = args[1]
local xPlayer = ESX.GetPlayerFromId(source)
local requiredRoleList = false
local requireAll = false
local requestedESXJob = false
local jobGrade = false

for index, jobRoleEntry in ipairs(Config.jobRoles) do
if jobRoleEntry[1] == requestedJob then
requestedESXJob = jobRoleEntry[2]
requiredRoleList = jobRoleEntry[3]
requireAll = jobRoleEntry[4]
jobGrade = jobRoleEntry[5]
end
end
end

local jobExists = ESX.DoesJobExist(requestedESXJob, jobGrade)

if jobExists then
exports.discordroles:getUserRoles(source, function(userRoles)
if xPlayer and userRoles then
if requireAll then
local restrict = false
for index, requiredRole in ipairs(requiredRoleList) do
local foundRole = false
for index, userRole in ipairs(userRoles) do
if userRole == requiredRole then
foundRole = true
end
end
restrict = not foundRole
end
if not restrict then

local jobExists = ESX.DoesJobExist(requestedESXJob, jobGrade)

if jobExists then
exports.discordroles:getUserRoles(source, function(userRoles)
if xPlayer and userRoles then
local hasPermissions = checkPermissions(requiredRoleList, requireAll, userRoles)
if hasPermissions then
xPlayer.setJob(requestedESXJob, jobGrade)
TriggerClientEvent('chat:addMessage', source, {args = {"Server","Your job has been set to " .. requestedESXJob .. " with grade " .. jobGrade .. "."}, color = Config.messageColor })
else
TriggerClientEvent('chat:addMessage', source, {args = {"Server","You do not have permission to take that job."}, color = Config.errorColor })
end
else
local restrict = true
for index, requiredRole in ipairs(requiredRoleList) do
for index, userRole in ipairs(userRoles) do
if userRole == requiredRole then
restrict = false
TriggerClientEvent('chat:addMessage', source, {args = {"Server","Could not get your Discord roles. Try restarting Fivem."}, color = Config.errorColor })
end
end)
else
TriggerClientEvent('chat:addMessage', source, {args = {"Server","The requested job does not exist."}, color = Config.errorColor })
end
end
end, false)
end

Citizen.CreateThread(function()
if Config.enableRoleChecks then
local requiredRoleList = false
local requireAll = false
local checkESXJob = false
local checkJobGrade = false
local playerJob = false
while true do
Citizen.Wait(Config.checkTime)
local xPlayers = ESX.GetExtendedPlayers()
for _, xPlayer in pairs(xPlayers) do
Citizen.Wait(Config.timeBetweenPlayers)
exports.discordroles:getUserRoles(xPlayer.source, function(userRoles)
for index, jobRoleEntry in ipairs(Config.jobRoles) do
checkESXJob = jobRoleEntry[2]
checkJobGrade = jobRoleEntry[5]
requiredRoleList = jobRoleEntry[3]
requireAll = jobRoleEntry[4]
playerJob = xPlayer.getJob()
if playerJob['grade'] == checkJobGrade and playerJob['name'] == checkESXJob then
if xPlayer and userRoles then
local hasPermissions = checkPermissions(requiredRoleList, requireAll, userRoles)
if not hasPermissions then
xPlayer.setJob(Config.defaultJob, Config.defaultGrade)
print(xPlayer.getName().." did not have permissions for the job " .. checkESXJob .. " and was set to the default job of " .. Config.defaultJob .. " with grade " .. Config.defaultGrade)
end
else
print("Count not check a player's discord permissions. Is discordroles running?")
end
end
if not restrict then
xPlayer.setJob(requestedESXJob, jobGrade)
TriggerClientEvent('chat:addMessage', source, {args = {"Server","Your job has been set to " .. requestedESXJob .. " with grade " .. jobGrade .. "."}, color = Config.messageColor })
else
TriggerClientEvent('chat:addMessage', source, {args = {"Server","You do not have permission to take that job."}, color = Config.errorColor })
end
end
else
TriggerClientEvent('chat:addMessage', source, {args = {"Server","Could not get your Discord roles. Try restarting Fivem."}, color = Config.errorColor })
end)
end
end
end
end)

function checkPermissions(requiredRoleList, requireAll, userRoles)
if requireAll then
local restrict = false
for index, requiredRole in ipairs(requiredRoleList) do
local foundRole = false
for index, userRole in ipairs(userRoles) do
if userRole == requiredRole then
foundRole = true
end
end
restrict = not foundRole
end
if not restrict then
return true
else
return false
end
else
local restrict = true
if next(requiredRoleList) == nil then
restrict = false
end
for index, requiredRole in ipairs(requiredRoleList) do
for index, userRole in ipairs(userRoles) do
if userRole == requiredRole then
restrict = false
end
end)
end
end
if not restrict then
return true
else
TriggerClientEvent('chat:addMessage', source, {args = {"Server","The requested job does not exist."}, color = Config.errorColor })
return false
end
end
end, false)
end
end

exports('checkJobPermissions', function(identifier, jobName, grade, cb)
local jobExists = ESX.DoesJobExist(jobName, grade)
local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
if xPlayer and jobExists then
local requiredRoleList = false
local requireAll = false

for index, jobRoleEntry in ipairs(Config.jobRoles) do
if jobRoleEntry[2] == jobName and jobRoleEntry[5] == grade then
requiredRoleList = jobRoleEntry[3]
requireAll = jobRoleEntry[4]
end
end

exports.discordroles:getUserRoles(xPlayer.source, function(userRoles)
if xPlayer and userRoles then
cb(checkPermissions(requiredRoleList, requireAll, userRoles))
end
end)
else
cb(false)
end
end)

exports('setJob', function(identifier, jobName, grade, cb)
local jobExists = ESX.DoesJobExist(jobName, grade)
local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
if xPlayer and jobExists then
local requiredRoleList = false
local requireAll = false

for index, jobRoleEntry in ipairs(Config.jobRoles) do
if jobRoleEntry[2] == jobName and jobRoleEntry[5] == grade then
requiredRoleList = jobRoleEntry[3]
requireAll = jobRoleEntry[4]
end
end

exports.discordroles:getUserRoles(xPlayer.source, function(userRoles)
if xPlayer and userRoles then
local hasPermissions = checkPermissions(requiredRoleList, requireAll, userRoles)
if checkPermissions(requiredRoleList, requireAll, userRoles) then
xPlayer.setJob(jobName, grade)
cb(true)
else
cb(false)
end
end
end)
else
cb(false)
end
end)

0 comments on commit 930c196

Please sign in to comment.