-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfunctions.lua
More file actions
192 lines (160 loc) · 5.36 KB
/
Copy pathfunctions.lua
File metadata and controls
192 lines (160 loc) · 5.36 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
function NDCore.getPlayerIdentifierByType(src, indentifierType)
if Config.sv_lan then
return ("%s:sv_lan"):format(indentifierType)
end
return GetPlayerIdentifierByType(src, indentifierType)
end
---@param src number
---@return table
function NDCore.getPlayer(src)
return NDCore.players[src]
end
---@param metadata string
---@param data any
---@return table
function NDCore.getPlayers(key, value, returnArray)
if not key or not value then return NDCore.players end
local players = {}
local keyTypes = {
id = "id",
firstname = "firstname",
lastname = "lastname",
gender = "gender",
groups = "groups",
job = "job",
gender = "gender"
}
local findBy = keyTypes[key] or "metadata"
if findBy then
for src, info in pairs(NDCore.players) do
if findBy == "metadata" and info["metadata"][key] == value or info[findBy] == value then
if returnArray then
players[#players+1] = info
else
players[src] = info
end
end
end
end
return players
end
---@param source number
---@return table
function NDCore.getPlayerServerInfo(source)
return PlayersInfo[source]
end
function NDCore.getConfig(info)
if not info then
return Config
end
return Config[info]
end
---@param fileLocation string|tabale
---@return boolean
function NDCore.loadSQL(fileLocation, resource)
local resourceName = resource or GetInvokingResource() or GetCurrentResourceName()
if type(fileLocation) == "string" then
local file = LoadResourceFile(resourceName, fileLocation)
if not file then return end
MySQL.query(file)
return true
end
for i=1, #fileLocation do
local file = LoadResourceFile(resourceName, fileLocation[i])
if file then
MySQL.query(file)
Wait(100)
end
end
return true
end
local DiscordCache = {}
local CacheRefreshInterval = 30 * 60 * 1000
local discordErrors = {
[400] = "Improper HTTP request",
[401] = "Discord bot token might be missing or incorrect",
[404] = "User might not be in the server",
[429] = "Discord bot rate limited"
}
function NDCore.buildDiscordCache()
local done = false
PerformHttpRequest(("https://discordapp.com/api/guilds/%s/members?limit=500"):format(Config.discordGuildId), function(errorCode, resultData, resultHeaders)
if errorCode ~= 200 then
done = true
return print(("^3[Warning]: %d %s"):format(errorCode, discordErrors[errorCode] or "Unknown Error"))
end
local working, members = pcall(function() return json.decode(resultData) end)
if not working or not members then
done = true
return print("^1[Error]: Failed to decode Discord cache response.")
end
DiscordCache = {}
local count = 0
for _, member in ipairs(members) do
local name = member.nick or member.user.username
if name and name:sub(1, 1) == "[" then
DiscordCache[member.user.id] = member
count = count + 1
end
end
print(("^2[DEBUG] Cached %d Discord members successfully."):format(count))
done = true
end, "GET", "", {["Authorization"] = "Bot " .. Config.discordBotToken})
while not done do Wait(50) end
end
function NDCore.getDiscordInfo(discordUserId)
if not discordUserId or not Config.discordBotToken or not Config.discordGuildId then return end
if type(discordUserId) == "string" and discordUserId:find("discord:") then
discordUserId = discordUserId:gsub("discord:", "")
end
if DiscordCache[discordUserId] then
local result = DiscordCache[discordUserId]
return {
nickname = result.nick or result.user.username,
user = result.user,
roles = result.roles
}
end
local done = false
PerformHttpRequest(("https://discordapp.com/api/guilds/%s/members/%s"):format(Config.discordGuildId, discordUserId), function(errorCode, resultData, resultHeaders)
if errorCode ~= 200 then
done = true
return print(("^3[Warning]: %d %s"):format(errorCode, discordErrors[errorCode] or "Unknown Error"))
end
local result = json.decode(resultData)
local data = {
nickname = result.nick or result.user.username,
user = result.user,
roles = result.roles
}
if result.nick and result.nick:sub(1, 1) == "[" then
DiscordCache[discordUserId] = result
end
done = true
end, "GET", "", {
["Content-Type"] = "application/json",
["Authorization"] = "Bot " .. Config.discordBotToken
})
while not done do Wait(50) end
return data
end
CreateThread(function()
while true do
NDCore.buildDiscordCache()
Wait(CacheRefreshInterval)
end
end)
RegisterCommand("reloaddiscordcache", function(src, args, raw)
if src == 0 then
print("^3[NDCore]^7 Reloading Discord cache manually...")
NDCore.buildDiscordCache()
end
end, true)
function NDCore.enableMultiCharacter(enable)
Config.multiCharacter = enable
end
for name, func in pairs(NDCore) do
if type(func) == "function" then
exports(name, func)
end
end