-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
149 lines (111 loc) · 4.1 KB
/
main.lua
File metadata and controls
149 lines (111 loc) · 4.1 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local ESX = exports["es_extended"]:getSharedObject()
lib.versionCheck("spectrumdevofficial/sd_playergroups")
local groups = Config.Roles.Groups
local groupPriorities = Config.Roles.Priorities
local function findGroup(roles)
local foundGroup = false
for i = 1, #roles do
local role = tostring(roles[i])
local roleGroup = groups[role]
if roleGroup then
local curPrio = foundGroup and groupPriorities[foundGroup] or 0
local newPrio = groupPriorities[roleGroup] or 1
if curPrio < newPrio then
foundGroup = roleGroup
end
end
end
return foundGroup
end
local function checkRoles(xPlayer)
local discord = getDiscordId(xPlayer.source)
if not discord then
local message = ("^7[^6GROUPS^7] Player ^3[%s] %s^7 discord id ^1not found^7 (discord ^2'%s'^7)."):format(xPlayer.source, GetPlayerName(xPlayer.source), discord)
print(message)
return false
end
local roles = getUserRoles(discord)
if not roles then
local message = ("^7[^6GROUPS^7] Player ^3[%s] %s^7 discord roles ^1not found^7 (discord ^2'%s'^7)."):format(xPlayer.source, GetPlayerName(xPlayer.source), discord)
print(message)
return false
end
local foundGroup = findGroup(roles)
return foundGroup ~= false, foundGroup
end
local function updateGroup(xPlayer, hasRole, group)
local currentGroup = xPlayer.getGroup()
local updatedGroup = false
if hasRole and currentGroup ~= group then
updatedGroup = group
elseif not hasRole and currentGroup ~= "user" then
updatedGroup = "user"
end
if updatedGroup ~= false then
xPlayer.setGroup(updatedGroup)
local message = ("^7[^6GROUPS^7] Group updated for ^3[%s] %s^7 to group ^2%s^7 (from ^2%s^7)."):format(xPlayer.source, GetPlayerName(xPlayer.source), updatedGroup, currentGroup)
print(message)
end
end
AddEventHandler("esx:playerLoaded", function(source, xPlayer, isNew)
if not Config.Enabled then return end
local source = source
local hasRole, group = checkRoles(xPlayer)
updateGroup(xPlayer, hasRole, group)
end)
local isReloading = false
RegisterCommand(Config.ReloadCommand, function(source, args, raw)
if not Config.Enabled then return end
if source ~= 0 then
local message = ("^7[^6GROUPS^7] Command ^3/%s^7 is ^1only available^7 for ^2Console^7."):format(Config.ReloadCommand)
return print(message)
elseif isReloading then
local message = ("^7[^6GROUPS^7] Command ^3/%s^7 is already ^2reloading^7."):format(Config.ReloadCommand)
return print(message)
end
isReloading = true
local xPlayers = ESX.GetExtendedPlayers()
local message = ("^7[^6GROUPS^7] Reloading groups of ^2%s^7 players."):format(#xPlayers)
print(message)
for _, xPlayer in pairs(xPlayers) do
local hasRole, group = checkRoles(xPlayer)
updateGroup(xPlayer, hasRole, group)
end
local message = ("^7[^6GROUPS^7] Reloaded groups of ^2%s^7 players."):format(#xPlayers)
print(message)
isReloading = false
end, true)
local function discordRequest(url)
local result = nil
PerformHttpRequest("https://discord.com/api/"..url, function(code, data, headers)
result = {
code = code,
data = data,
headers = headers
}
end, "GET", "", {
["Content-Type"] = "application/json",
["Authorization"] = "Bot "..Config.Token
})
while result == nil do
Wait(100)
end
return result
end
function getDiscordId(source)
local identifiers = GetPlayerIdentifiers(source)
for _, identifier in pairs(identifiers) do
if string.find(identifier, "discord") then
return string.gsub(identifier, "discord:", "")
end
end
return nil
end
function getUserRoles(discord)
if not discord then return end
local url = "guilds/"..Config.GuildID.."/members/"..discord
local result = discordRequest(url)
if result.code ~= 200 then return end
local data = json.decode(result.data)
return data.roles
end