From 20f2290ba4550b640da9184adacaec67ec2b970c Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Fri, 25 Dec 2020 23:12:29 +0800 Subject: [PATCH] feature set_group_anonymous_ban. --- coolq/api.go | 20 ++++++++++++++++++++ coolq/event.go | 6 +++--- server/http.go | 15 +++++++++++++++ server/websocket.go | 14 ++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index 9a1a36d..1f49fba 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -840,6 +840,26 @@ func (bot *CQBot) CQSetGroupPortrait(groupId int64, file, cache string) MSG { return Failed(100, "GROUP_NOT_FOUND", "群聊不存在") } +func (bot *CQBot) CQSetGroupAnonymousBan(groupId int64, flag string, duration int32) MSG { + if flag == "" { + return Failed(100, "INVALID_FLAG", "无效的flag") + } + if g := bot.Client.FindGroup(groupId); g != nil { + s := strings.SplitN(flag, "|", 2) + if len(s) != 2 { + return Failed(100, "INVALID_FLAG", "无效的flag") + } + id := s[0] + nick := s[1] + if err := g.MuteAnonymous(id, nick, duration); err != nil { + log.Warnf("anonymous ban error: %v", err) + return Failed(100, "CALL_API_ERROR", err.Error()) + } + return OK(nil) + } + return Failed(100, "GROUP_NOT_FOUND", "群聊不存在") +} + // https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_status-%E8%8E%B7%E5%8F%96%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81 func (bot *CQBot) CQGetStatus() MSG { return OK(MSG{ diff --git a/coolq/event.go b/coolq/event.go index b826d91..5b9dd79 100644 --- a/coolq/event.go +++ b/coolq/event.go @@ -114,9 +114,9 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) } if m.Sender.IsAnonymous() { gm["anonymous"] = MSG{ - "flag": "", - "id": 0, - "name": m.Sender.Nickname, + "flag": m.Sender.AnonymousInfo.AnonymousId + "|" + m.Sender.AnonymousInfo.AnonymousNick, + "id": m.Sender.Uin, + "name": m.Sender.AnonymousInfo.AnonymousNick, } gm["sender"].(MSG)["nickname"] = "匿名消息" gm["sub_type"] = "anonymous" diff --git a/server/http.go b/server/http.go index 34c15d2..f036be5 100644 --- a/server/http.go +++ b/server/http.go @@ -399,6 +399,20 @@ func GetGroupAtAllRemain(s *httpServer, c *gin.Context) { c.JSON(200, s.bot.CQGetAtAllRemain(gid)) } +func SetGroupAnonymousBan(s *httpServer, c *gin.Context) { + gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64) + d, _ := strconv.ParseInt(getParam(c, "duration"), 10, 64) + flag := getParam(c, "flag") + if flag == "" { + flag = getParam(c, "anonymous_flag") + } + if flag == "" { + o := gjson.Parse(getParam(c, "anonymous")) + flag = o.Get("flag").String() + } + c.JSON(200, s.bot.CQSetGroupAnonymousBan(gid, flag, int32(d))) +} + func HandleQuickOperation(s *httpServer, c *gin.Context) { if c.Request.Method != "POST" { c.AbortWithStatus(404) @@ -516,6 +530,7 @@ var httpApi = map[string]func(s *httpServer, c *gin.Context){ "get_stranger_info": GetStrangerInfo, "reload_event_filter": ReloadEventFilter, "set_group_portrait": SetGroupPortrait, + "set_group_anonymous_ban": SetGroupAnonymousBan, ".handle_quick_operation": HandleQuickOperation, ".ocr_image": OcrImage, "ocr_image": OcrImage, diff --git a/server/websocket.go b/server/websocket.go index 0826662..11f1931 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -553,6 +553,20 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{ "set_group_portrait": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQSetGroupPortrait(p.Get("group_id").Int(), p.Get("file").String(), p.Get("cache").String()) }, + "set_group_anonymous_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { + obj := p.Get("anonymous") + flag := p.Get("anonymous_flag") + if !flag.Exists() { + flag = p.Get("flag") + } + if !flag.Exists() && !obj.Exists() { + return coolq.Failed(100, "FLAG_NOT_FOUND", "flag未找到") + } + if !flag.Exists() { + flag = obj.Get("flag") + } + return bot.CQSetGroupAnonymousBan(p.Get("group_id").Int(), flag.String(), int32(p.Get("duration").Int())) + }, ".handle_quick_operation": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQHandleQuickOperation(p.Get("context"), p.Get("operation")) },