From cf67d9a4e215ebfa6fad2757b975d65a70b8b1eb Mon Sep 17 00:00:00 2001 From: Andyyy7666 <86536434+Andyyy7666@users.noreply.github.com> Date: Wed, 23 Aug 2023 00:32:49 +0200 Subject: [PATCH] update: discord function to core object func --- server/functions.lua | 32 ++++++++++++++++++++++++++++++++ server/main.lua | 29 ----------------------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 1a34ecf..6721992 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -50,6 +50,38 @@ function NDCore.loadSQL(fileLocation, resource) return true end +function NDCore.getDiscordInfo(discordUserId) + local done = false + local data + local discordErrors = { + [400] = "Improper HTTP request", + [401] = "Discord bot token might be missing or incorrect", + [404] = "User might not be in the server", + [429] = "Discord bot rate limited" + } + + if not discordUserId then return end + if type(discordUserId) == "string" and discordUserId:find("discord:") then discordUserId:gsub("discord:", "") end + + PerformHttpRequest(("https://discordapp.com/api/guilds/%s/members/%s"):format(Config.discordGuildId, discordUserId), function(errorCode, resultData, resultHeaders) + if errorCode ~= 200 then + done = true + return print(("^3Warning: %d %s"):format(errorCode, discordErrors[errorCode])) + end + + local result = json.decode(resultData) + data = { + nickname = result.nick or result.user.username, + user = result.user, + roles = result.roles + } + done = true + end, "GET", "", {["Content-Type"] = "application/json", ["Authorization"] = ("Bot %s"):format(Config.discordBotToken)}) + + while not done do Wait(50) end + return data +end + for name, func in pairs(NDCore) do if type(func) == "function" then exports(name, func) diff --git a/server/main.lua b/server/main.lua index bde831b..11885ae 100644 --- a/server/main.lua +++ b/server/main.lua @@ -3,12 +3,6 @@ NDCore.players = {} PlayersInfo = {} local resourceName = GetCurrentResourceName() local tempPlayersInfo = {} -local discordErrors = { - [400] = "Improper HTTP request", - [401] = "Discord bot token might be missing or incorrect", - [404] = "User might not be in the server", - [429] = "Discord bot rate limited" -} Config = { serverName = GetConvar("core:serverName", "Unconfigured ND-Core Server"), @@ -45,29 +39,6 @@ local function getIdentifierList(src) return list end -local function getDiscordInfo(discordUserId) - local done = false - local data - - PerformHttpRequest(("https://discordapp.com/api/guilds/%s/members/%s"):format(Config.discordGuildId, discordUserId), function(errorCode, resultData, resultHeaders) - if errorCode ~= 200 then - done = true - return print(("^3Warning: %d %s"):format(errorCode, discordErrors[errorCode])) - end - - local result = json.decode(resultData) - data = { - nickname = result.nick or result.user.username, - user = result.user, - roles = result.roles - } - done = true - end, "GET", "", {["Content-Type"] = "application/json", ["Authorization"] = ("Bot %s"):format(Config.discordBotToken)}) - - while not done do Wait(500) end - return data -end - AddEventHandler("playerJoining", function(oldId) local src = source PlayersInfo[src] = tempPlayersInfo[oldId]