Jump to content

[HELP] fetchRemote ip json on player join


FIY9AL

Recommended Posts

Hello evreyone, i have this code and it should be outputChatBox on player join his country name but i tried to get it work but nothing show on chatbox
here is the code:

addEventHandler("onPlayerJoin", root,
    function()
        function displayPlayerCity(resp, errno, source)
            if errno == 0 then
                data = fromJSON("[".. resp .. "]")
                status = data["status"] or "fail"
                if status == "success" then
                    if isElement(source) then
                        outputChatBox(getPlayerName(source).." has joined the game ("..data["city"]..", "..data["country"]..")", root, 255, 100, 100)
                    end
                else
                    if isElement(source) then
                        outputChatBox(getPlayerName(source).." has joined the game.", root, 255, 100, 100)
                    end
                    outputDebugString("JOINQUIT: Cannot retrieve network details")
                end
            else
                outputDebugString("JOINQUIT: Error @fetchRemote, ERRNO: "..errno)
            end
        end 
        fetchRemote("http://ip-api.com/json/" .. getPlayerIP(source), displayPlayerCity, "", false, source)
    end
)

Any idea ?

Link to comment
  • Other Languages Moderators
addEventHandler("onPlayerJoin", root,
    function()
        local ipAddress = getPlayerIP(source)

        fetchRemote("http://ip-api.com/json/" .. ipAddress, callback, "", false, source)
    end
)

function callback(data, error, player)
    if error == 0 then
        local json = fromJSON("[" .. data .. "]")

        if json then
            local country = json.country or "???"
            local region = json.region or "???"

            outputChatBox("* " .. getPlayerName(player) .. " joined the server. [" .. country .. " - " .. region .. "]", root, 255, 255, 0)
        end
    else
        outputChatBox("* " .. getPlayerName(player) .. " joined the server. [??? - ???]", root, 255, 255, 0)
    end
end

You need to give ACL rights to the resource. If it didn't help, try using my code above.

Link to comment
On 10/04/2020 at 17:59, andreisrww said:

addEventHandler("onPlayerJoin", root,
    function()
        local ipAddress = getPlayerIP(source)

        fetchRemote("http://ip-api.com/json/" .. ipAddress, callback, "", false, source)
    end
)

function callback(data, error, player)
    if error == 0 then
        local json = fromJSON("[" .. data .. "]")

        if json then
            local country = json.country or "???"
            local region = json.region or "???"

            outputChatBox("* " .. getPlayerName(player) .. " joined the server. [" .. country .. " - " .. region .. "]", root, 255, 255, 0)
        end
    else
        outputChatBox("* " .. getPlayerName(player) .. " joined the server. [??? - ???]", root, 255, 255, 0)
    end
end

You need to give ACL rights to the resource. If it didn't help, try using my code above.

Thank you its worked after i added it to admin acl and used your code :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...