Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MemoryShadow/87f79ae763a97940a1885fcd68bca6e1 to your computer and use it in GitHub Desktop.
Save MemoryShadow/87f79ae763a97940a1885fcd68bca6e1 to your computer and use it in GitHub Desktop.
-- 查询指定Minecraft服务器当前的信息
-- 依赖: json,http
function motd(Domain)
-- 支持 域名:端口 语法
if (string.find(Domain, ':') ~= nil) then -- 当检测到冒号时,就做额外的词法解析,拆分端口和域名
Server = split(Domain, ':')[1]
prot = split(Domain, ':')[2]
else
-- 设置在未填写端口时
Server = Domain
prot = 25565
end
-- 对API发起请求
response, error_message = http.request("GET", "https://mcapi.us/server/status", {
query = string.format("ip=" .. Server .. "&port=" .. prot)
})
-- 处理json数据,处理转义
ServerInfoObj = json.decode(response.body)
-- 对获取失败的情况做出反应
if (ServerInfoObj.status == "success") then
-- 处理玩家列表,用于在末尾显示
if (ServerInfoObj.players.now ~= 0) then
PlayList = ServerInfoObj.players.sample[1].name
for i = 2, #ServerInfoObj.players.sample do
PlayList = PlayList .. "," .. ServerInfoObj.players.sample[i].name
end
else
PlayList = "" -- 避免出现问题的else分支
end
return
"ip: " .. Domain ..
"\n版本: " .. ServerInfoObj.server.name ..
"\n验证模式: " .. ((ServerInfoObj.online == true) and "联机" or "离线") .. "验证" ..
"\nmotd: " .. ServerInfoObj.motd ..
"\n最大玩家: " .. ServerInfoObj.players.max..
"\n" .. ServerInfoObj.players.now .. "名玩家在线: " .. PlayList
else
return "查询 ".. Domain .." 失败,以下是失败原因:\n" .. ServerInfoObj.error
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment