TheRealToxicDev 修订了这个 Gist . 跳至此修订
1 file changed, 109 insertions
discord.lua(file created)
@@ -0,0 +1,109 @@ | |||
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 |
更新
更早