Lua IP Show [HELP]

Epepe

Новичок
Автор темы
7
0
Версия MoonLoader
.027.0-preview
Hello there, I'm trying to make a .lua mod for SA:MP and I need help with a command, I'd like to make a command like [/myip] and I want it to display my IP in chat. Can you please help me? (sorry that I can't speak Russion but I don't have any Russion knowledge).
 
Последнее редактирование:
Решение
Thank you, but I only want a text in chat saying the IP address, how can I eliminate the dialog and just have text?
Lua:
local effil = require('effil')
local callback = {
    ok = function(response)
        if response.status_code ~= 200 then return sampAddChatMessage('error, code '..response.status_code, -1) end
        local status, data = pcall(decodeJson, response.text)
        if not status or not data then return sampAddChatMessage('error decoding json!', -1) end
        sampAddChatMessage('Your ip: '..(data.query or 'error'), -1)
    end,
    error = function(response)
        sampAddChatMessage('error: '..response, -1)
    end
}

function main()
    while not isSampAvailable() do wait(0) end...

YarikVL

Известный
Проверенный
4,796
1,813
Hello there, I'm trying to make a .lua mod for SA:MP and I need help with a command, I'd like to make a command like [/myip] and I want it to display my IP in chat. Can you please help me?
 

|Il|Il|

Активный
407
70
Hello there, I'm trying to make a .lua mod for SA:MP and I need help with a command, I'd like to make a command like [/myip] and I want it to display my IP in chat. Can you please help me? (sorry that I can't speak Russion but I don't have any Russion knowledge).
Here is the website for getting your IP

Function for taking only numbers from IP
asyncHttpRequest('GET', 'http://ip-api.com/json/?fields=query&lang=ru', nil, function(response) Strana = response.text:match('{"query":"(.*)"}') end)
 
  • Bug
Реакции: chapo

Epepe

Новичок
Автор темы
7
0

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,776
11,228
/ipinfo
1682173125688.png

Lua:
local effil = require('effil')
local callback = {
    ok = function(response)
        if response.status_code ~= 200 then
            return sampAddChatMessage('error, code '..response.status_code, -1)
        end
        local status, data = pcall(decodeJson, response.text)
        if not status or not data then
            return sampAddChatMessage('error decoding json!', -1)
        end
        local text = {}
        for field, value in pairs(data) do
            table.insert(text, field..'\t'..value)
        end
        sampShowDialog(891, 'IP INFO', table.concat(text, '\n'), 'X', nil, 4)
    end,
    error = function(response)
        sampAddChatMessage('error: '..response, -1)
    end
}

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('ipinfo', function()
        asyncHttpRequest('GET', 'http://ip-api.com/json/', nil, callback.ok, callback.error)
    end)
    wait(-1)
end

function asyncHttpRequest(method, url, args, resolve, reject)
    local request_thread = effil.thread(function (method, url, args)
        local requests = require 'requests'
        local result, response = pcall(requests.request, method, url, args)
        if result then
            response.json, response.xml = nil, nil
            return true, response
        else
            return false, response
        end
    end)(method, url, args)
    if not resolve then resolve = function() end end
    if not reject then reject = function() end end
    lua_thread.create(function()
        local runner = request_thread
        while true do
            local status, err = runner:status()
            if not err then
                if status == 'completed' then
                    local result, response = runner:get()
                    if result then
                        resolve(response)
                    else
                        reject(response)
                    end
                    return
                elseif status == 'canceled' then
                    return reject(status)
                end
            else
                return reject(err)
            end
            wait(0)
        end
    end)
end
 

|Il|Il|

Активный
407
70
Посмотреть вложение 198842
Lua:
local effil = require('effil')
local callback = {
    ok = function(response)
        if response.status_code ~= 200 then
            return sampAddChatMessage('error, code '..response.status_code, -1)
        end
        local status, data = pcall(decodeJson, response.text)
        if not status or not data then
            return sampAddChatMessage('error decoding json!', -1)
        end
        local text = {}
        for field, value in pairs(data) do
            table.insert(text, field..'\t'..value)
        end
        sampShowDialog(891, 'IP INFO', table.concat(text, '\n'), 'X', nil, 4)
    end,
    error = function(response)
        sampAddChatMessage('error: '..response, -1)
    end
}

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('ipinfo', function()
        asyncHttpRequest('GET', 'http://ip-api.com/json/', nil, callback.ok, callback.error)
    end)
    wait(-1)
end

function asyncHttpRequest(method, url, args, resolve, reject)
    local request_thread = effil.thread(function (method, url, args)
        local requests = require 'requests'
        local result, response = pcall(requests.request, method, url, args)
        if result then
            response.json, response.xml = nil, nil
            return true, response
        else
            return false, response
        end
    end)(method, url, args)
    if not resolve then resolve = function() end end
    if not reject then reject = function() end end
    lua_thread.create(function()
        local runner = request_thread
        while true do
            local status, err = runner:status()
            if not err then
                if status == 'completed' then
                    local result, response = runner:get()
                    if result then
                        resolve(response)
                    else
                        reject(response)
                    end
                    return
                elseif status == 'canceled' then
                    return reject(status)
                end
            else
                return reject(err)
            end
            wait(0)
        end
    end)
