-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsv_discord_api.lua
87 lines (67 loc) · 2.43 KB
/
sv_discord_api.lua
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
local token = "Bot " .. Config.DiscordToken
local pairs = pairs
local tostring = tostring
local encode = json.encode
local decode = json.decode
local _GetIdentifiersTable = GetIdentifiersTable
local _PerformHttpRequest = PerformHttpRequest
local _Wait = Citizen.Wait
local GuildID = Config.GuildID
local DiscordRoles = Config.DiscordRoles
local function DiscordRequest(endpoint, method, jsondata)
local data = nil
_PerformHttpRequest("https://discord.com/api/" .. endpoint, function(codeResponse, dataResponse, headersResponse)
data = {
code = codeResponse,
data = dataResponse,
headers = headersResponse
}
end, method, ((#jsondata > 0 and encode(jsondata)) or ""),
{
["Content-Type"] = "application/json",
["Authorization"] = token
})
while data == nil do
_Wait(0)
end
return data
end
function isInGuild(user)
local identifiers = _GetIdentifiersTable(user)
if not identifiers.discord then return false end
local endpoint = ("guilds/%s/members/%s"):format(GuildID, identifiers.discord)
local result = DiscordRequest(endpoint, "GET", {})
return result.code == 200
end
function getRoles(user)
local identifiers = _GetIdentifiersTable(user)
if not identifiers.discord then return {} end
local endpoint = ("guilds/%s/members/%s"):format(GuildID, identifiers.discord)
local result = DiscordRequest(endpoint, "GET", {})
if result.code ~= 200 then return {} end
local data = decode(result.data)
return data.roles
end
function isRolePresent(user, role)
local identifiers = _GetIdentifiersTable(user)
if not identifiers.discord then return false end
local endpoint = ("guilds/%s/members/%s"):format(GuildID, identifiers.discord)
local result = DiscordRequest(endpoint, "GET", {})
if result.code ~= 200 then return false end
local data = decode(result.data)
local toFind = DiscordRoles[tostring(role)]
for k, v in pairs(data.roles) do
if toFind == v then
return true
end
end
end
Citizen.CreateThread(function()
local guild = DiscordRequest("guilds/" .. GuildID, "GET", {})
if guild.code ~= 200 then
Log("^1An error has occured with your guild information. (".. (guild.data or guild.code) .. ")")
return
end
local data = decode(guild.data)
Log("Permission guild was set to: " .. data.name .. " (ID: " .. data.id .. ")")
end)