From 164feca7c72c4c121f2ae6707ba084e518ee74ef Mon Sep 17 00:00:00 2001 From: Mrs4s <1844812067@qq.com> Date: Sat, 23 Jan 2021 00:04:57 +0800 Subject: [PATCH] feature get_online_clients. --- coolq/api.go | 20 ++++++++++++++++++++ server/http.go | 5 +++++ server/websocket.go | 3 +++ 3 files changed, 28 insertions(+) diff --git a/coolq/api.go b/coolq/api.go index 1ed1d26..be8ca76 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -868,6 +868,26 @@ func (bot *CQBot) CQGetGroupMessageHistory(groupId int64, seq int64) MSG { }) } +func (bot *CQBot) CQGetOnlineClients(noCache bool) MSG { + if noCache { + if err := bot.Client.RefreshStatus(); err != nil { + log.Warnf("刷新客户端状态时出现问题 %v", err) + return Failed(100, "REFRESH_STATUS_ERROR", err.Error()) + } + } + var d []MSG + for _, oc := range bot.Client.OnlineClients { + d = append(d, MSG{ + "app_id": oc.AppId, + "device_name": oc.DeviceName, + "device_kind": oc.DeviceKind, + }) + } + return OK(MSG{ + "clients": d, + }) +} + func (bot *CQBot) CQCanSendImage() MSG { return OK(MSG{"yes": true}) } diff --git a/server/http.go b/server/http.go index 14e0eea..8b6832c 100644 --- a/server/http.go +++ b/server/http.go @@ -427,6 +427,10 @@ func GetGroupMessageHistory(s *httpServer, c *gin.Context) { c.JSON(200, s.bot.CQGetGroupMessageHistory(gid, seq)) } +func GetOnlineClients(s *httpServer, c *gin.Context) { + c.JSON(200, s.bot.CQGetOnlineClients(getParamOrDefault(c, "no_cache", "false") == "true")) +} + func HandleQuickOperation(s *httpServer, c *gin.Context) { if c.Request.Method != "POST" { c.AbortWithStatus(404) @@ -578,6 +582,7 @@ var httpApi = map[string]func(s *httpServer, c *gin.Context){ ".ocr_image": OcrImage, "ocr_image": OcrImage, "get_group_at_all_remain": GetGroupAtAllRemain, + "get_online_clients": GetOnlineClients, ".get_word_slices": GetWordSlices, } diff --git a/server/websocket.go b/server/websocket.go index 77f6465..78e0ae0 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -580,6 +580,9 @@ var wsAPI = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{ "get_group_at_all_remain": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQGetAtAllRemain(p.Get("group_id").Int()) }, + "get_online_clients": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { + return bot.CQGetOnlineClients(p.Get("no_cache").Bool()) + }, ".get_word_slices": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQGetWordSlices(p.Get("content").Str) },