From 92c9d1d2ce8aa40b0babe011e61cc784cea027b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E6=9F=B3=E7=85=9C?= <101934327+fangliuyu@users.noreply.github.com> Date: Mon, 9 May 2022 11:08:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E5=A4=A9=E4=B8=80=E5=A4=AB=E4=B8=80?= =?UTF-8?q?=E5=A6=BB=E5=88=B6=E7=BE=A4=E8=80=81=E5=A9=86=20=20(#219)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/qqwife/qqmapwife.go | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 plugin/qqwife/qqmapwife.go diff --git a/plugin/qqwife/qqmapwife.go b/plugin/qqwife/qqmapwife.go new file mode 100644 index 00000000..2743a866 --- /dev/null +++ b/plugin/qqwife/qqmapwife.go @@ -0,0 +1,146 @@ +// Package qqwife 娶群友 基于“翻牌”和江林大佬的“群老婆”插件魔改作品 +package qqwife + +import ( + "math/rand" + "sort" + "strconv" + "strings" + "sync" + "time" + + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" + + control "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/math" +) + +var ( + setcp = make(map[int64]int64, 60) //初始化中间的map元素,虽然取了30个人数,以防万一翻个倍储存 + qqwifegroup = make(map[int64]map[int64]int64, 50) //50个群的预算大小 + lastdate time.Time + rock sync.Mutex +) + +func init() { + engine := control.Register("qqwife", &control.Options{ + DisableOnDefault: false, + Help: "一群一天一夫一妻制群老婆\n" + + "-娶群友", + }) + engine.OnFullMatch("娶群友", zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser). + Handle(func(ctx *zero.Ctx) { + rock.Lock() + defer rock.Unlock() + if time.Now().Day() != lastdate.Day() { + qqwifegroup = make(map[int64]map[int64]int64, 50) //跨天就重新初始化数据 + } + //先判断是否已经娶过或者被娶 + groupid := ctx.Event.GroupID + uid := ctx.Event.UserID + var status string + //如果娶过 + userinfo, ok := qqwifegroup[groupid][uid] + if ok { + ctx.SendChain( + message.At(uid), + message.Text("今天你的群老婆是"), + message.Image("http://q4.qlogo.cn/g?b=qq&nk="+strconv.FormatInt(userinfo, 10)+"&s=640"), + message.Text( + "\n", + "[", ctx.CardOrNickName(userinfo), "]", + "(", strconv.FormatInt(userinfo, 10), ")哒", + ), + ) + return + } + //如果被娶过 + for k, v := range qqwifegroup[groupid] { + if v == uid { //如果为0且是在本群抽的就输出 + ctx.SendChain( + message.At(uid), + message.Text("今天你被娶了,群老公是"), + message.Image("http://q4.qlogo.cn/g?b=qq&nk="+strconv.FormatInt(k, 10)+"&s=640"), + message.Text( + "\n", + "[", ctx.CardOrNickName(k), "]", + "(", strconv.FormatInt(k, 10), ")哒", + ), + ) + return + } + } + // 无缓存获取群员列表 + temp := ctx.GetThisGroupMemberListNoCache().Array() + sort.SliceStable(temp, func(i, j int) bool { + return temp[i].Get("last_sent_time").Int() < temp[j].Get("last_sent_time").Int() + }) + temp = temp[math.Max(0, len(temp)-30):] + //将已经娶过的人剔除 + var qqgrouplist = []int64{} + if len(qqwifegroup) == 0 { + for k := 0; k < len(temp); k++ { + qqgrouplist = append(qqgrouplist, temp[k].Get("user_id").Int()) + } + } else { + for k := 0; k < len(temp); k++ { + _, ok := qqwifegroup[groupid][temp[k].Get("user_id").Int()] + if !ok { + qqgrouplist = append(qqgrouplist, temp[k].Get("user_id").Int()) + } + } + } + //没有人(只剩自己)的时候 + if len(qqgrouplist) == 0 { + ctx.SendChain(message.Text("噢,此时此刻你还是一只单身狗,等待下一次情缘吧")) + return + } + //随机抽娶 + who := qqgrouplist[rand.Intn(len(qqgrouplist))] + if who == uid { //如果是自己 + ctx.SendChain(message.Text("噢,此时此刻你还是一只单身狗,等待下一次情缘吧")) + return + } + //绑定CP + setcp[uid] = who + qqwifegroup[groupid] = setcp + //输出结果 + ctx.SendChain( + message.Text(status), + message.At(uid), + message.Text("今天你的群老婆是"), + message.Image("http://q4.qlogo.cn/g?b=qq&nk="+strconv.FormatInt(who, 10)+"&s=640"), + message.Text( + "\n", + "[", ctx.CardOrNickName(who), "]", + "(", strconv.FormatInt(who, 10), ")哒", + ), + ) + //更新时间 + lastdate = time.Now() + }) + engine.OnFullMatch("群老婆列表", zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser). + Handle(func(ctx *zero.Ctx) { + var cplist = []string{"群老公←———→群老婆\n--------------------------"} + rock.Lock() + defer rock.Unlock() + group, ok := qqwifegroup[ctx.Event.GroupID] + if !ok { + ctx.SendChain(message.Text("你群并没有任何的CP额")) + return + } + for k, v := range group { + if k != 0 { + husband := ctx.CardOrNickName(k) + wife := ctx.CardOrNickName(v) + cplist = append(cplist, husband+" & "+wife) + } else { + ctx.SendChain(message.Text("你群并没有任何的CP额")) + return + } + } + ctx.SendChain(message.Text(strings.Join(cplist, "\n"))) + }) +}