end
Как пункты в окне поменять на русскоязычные слова
к примеру zip - Айпи, City - Город и тд
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,776
11,228
Как пункты в окне поменять на русскоязычные слова
к примеру zip - Айпи, City - Город и тд
меняешь 13 строку на это
Lua:
table.insert(text, (keys[field] or field)..'\t'..value)
а это в начало скрипта:
Lua:
local keys = {
    query = 'IP',
    countryCode = 'Код страны'
}
 
  • Нравится
Реакции: de_clain

Epepe

Новичок
Автор темы
7
0
/ipinfo
Посмотреть вложение 198842
Lua:
local effil = require('effil')
local callback = {
    ok = function(response)
        if response.status_code ~= 200 then
            return sampAddChatMessage('error, code '..response.status_code, -1)
        end
        local status, data = pcall(decodeJson, response.text)
        if not status or not data then
            return sampAddChatMessage('error decoding json!', -1)
        end
        local text = {}
        for field, value in pairs(data) do
            table.insert(text, field..'\t'..value)
        end
        sampShowDialog(891, 'IP INFO', table.concat(text, '\n'), 'X', nil, 4)
    end,
    error = function(response)
        sampAddChatMessage('error: '..response, -1)
    end
}

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('ipinfo', function()
        asyncHttpRequest('GET', 'http://ip-api.com/json/', nil, callback.ok, callback.error)
    end)
    wait(-1)
end

function asyncHttpRequest(method, url, args, resolve, reject)
    local request_thread = effil.thread(function (method, url, args)
        local requests = require 'requests'
        local result, response = pcall(requests.request, method, url, args)
        if result then
            response.json, response.xml = nil, nil
            return true, response
        else
            return false, response
        end
    end)(method, url, args)
    if not resolve then resolve = function() end end
    if not reject then reject = function() end end
    lua_thread.create(function()
        local runner = request_thread
        while true do
            local status, err = runner:status()
            if not err then
                if status == 'completed' then
                    local result, response = runner:get()
                    if result then
                        resolve(response)
                    else
                        reject(response)
                    end
                    return
                elseif status == 'canceled' then
                    return reject(status)
                end
            else
                return reject(err)
            end
            wait(0)
        end
    end)
end
Thank you, but I only want a text in chat saying the IP address, how can I eliminate the dialog and just have text with just the IP address?
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,776
11,228
Thank you, but I only want a text in chat saying the IP address, how can I eliminate the dialog and just have text?
Lua:
local effil = require('effil')
local callback = {
    ok = function(response)
        if response.status_code ~= 200 then return sampAddChatMessage('error, code '..response.status_code, -1) end
        local status, data = pcall(decodeJson, response.text)
        if not status or not data then return sampAddChatMessage('error decoding json!', -1) end
        sampAddChatMessage('Your ip: '..(data.query or 'error'), -1)
    end,
    error = function(response)
        sampAddChatMessage('error: '..response, -1)
    end
}

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('ipinfo', function()
        asyncHttpRequest('GET', 'http://ip-api.com/json/', nil, callback.ok, callback.error)
    end)
    wait(-1)
end

function asyncHttpRequest(method, url, args, resolve, reject)
    local request_thread = effil.thread(function (method, url, args)
        local requests = require 'requests'
        local result, response = pcall(requests.request, method, url, args)
        if result then
            response.json, response.xml = nil, nil
            return true, response
        else
            return false, response
        end
    end)(method, url, args)
    if not resolve then resolve = function() end end
    if not reject then reject = function() end end
    lua_thread.create(function()
        local runner = request_thread
        while true do
            local status, err = runner:status()
            if not err then
                if status == 'completed' then
                    local result, response = runner:get()
                    if result then
                        resolve(response)
                    else
                        reject(response)
                    end
                    return
                elseif status == 'canceled' then
                    return reject(status)
                end
            else
                return reject(err)
            end
            wait(0)
        end
    end)
end
 
  • Нравится
Реакции: YarikVL

Epepe

Новичок
Автор темы
7
0
Lua:
local effil = require('effil')
local callback = {
    ok = function(response)
        if response.status_code ~= 200 then return sampAddChatMessage('error, code '..response.status_code, -1) end
        local status, data = pcall(decodeJson, response.text)
        if not status or not data then return sampAddChatMessage('error decoding json!', -1) end
        sampAddChatMessage('Your ip: '..(data.query or 'error'), -1)
    end,
    error = function(response)
        sampAddChatMessage('error: '..response, -1)
    end
}

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('ipinfo', function()
        asyncHttpRequest('GET', 'http://ip-api.com/json/', nil, callback.ok, callback.error)
    end)
    wait(-1)
end

function asyncHttpRequest(method, url, args, resolve, reject)
    local request_thread = effil.thread(function (method, url, args)
        local requests = require 'requests'
        local result, response = pcall(requests.request, method, url, args)
        if result then
            response.json, response.xml = nil, nil
            return true, response
        else
            return false, response
        end
    end)(method, url, args)
    if not resolve then resolve = function() end end
    if not reject then reject = function() end end
    lua_thread.create(function()
        local runner = request_thread
        while true do
            local status, err = runner:status()
            if not err then
                if status == 'completed' then
                    local result, response = runner:get()
                    if result then
                        resolve(response)
                    else
                        reject(response)
                    end
                    return
                elseif status == 'canceled' then
                    return reject(status)
                end
            else
                return reject(err)
            end
            wait(0)
        end
    end)
end
Thank you very much 👍