feat(chatcount): rank use image (#1027)

* feat(chatcount): rank use image

* chore(lint): 改进代码样式 (#31)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Jiang-Red 2024-10-13 19:23:45 +08:00 committed by GitHub
parent 26d2074db6
commit 4cf296c839
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 58 additions and 21 deletions

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/FloatTech/sqlite v1.6.3 github.com/FloatTech/sqlite v1.6.3
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb 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/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5 github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
github.com/antchfx/htmlquery v1.3.3 github.com/antchfx/htmlquery v1.3.3

4
go.sum
View File

@ -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/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 h1:sGqwCiMDyUD/znWEVVRVxbd6Kg1KLgGnnIuq5bCUWaQ=
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo= 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.20240911161040-1d89a7b10ff8 h1:XlOrX7WJqIQVBziB+ZXXzp/OG0YQICO5IC6T9s4iohE=
github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c/go.mod h1:MwTFLPhlP0qMMLcq4x90oiu1IVE1T5dN0ZsxyTGSf6k= 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/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 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w= github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=

View File

@ -29,8 +29,8 @@ schema = 3
version = "v1.6.2-0.20240904160347-1317e11a15bb" version = "v1.6.2-0.20240904160347-1317e11a15bb"
hash = "sha256-x0ZR2bnkboEIjjRFMtMAN0T34BP9BPs7r3AwT3BCyzo=" hash = "sha256-x0ZR2bnkboEIjjRFMtMAN0T34BP9BPs7r3AwT3BCyzo="
[mod."github.com/FloatTech/zbputils"] [mod."github.com/FloatTech/zbputils"]
version = "v1.7.2-0.20240822065525-5ea6811ed91c" version = "v1.7.2-0.20240911161040-1d89a7b10ff8"
hash = "sha256-ouAExps1iPCcD1AmOxyhRXMBGHBDXvUGkplcnQCf3Bg=" hash = "sha256-3j7LE5Uqx5kPmi6c+WJEBFtLK0JGU4UGX7RpVuAjmf8="
[mod."github.com/RomiChan/syncx"] [mod."github.com/RomiChan/syncx"]
version = "v0.0.0-20240418144900-b7402ffdebc7" version = "v0.0.0-20240418144900-b7402ffdebc7"
hash = "sha256-L1j1vgiwqXpF9pjMoRRlrQUHzoULisw/01plaEAwxs4=" hash = "sha256-L1j1vgiwqXpF9pjMoRRlrQUHzoULisw/01plaEAwxs4="

View File

@ -3,15 +3,21 @@ package chatcount
import ( import (
"fmt" "fmt"
"image"
"net/http"
"strconv" "strconv"
"strings" "sync"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/imgfactory"
"github.com/FloatTech/rendercard"
ctrl "github.com/FloatTech/zbpctrl" ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/text"
) )
const ( const (
@ -43,22 +49,53 @@ func init() {
}) })
engine.OnFullMatch("查看水群排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true). engine.OnFullMatch("查看水群排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
text := strings.Builder{}
text.WriteString("今日水群排行榜:\n")
chatTimeList := ctdb.getChatRank(ctx.Event.GroupID) chatTimeList := ctdb.getChatRank(ctx.Event.GroupID)
for i := 0; i < len(chatTimeList) && i < rankSize; i++ { if len(chatTimeList) == 0 {
text.WriteString("第") ctx.SendChain(message.Text("ERROR: 没有水群数据"))
text.WriteString(strconv.Itoa(i + 1)) return
text.WriteString("名:") }
text.WriteString(ctx.CardOrNickName(chatTimeList[i].UserID)) rankinfo := make([]*rendercard.RankInfo, len(chatTimeList))
text.WriteString(" - ")
text.WriteString(strconv.FormatInt(chatTimeList[i].TodayMessage, 10)) wg := &sync.WaitGroup{}
text.WriteString("条,共") wg.Add(len(chatTimeList))
text.WriteString(strconv.FormatInt(chatTimeList[i].TodayTime/60, 10)) for i := 0; i < len(chatTimeList) && i < rankSize; i++ {
text.WriteString("分") go func(i int) {
text.WriteString(strconv.FormatInt(chatTimeList[i].TodayTime%60, 10)) defer wg.Done()
text.WriteString("秒\n") 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()))
}) })
} }