diff --git a/main.go b/main.go index ed135ea4..8dce86b0 100644 --- a/main.go +++ b/main.go @@ -18,8 +18,9 @@ import ( // 娱乐类 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_ai_false" // 服务器监控 - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_music" // 点歌 - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_shindan" // 测定 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_minecraft" + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_music" // 点歌 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_shindan" // 测定 // b站相关 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili" // 查询b站用户信息 diff --git a/plugin_minecraft/info.go b/plugin_minecraft/info.go new file mode 100644 index 00000000..28b575f5 --- /dev/null +++ b/plugin_minecraft/info.go @@ -0,0 +1,84 @@ +package plugin_minecraft + +import ( + "encoding/json" + "fmt" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" + "net/http" + "strings" +) + +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"` +} + +func init() { + zero.OnRegex(`^/list (.*)$`). + Handle(func(ctx *zero.Ctx) { + // 支持多个服务器 + switch ctx.State["regex_matched"].([]string)[1] { + case "ftbi": // 这里对应触发指令里的服务器名称 + ftbijson := infoapi("115.28.186.22:25710") //这里填对应mc服务器的登录地址 + var str = ftbijson.Players.List + cs := strings.Join(str, "\n") + ctx.SendChain(message.Text( + "服务器名字: ", ftbijson.Motd.Raw[0], "\n", + "在线人数: ", ftbijson.Players.Online, "/", ftbijson.Players.Max, "\n", + "以下为玩家名字: ", "\n", cs, + )) + case "ges": // 这里对应触发指令里的服务器名称 + gesjson := infoapi("115.28.186.22:25701") //这里填对应mc服务器的登录地址 + var str = gesjson.Players.List + cs := strings.Join(str, "\n") + ctx.SendChain(message.Text( + "服务器名字: ", gesjson.Motd.Raw[0], "\n", + "在线人数: ", gesjson.Players.Online, "/", gesjson.Players.Max, "\n", + "以下为玩家名字: ", "\n", cs, + )) + } + }) +} + +// 开放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 +} diff --git a/plugin_minecraft/manager.go b/plugin_minecraft/manager.go new file mode 100644 index 00000000..311701d6 --- /dev/null +++ b/plugin_minecraft/manager.go @@ -0,0 +1,75 @@ +package plugin_minecraft + +import ( + "fmt" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" + "io/ioutil" + "net/http" +) + +// 此功能实现依赖MCSManager项目对服务器的管理api,mc服务器如果没有在该管理平台部署此功能无效 +// 项目地址: https://github.com/Suwings/MCSManager +// 项目的api文档: https://github.com/Suwings/MCSManager/wiki/API-Documentation + +func init() { + zero.OnRegex(`^/start (.*)$`). + Handle(func(ctx *zero.Ctx) { + name := ctx.State["regex_matched"].([]string)[1] + ctx.SendChain(message.Text("开启服务器: ", name, ".....")) + result := start(name) + ctx.Send(result) + }) +} + +func init() { + zero.OnRegex(`^/stop (.*)$`). + Handle(func(ctx *zero.Ctx) { + name := ctx.State["regex_matched"].([]string)[1] + ctx.SendChain(message.Text("关闭服务器: ", name, ".....")) + result := stop(name) + ctx.Send(result) + }) +} + +//开启服务器的api请求 +func start(name string) string { + url := fmt.Sprintf("http://your.addr:23333/api/start_server/%s/?apikey=apikey", name) + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err) + } + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } + return string(body) +} + +//关闭服务器的api请求 +func stop(name string) string { + url := fmt.Sprintf("http://your.addr:23333/api/stop_server/%s/?apikey=apikey", name) + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) + if err != nil { + fmt.Println(err) + } + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(err) + } + return string(body) +}