-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
70 lines (61 loc) · 2.04 KB
/
server.lua
File metadata and controls
70 lines (61 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
ESX = exports["es_extended"]:getSharedObject()
local DISCORD_BOT_TOKEN = "YOUR TOKEN"
local GUILD_ID = "SERVERID"
local ROLE_IDS = {
{ id = "ROLEID", group = "GROUPNAME" }
}
function hasDiscordRole(discordId, cb)
PerformHttpRequest("https://discord.com/api/v9/guilds/" .. GUILD_ID .. "/members/" .. discordId, function(statusCode, response, headers)
if statusCode == 200 then
local data = json.decode(response)
if data and data.roles then
local highestGroup = nil
for _, role in ipairs(ROLE_IDS) do
for _, userRole in ipairs(data.roles) do
if userRole == role.id then
highestGroup = role.group
break
end
end
if highestGroup then
break
end
end
if highestGroup then
cb(true, highestGroup)
else
cb(false, nil)
end
else
cb(false, nil)
end
else
print("Error fetching Discord user: " .. statusCode)
cb(false, nil)
end
end, "GET", "", { ["Authorization"] = "Bot " .. DISCORD_BOT_TOKEN })
end
AddEventHandler("esx:playerLoaded", function(playerId)
local xPlayer = ESX.GetPlayerFromId(playerId)
local discordId = nil
for _, identifier in ipairs(GetPlayerIdentifiers(playerId)) do
if string.sub(identifier, 1, 8) == "discord:" then
discordId = string.sub(identifier, 9)
break
end
end
if discordId then
hasDiscordRole(discordId, function(hasRole)
if hasRole then
xPlayer.setGroup("admin")
end
end)
end
end)
AddEventHandler("playerDropped", function()
local playerId = source
local xPlayer = ESX.GetPlayerFromId(playerId)
if xPlayer then
xPlayer.setGroup("user")
end
end)