diff --git a/go.mod b/go.mod index 6282d6a68f..e45574a18c 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/FloatTech/sqlite v1.6.3 github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb - github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c + github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8 github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 github.com/antchfx/htmlquery v1.3.3 diff --git a/go.sum b/go.sum index de35f13c61..c1d648a653 100644 --- a/go.sum +++ b/go.sum @@ -17,8 +17,8 @@ github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs= github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb h1:sGqwCiMDyUD/znWEVVRVxbd6Kg1KLgGnnIuq5bCUWaQ= github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo= -github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c h1:hFiqx4uk6+lc2zHAaQ3JkkI2KH59c6O4yHKWKXFzxLs= -github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c/go.mod h1:MwTFLPhlP0qMMLcq4x90oiu1IVE1T5dN0ZsxyTGSf6k= +github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8 h1:XlOrX7WJqIQVBziB+ZXXzp/OG0YQICO5IC6T9s4iohE= +github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8/go.mod h1:2ZS6vZHn/imRIOhlDytd/FPVdBIY5KYdTPTIwBJU9Js= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU= github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w= diff --git a/gomod2nix.toml b/gomod2nix.toml index 9163867b7e..c8490b22f1 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -29,8 +29,8 @@ schema = 3 version = "v1.6.2-0.20240904160347-1317e11a15bb" hash = "sha256-x0ZR2bnkboEIjjRFMtMAN0T34BP9BPs7r3AwT3BCyzo=" [mod."github.com/FloatTech/zbputils"] - version = "v1.7.2-0.20240822065525-5ea6811ed91c" - hash = "sha256-ouAExps1iPCcD1AmOxyhRXMBGHBDXvUGkplcnQCf3Bg=" + version = "v1.7.2-0.20240911161040-1d89a7b10ff8" + hash = "sha256-3j7LE5Uqx5kPmi6c+WJEBFtLK0JGU4UGX7RpVuAjmf8=" [mod."github.com/RomiChan/syncx"] version = "v0.0.0-20240418144900-b7402ffdebc7" hash = "sha256-L1j1vgiwqXpF9pjMoRRlrQUHzoULisw/01plaEAwxs4=" diff --git a/plugin/chatcount/chatcount.go b/plugin/chatcount/chatcount.go index 6055c14f98..a4e1bb41a8 100644 --- a/plugin/chatcount/chatcount.go +++ b/plugin/chatcount/chatcount.go @@ -3,15 +3,21 @@ package chatcount import ( "fmt" + "image" + "net/http" "strconv" - "strings" + "sync" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" + "github.com/FloatTech/floatbox/file" + "github.com/FloatTech/imgfactory" + "github.com/FloatTech/rendercard" ctrl "github.com/FloatTech/zbpctrl" "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/img/text" ) const ( @@ -43,22 +49,53 @@ func init() { }) engine.OnFullMatch("查看水群排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true). Handle(func(ctx *zero.Ctx) { - text := strings.Builder{} - text.WriteString("今日水群排行榜:\n") chatTimeList := ctdb.getChatRank(ctx.Event.GroupID) + if len(chatTimeList) == 0 { + ctx.SendChain(message.Text("ERROR: 没有水群数据")) + return + } + rankinfo := make([]*rendercard.RankInfo, len(chatTimeList)) + + wg := &sync.WaitGroup{} + wg.Add(len(chatTimeList)) for i := 0; i < len(chatTimeList) && i < rankSize; i++ { - text.WriteString("第") - text.WriteString(strconv.Itoa(i + 1)) - text.WriteString("名:") - text.WriteString(ctx.CardOrNickName(chatTimeList[i].UserID)) - text.WriteString(" - ") - text.WriteString(strconv.FormatInt(chatTimeList[i].TodayMessage, 10)) - text.WriteString("条,共") - text.WriteString(strconv.FormatInt(chatTimeList[i].TodayTime/60, 10)) - text.WriteString("分") - text.WriteString(strconv.FormatInt(chatTimeList[i].TodayTime%60, 10)) - text.WriteString("秒\n") + go func(i int) { + defer wg.Done() + resp, err := http.Get("https://q4.qlogo.cn/g?b=qq&nk=" + strconv.FormatInt(chatTimeList[i].UserID, 10) + "&s=100") + if err != nil { + return + } + defer resp.Body.Close() + img, _, err := image.Decode(resp.Body) + if err != nil { + return + } + rankinfo[i] = &rendercard.RankInfo{ + TopLeftText: ctx.CardOrNickName(chatTimeList[i].UserID), + BottomLeftText: "消息数: " + strconv.FormatInt(chatTimeList[i].TodayMessage, 10) + " 条", + RightText: strconv.FormatInt(chatTimeList[i].TodayTime/60, 10) + "分" + strconv.FormatInt(chatTimeList[i].TodayTime%60, 10) + "秒", + Avatar: img, + } + }(i) + } + wg.Wait() + fontbyte, err := file.GetLazyData(text.GlowSansFontFile, control.Md5File, true) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + img, err := rendercard.DrawRankingCard(fontbyte, "今日水群排行榜", rankinfo) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + sendimg, err := imgfactory.ToBytes(img) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + if id := ctx.SendChain(message.ImageBytes(sendimg)); id.ID() == 0 { + ctx.SendChain(message.Text("ERROR: 可能被风控了")) } - ctx.SendChain(message.Text(text.String())) }) }