discord.lua
· 3.5 KiB · Lua
Raw
--[[
========================================
DISCORD MANAGER/CLIENT FOR FIVEM2DISCORD
========================================
- DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING
========================================
]]
local formattedToken = "Bot " .. CONFIG.DiscordBotToken
local function FM2Discord()
-- [[ HANDLE DISCORD REQUESTS SERVER SIDE ]]
function DiscordRequest(method, endpoint, jsondata)
local data = nil
PerformHttpRequest(CONFIG.DiscordAPIUrl .. endpoint, function(errorCode, resultData, resultHeaders)
data = { data=resultData, code=errorCode, headers=resultHeaders}
end, method, #jsondata > 0 and json.encode(jsondata) or "", {["Content-Type"] = "application/json", ["Authorization"] = formattedToken })
while data == nil do
Citizen.wait(0)
end
return data
end
-- [[ GET DISCORD USER DATA ]]
function GetDiscordUser(id)
local user = DiscordRequest("GET", "user/" .. id)
if user.code ~= 200 then
return nil
else
local userData = json.decode(user.data)
return userData.user
end
end
-- [[ GET DISCORD USER AVATAR ]]
function GetDiscordAvatar(id)
local id = string:gsub(ExtractIdentifiers(id).discord, "discord:", "")
local Userdata = GetDiscordUser(id)
if Userdata ~= nil then
if (Userdata.avatar:sub(1, 1) and Userdata.avatar:sub(2, 2) == '_') then
imgUrl = "https://cdn.discordapp.com/avatars/" .. id .. "/" .. Userdata.avatar .. ".gif"
else
imgUrl = "https://cdn.discordapp.com/avatars/" .. id .. "/" .. Userdata.avatar .. ".png"
end
else
imgUrl = CONFIG.System.UserAvatar
end
return imgUrl
end
function GetDiscordName(user, f, l)
if CONFIG.Discord.UseNames then
local id = string:gsub(ExtractIdentifiers(user).discord, "discord:", "")
local userData = GetDiscordUser(id)
if userData ~= nil then
if userData.discriminator ~= nil or 0 then
return userData.username
else
return userData.global_name
end
else
return GetPlayerName(user) or ''..f..' '..l..'' -- default
end
else
return GetPlayerName(user) or ''..f..' '..l..'' -- default
end
end
function ExtractIdentifiers(src)
local identifiers = {
steam = "",
ip = "",
discord = "",
license = "",
xbl = "",
live = "",
fivem = ""
}
for i = 0, GetNumPlayerIdentifiers(src) - 1 do
local id = GetPlayerIdentifier(src, i)
-- [[ LOOP OVER ALL AVAILABLE SERVER IDENTIFIERS ]]
if string.find(id, "steam") then
identifiers.steam = id
elseif string.find(id, "ip") then
identifiers.ip = id
elseif string.find(id, "discord") then
identifiers.discord = id
elseif string.find(id, "license") then
identifiers.license = id
elseif string.find(id, "xbl") then
identifiers.xbl = id
elseif string.find(id, "live") then
identifiers.live = id
elseif string.find(id, "fivem") then
identifiers.fivem = id
end
end
end
end
1 | --[[ |
2 | ======================================== |
3 | DISCORD MANAGER/CLIENT FOR FIVEM2DISCORD |
4 | ======================================== |
5 | - DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING |
6 | ======================================== |
7 | ]] |
8 | local formattedToken = "Bot " .. CONFIG.DiscordBotToken |
9 | |
10 | local function FM2Discord() |
11 | -- [[ HANDLE DISCORD REQUESTS SERVER SIDE ]] |
12 | function DiscordRequest(method, endpoint, jsondata) |
13 | |
14 | local data = nil |
15 | |
16 | PerformHttpRequest(CONFIG.DiscordAPIUrl .. endpoint, function(errorCode, resultData, resultHeaders) |
17 | data = { data=resultData, code=errorCode, headers=resultHeaders} |
18 | end, method, #jsondata > 0 and json.encode(jsondata) or "", {["Content-Type"] = "application/json", ["Authorization"] = formattedToken }) |
19 | |
20 | while data == nil do |
21 | Citizen.wait(0) |
22 | end |
23 | |
24 | return data |
25 | end |
26 | |
27 | -- [[ GET DISCORD USER DATA ]] |
28 | function GetDiscordUser(id) |
29 | local user = DiscordRequest("GET", "user/" .. id) |
30 | |
31 | if user.code ~= 200 then |
32 | return nil |
33 | else |
34 | local userData = json.decode(user.data) |
35 | return userData.user |
36 | end |
37 | end |
38 | |
39 | -- [[ GET DISCORD USER AVATAR ]] |
40 | function GetDiscordAvatar(id) |
41 | local id = string:gsub(ExtractIdentifiers(id).discord, "discord:", "") |
42 | local Userdata = GetDiscordUser(id) |
43 | |
44 | if Userdata ~= nil then |
45 | if (Userdata.avatar:sub(1, 1) and Userdata.avatar:sub(2, 2) == '_') then |
46 | imgUrl = "https://cdn.discordapp.com/avatars/" .. id .. "/" .. Userdata.avatar .. ".gif" |
47 | else |
48 | imgUrl = "https://cdn.discordapp.com/avatars/" .. id .. "/" .. Userdata.avatar .. ".png" |
49 | end |
50 | else |
51 | |
52 | imgUrl = CONFIG.System.UserAvatar |
53 | |
54 | end |
55 | |
56 | return imgUrl |
57 | end |
58 | |
59 | function GetDiscordName(user, f, l) |
60 | if CONFIG.Discord.UseNames then |
61 | local id = string:gsub(ExtractIdentifiers(user).discord, "discord:", "") |
62 | local userData = GetDiscordUser(id) |
63 | |
64 | if userData ~= nil then |
65 | if userData.discriminator ~= nil or 0 then |
66 | return userData.username |
67 | else |
68 | return userData.global_name |
69 | end |
70 | else |
71 | return GetPlayerName(user) or ''..f..' '..l..'' -- default |
72 | end |
73 | else |
74 | return GetPlayerName(user) or ''..f..' '..l..'' -- default |
75 | end |
76 | end |
77 | |
78 | function ExtractIdentifiers(src) |
79 | local identifiers = { |
80 | steam = "", |
81 | ip = "", |
82 | discord = "", |
83 | license = "", |
84 | xbl = "", |
85 | live = "", |
86 | fivem = "" |
87 | } |
88 | |
89 | for i = 0, GetNumPlayerIdentifiers(src) - 1 do |
90 | local id = GetPlayerIdentifier(src, i) |
91 | -- [[ LOOP OVER ALL AVAILABLE SERVER IDENTIFIERS ]] |
92 | if string.find(id, "steam") then |
93 | identifiers.steam = id |
94 | elseif string.find(id, "ip") then |
95 | identifiers.ip = id |
96 | elseif string.find(id, "discord") then |
97 | identifiers.discord = id |
98 | elseif string.find(id, "license") then |
99 | identifiers.license = id |
100 | elseif string.find(id, "xbl") then |
101 | identifiers.xbl = id |
102 | elseif string.find(id, "live") then |
103 | identifiers.live = id |
104 | elseif string.find(id, "fivem") then |
105 | identifiers.fivem = id |
106 | end |
107 | end |
108 | end |
109 | end |