Advertisement
glitchdetector

FiveM vRP User 3rd Party Data

Feb 6th, 2019
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1. local FormattedToken = "Bot " .. config.discordBotToken
  2. function vRP.DiscordRequest(method, endpoint, jsondata)
  3.     local data = nil
  4.     PerformHttpRequest("https://discordapp.com/api/"..endpoint, function(errorCode, resultData, resultHeaders)
  5.         data = {data=resultData, code=errorCode, headers=resultHeaders}
  6.     end, method, #jsondata > 0 and json.encode(jsondata) or "", {["Content-Type"] = "application/json", ["Authorization"] = FormattedToken})
  7.  
  8.     while data == nil do
  9.         Citizen.Wait(0)
  10.     end
  11.  
  12.     return data
  13. end
  14.  
  15. function vRP.loadUserDiscordData(source, user_id, cbr)
  16.     local callback = false
  17.     local function cb(ret)
  18.         if not callback then
  19.             callback = true
  20.             cbr(ret)
  21.         end
  22.     end
  23.  
  24.     local discordId = nil
  25.     for _, id in ipairs(GetPlayerIdentifiers(source)) do
  26.         if string.match(id, "discord:") then
  27.             discordId = string.gsub(id, "discord:", "")
  28.             break
  29.         end
  30.     end
  31.  
  32.     if discordId then
  33.         local result = vRP.DiscordRequest("GET", "users/" .. discordId, {})
  34.         local data = json.decode(result.data)
  35.         if data then
  36.             if data['avatar'] then
  37.                 vRP.user_tmp_tables[user_id].avatar_hash = data['avatar']
  38.                 if string.sub(data['avatar'], 1, 2) == "a_" then
  39.                     vRP.user_tmp_tables[user_id].avatar = "https://cdn.discordapp.com/avatars/" .. discordId .. "/" .. data['avatar'] .. ".gif"
  40.                 else
  41.                     vRP.user_tmp_tables[user_id].avatar = "https://cdn.discordapp.com/avatars/" .. discordId .. "/" .. data['avatar'] .. ".png"
  42.                 end
  43.             end
  44.             if data['id'] then
  45.                 vRP.user_tmp_tables[user_id].discord_id = data['id']
  46.             end
  47.             if data['username'] then
  48.                 vRP.user_tmp_tables[user_id].discord_username = data['username']
  49.             end
  50.             if data['discriminator'] then
  51.                 vRP.user_tmp_tables[user_id].discord_discriminator = data['discriminator']
  52.             end
  53.             if data['username'] and data['discriminator'] then
  54.                 vRP.user_tmp_tables[user_id].discord = data['username'] .. "#" .. data['discriminator']
  55.             end
  56.         end
  57.         cb(true)
  58.     else
  59.         cb(false)
  60.     end
  61.     Wait(500)
  62.     if not callback then
  63.         cb(false)
  64.     end
  65. end
  66.  
  67. function vRP.initializeUserData(source, user_id, cbr)
  68.     local callback = false
  69.     local function cb(ret)
  70.         if not callback then
  71.             callback = true
  72.             cbr(ret)
  73.         end
  74.     end
  75.  
  76.     vRP.user_tmp_tables[user_id].connected = os.time()
  77.  
  78.     cb(true)
  79. end
  80.  
  81. -- Complicated spaghet that fetches the users Steam Avatar
  82. local SAK = config.steamApiKey
  83. function vRP.loadUserSteamData(source, user_id, cbr)
  84.     local callback = false
  85.     local function cb(ret)
  86.         if not callback then
  87.             callback = true
  88.             cbr(ret)
  89.         end
  90.     end
  91.     local function validResponse(text, statusCode)
  92.         return (text ~= nil and statusCode ~= nil and tonumber(statusCode) == 200)
  93.     end
  94.     local steamid = GetPlayerIdentifier(source, 0)
  95.     if not steamid then
  96.         cb(true)
  97.         return true
  98.     end
  99.     if not string.find(steamid, "steam:") then
  100.         cb(true)
  101.         return true
  102.     end
  103.     local steam64 = tonumber(string.gsub(steamid,"steam:", ""),16)
  104.     if not steam64 then
  105.         cb(true)
  106.         return true
  107.     end
  108.     PerformHttpRequest('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='..SAK..'&steamids='..steam64..'', function(statusCode, text, headers)
  109.         if text then
  110.             if validResponse(text, statusCode) then
  111.                 local info = json.decode(text)
  112.                 if info['response']['players'][1]['avatarmedium'] then
  113.                     vRP.user_tmp_tables[user_id].avatar = info['response']['players'][1]['avatarmedium']
  114.                 end
  115.                 if info['response']['players'][1]['steamid'] then
  116.                     vRP.user_tmp_tables[user_id].steamid = info['response']['players'][1]['steamid']
  117.                 end
  118.                 if info['response']['players'][1]['profileurl'] then
  119.                     vRP.user_tmp_tables[user_id].profileurl = info['response']['players'][1]['profileurl']
  120.                 end
  121.             else
  122.  
  123.             end
  124.         end
  125.         cb(true)
  126.     end, 'GET', json.encode({}), { ["Content-Type"] = 'application/json' })
  127.     Wait(500)
  128.     if not callback then
  129.         cb(false)
  130.     end
  131. end
  132.  
  133. function vRP.getAvatar(user_id)
  134.     if not vRP.user_tmp_tables[user_id] then return nil end
  135.     return vRP.user_tmp_tables[user_id].avatar
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement