ZeroBot-Plugin/plugin_minecraft/info.go
2022-02-09 15:43:14 +08:00

59 lines
1.3 KiB
Go

package minecraft
import (
"encoding/json"
"fmt"
"net/http"
)
type resultjson struct {
IP string `json:"ip"`
Port int `json:"port"`
Debug struct {
Ping bool `json:"ping"`
Query bool `json:"query"`
Srv bool `json:"srv"`
Querymismatch bool `json:"querymismatch"`
Ipinsrv bool `json:"ipinsrv"`
Cnameinsrv bool `json:"cnameinsrv"`
Animatedmotd bool `json:"animatedmotd"`
Cachetime int `json:"cachetime"`
Apiversion int `json:"apiversion"`
} `json:"debug"`
Motd struct {
Raw []string `json:"raw"`
Clean []string `json:"clean"`
HTML []string `json:"html"`
} `json:"motd"`
Players struct {
Online int `json:"online"`
Max int `json:"max"`
List []string `json:"list"`
} `json:"players"`
}
var (
servers = make(map[string]string)
)
// 开放api请求调用
func infoapi(addr string) *resultjson {
url := "https://api.mcsrvstat.us/2/" + addr
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
result := &resultjson{}
if err := json.NewDecoder(res.Body).Decode(result); err != nil {
panic(err)
}
return result
}