diff --git a/config.lua b/config.lua index a29af53..dd0cf88 100644 --- a/config.lua +++ b/config.lua @@ -9,6 +9,7 @@ Config = { DebugScript = false, CacheDiscordRoles = true, -- true to cache player roles, false to make a new Discord Request every time CacheDiscordRolesTime = 60, -- if CacheDiscordRoles is true, how long to cache roles before clearing (in seconds) + MaxStartAttempts = 15, -- This is in 500 millisecond per attempt, wouldn't set higher than 60 } Config.Splash = { @@ -19,4 +20,4 @@ Config.Splash = { Heading2 = "Make sure to join our Discord and check out our website!", Discord_Link = 'https://discord.gg', Website_Link = 'https://badger.store', -} \ No newline at end of file +} diff --git a/server.lua b/server.lua index f7540b1..495f049 100644 --- a/server.lua +++ b/server.lua @@ -1,4 +1,6 @@ local FormattedToken = "Bot " .. Config.Bot_Token +local resourceState = "stopped" +local attempts = 0 local error_codes_defined = { [200] = 'OK - The request was completed successfully..!', @@ -11,19 +13,62 @@ local error_codes_defined = { [502] = 'Error - Discord API may be down?...' }; -Citizen.CreateThread(function() - if (GetCurrentResourceName() ~= "Badger_Discord_API") then - --StopResource(GetCurrentResourceName()); - print("[" .. GetCurrentResourceName() .. "] " .. "IMPORTANT: This resource must be named Badger_Discord_API for it to work properly with other scripts..."); +while resourceState ~= "started" do + attempts = attempts + 1 + Wait(500) + resourceState = GetResourceState(GetCurrentResourceName()) + if attempts > Config.MaxStartAttempts then + print('[Badger_Discord_API] The resource took too long to start!') + return end print("[Badger_Discord_API] For support, make sure to join Badger's official Discord server: discord.gg/WjB5VFz"); print('^7[^2Zap-Hosting^7] ^3Use code ^5TheWolfBadger-4765 ^3at checkout for ^220% ^3off of selected services. Visit ^5https://zap-hosting.com/badger ^3to get started!'); -end) +end + +if GetCurrentResourceName() ~= "Badger_Discord_API" then + print("[" .. GetCurrentResourceName() .. "] " .. "IMPORTANT: This resource must be named Badger_Discord_API for it to work properly with other scripts..."); + print("[" .. GetCurrentResourceName() .. "] " .. "Resource stopped!"); + StopResource(GetCurrentResourceName()) +end + +function DiscordRequest(method, endpoint, jsondata, reason) + local data = nil + PerformHttpRequest("https://discord.com/api/"..endpoint, function(errorCode, resultData, resultHeaders) + data = {data=resultData, code=errorCode, headers=resultHeaders} + end, method, #jsondata > 0 and jsondata or "", {["Content-Type"] = "application/json", ["Authorization"] = FormattedToken, ['X-Audit-Log-Reason'] = reason}) + + while data == nil do + Citizen.Wait(0) + end + + return data +end function sendDebugMessage(msg) print("^1[^5Badger_Discord_API^1] ^3" .. msg); end +function GuildConnection(guildID) + local guild = DiscordRequest("GET", "guilds/"..guildID, {}) + if guild.code == 200 then + local data = json.decode(guild.data) + sendDebugMessage("[Badger_Discord_API] Successful connection to Guild : "..data.name.." ("..data.id..")") + else + sendDebugMessage("[Badger_Discord_API] An error occured, please check your config and ensure everything is correct. Error: "..(guild.data and json.decode(guild.data) or guild.code)) + end +end + +if Config.Multiguild then + print("[Badger_Discord_API] Attempting multi guild connections!"); + GuildConnection(Config.Guild_ID) + for _,guildIDs in pairs(Config.Guilds) do + GuildConnection(guildIDs) + end +else + print("[Badger_Discord_API] Attempting guild connection!"); + GuildConnection(Config.Guild_ID) +end + tracked = {} RegisterNetEvent('Badger_Discord_API:PlayerLoaded') @@ -32,7 +77,6 @@ AddEventHandler('Badger_Discord_API:PlayerLoaded', function() TriggerClientEvent('chatMessage', -1, '^1[^5SCRIPT ERROR^1] ^3The script ^1' .. GetCurrentResourceName() .. ' ^3will not work properly... You must ' .. 'rename the resource to ^1Badger_Discord_API'); end - local license = GetIdentifier(source, 'license'); if (tracked[license] == nil) then tracked[license] = true; TriggerClientEvent('chatMessage', source, @@ -82,19 +126,6 @@ function GetGuildId (guildName) return result end -function DiscordRequest(method, endpoint, jsondata, reason) - local data = nil - PerformHttpRequest("https://discord.com/api/"..endpoint, function(errorCode, resultData, resultHeaders) - data = {data=resultData, code=errorCode, headers=resultHeaders} - end, method, #jsondata > 0 and jsondata or "", {["Content-Type"] = "application/json", ["Authorization"] = FormattedToken, ['X-Audit-Log-Reason'] = reason}) - - while data == nil do - Citizen.Wait(0) - end - - return data -end - function GetRoleIdFromRoleName(name, guild --[[optional]]) local guildId = GetGuildId(guild) if (Caches.RoleList[guildId] ~= nil) then @@ -490,6 +521,10 @@ function GetUserRolesInGuild (user, guild) Citizen.SetTimeout(((Config.CacheDiscordRolesTime or 60)*1000), function() recent_role_cache[user][guild] = nil end) end return roles + elseif member.code == 404 then + -- This code is ignored here because if a user isn't a member of a guild it throws an error that doesn't mean anything + sendDebugMessage("[Badger_Discord_API] The user was not a member of one of your guilds, this is safe to ignore.") + Wait(1) else sendDebugMessage("[Badger_Discord_API] ERROR: Code 200 was not reached... Returning false. [Member Data NOT FOUND] DETAILS: " .. error_codes_defined[member.code]) return false @@ -623,24 +658,3 @@ function ChangeDiscordVoice(user, voice, reason) end end end - -Citizen.CreateThread(function() - local mguild = DiscordRequest("GET", "guilds/"..Config.Guild_ID, {}) - if mguild.code == 200 then - local data = json.decode(mguild.data) - sendDebugMessage("[Badger_Discord_API] Successful connection to Guild : "..data.name.." ("..data.id..")") - else - sendDebugMessage("[Badger_Discord_API] An error occured, please check your config and ensure everything is correct. Error: "..(mguild.data and json.decode(mguild.data) or mguild.code)) - end - if (Config.Multiguild) then - for _,guildID in pairs(Config.Guilds) do - local guild = DiscordRequest("GET", "guilds/"..guildID, {}) - if guild.code == 200 then - local data = json.decode(guild.data) - sendDebugMessage("[Badger_Discord_API] Successful connection to Guild : "..data.name.." ("..data.id..")") - else - sendDebugMessage("[Badger_Discord_API] An error occured, please check your config and ensure everything is correct. Error: "..(guild.data and json.decode(guild.data) or guild.code)) - end - end - end -end)