diff --git a/init.lua b/init.lua index b88768f..6ad0633 100644 --- a/init.lua +++ b/init.lua @@ -11,7 +11,9 @@ Config = { groups = json.decode(GetConvar("core:groups")) or {}, characterIdentifier = GetConvar("core:characterIdentifier", "license"), randomUnlockedVehicleChance = GetConvarInt("core:randomUnlockedVehicleChance", 30), - disableVehicleAirControl = GetConvarInt("core:disableVehicleAirControl", 1) == 1 + disableVehicleAirControl = GetConvarInt("core:disableVehicleAirControl", 1) == 1, + discordGuildId = GetConvar("core:discordGuildId"), + discordBotToken = GetConvar("core:discordBotToken") } local nd_core = exports["ND_Core"] diff --git a/server/main.lua b/server/main.lua index 18aa20b..629634a 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,5 +1,7 @@ NDCore = {} ActivePlayers = {} +PlayersInfo = {} +local tempPlayersInfo = {} AddEventHandler("playerDropped", function() local src = source @@ -9,8 +11,91 @@ AddEventHandler("playerDropped", function() end) SetConvarServerInfo("ND_Core", GetResourceMetadata(GetCurrentResourceName(), "version", 0) or "invalid") +SetConvarReplicated("inventory:framework", "nd") local file = LoadResourceFile(GetCurrentResourceName(), "query.sql") if file then MySQL.query(file) end + +local function getIdentifierList(src) + local list = {} + for i=0, GetNumPlayerIdentifiers(src) do + local identifier = GetPlayerIdentifier(src, i) + if identifier then + local colon = identifier:find(":") + list[identifierType] = identifier:sub(1, colon-1) + end + end + return list +end + +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" +} + +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(("Error %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] + tempPlayersInfo[oldId] = nil +end) + +AddEventHandler("playerConnecting", function(name, setKickReason, deferrals) + local tempSrc = source + local identifiers = getIdentifierList(tempSrc) + local mainIdentifier = identifiers[Config.characterIdentifier] + local discordInfo = {} + + deferrals.defer() + Wait(0) + + if mainIdentifier and Config.discordBotToken and Config.discordGuildId then + local discordIdentifier = identifiers["discord"] + if not discordIdentifier then + deferrals.done(("Your discord was not found, join our discord here: %s."):format(Config.discordInvite)) + Wait(0) + return + end + discordInfo = getDiscordInfo(discordIdentifier:gsub("discord:")) + end + + deferrals.update("Connecting...") + Wait(0) + + if mainIdentifier then + tempPlayersInfo[tempSrc] = { + identifiers = identifiers, + discord = discordInfo + } + deferrals.done() + else + deferrals.done(("Your %s was not found."):format(Config.characterIdentifier)) + Wait(0) + end +end)