Skip to content

Commit 54110f7

Browse files
committed
feat(chatcount): rank use image
1 parent 26d2074 commit 54110f7

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/FloatTech/sqlite v1.6.3
1313
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562
1414
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb
15-
github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c
15+
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8
1616
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
1717
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
1818
github.com/antchfx/htmlquery v1.3.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562 h1:snfw7FNFym1eNnLrQ
1717
github.com/FloatTech/ttl v0.0.0-20240716161252-965925764562/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
1818
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb h1:sGqwCiMDyUD/znWEVVRVxbd6Kg1KLgGnnIuq5bCUWaQ=
1919
github.com/FloatTech/zbpctrl v1.6.2-0.20240904160347-1317e11a15bb/go.mod h1:I+MetM++1sJhNPg3zww1aw04BicYsNohvHC4Jh52XSo=
20-
github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c h1:hFiqx4uk6+lc2zHAaQ3JkkI2KH59c6O4yHKWKXFzxLs=
21-
github.com/FloatTech/zbputils v1.7.2-0.20240822065525-5ea6811ed91c/go.mod h1:MwTFLPhlP0qMMLcq4x90oiu1IVE1T5dN0ZsxyTGSf6k=
20+
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8 h1:XlOrX7WJqIQVBziB+ZXXzp/OG0YQICO5IC6T9s4iohE=
21+
github.com/FloatTech/zbputils v1.7.2-0.20240911161040-1d89a7b10ff8/go.mod h1:2ZS6vZHn/imRIOhlDytd/FPVdBIY5KYdTPTIwBJU9Js=
2222
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
2323
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
2424
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=

gomod2nix.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ schema = 3
2929
version = "v1.6.2-0.20240904160347-1317e11a15bb"
3030
hash = "sha256-x0ZR2bnkboEIjjRFMtMAN0T34BP9BPs7r3AwT3BCyzo="
3131
[mod."github.com/FloatTech/zbputils"]
32-
version = "v1.7.2-0.20240822065525-5ea6811ed91c"
33-
hash = "sha256-ouAExps1iPCcD1AmOxyhRXMBGHBDXvUGkplcnQCf3Bg="
32+
version = "v1.7.2-0.20240911161040-1d89a7b10ff8"
33+
hash = "sha256-3j7LE5Uqx5kPmi6c+WJEBFtLK0JGU4UGX7RpVuAjmf8="
3434
[mod."github.com/RomiChan/syncx"]
3535
version = "v0.0.0-20240418144900-b7402ffdebc7"
3636
hash = "sha256-L1j1vgiwqXpF9pjMoRRlrQUHzoULisw/01plaEAwxs4="

plugin/chatcount/chatcount.go

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ package chatcount
33

44
import (
55
"fmt"
6+
"image"
7+
"net/http"
68
"strconv"
7-
"strings"
9+
"sync"
810

911
zero "github.com/wdvxdr1123/ZeroBot"
1012
"github.com/wdvxdr1123/ZeroBot/message"
1113

14+
"github.com/FloatTech/floatbox/file"
15+
"github.com/FloatTech/imgfactory"
16+
"github.com/FloatTech/rendercard"
1217
ctrl "github.com/FloatTech/zbpctrl"
1318
"github.com/FloatTech/zbputils/control"
1419
"github.com/FloatTech/zbputils/ctxext"
20+
"github.com/FloatTech/zbputils/img/text"
1521
)
1622

1723
const (
@@ -43,22 +49,54 @@ func init() {
4349
})
4450
engine.OnFullMatch("查看水群排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
4551
Handle(func(ctx *zero.Ctx) {
46-
text := strings.Builder{}
47-
text.WriteString("今日水群排行榜:\n")
4852
chatTimeList := ctdb.getChatRank(ctx.Event.GroupID)
53+
if len(chatTimeList) == 0 {
54+
ctx.SendChain(message.Text("ERROR: 没有水群数据"))
55+
return
56+
}
57+
rankinfo := make([]*rendercard.RankInfo, len(chatTimeList))
58+
59+
wg := &sync.WaitGroup{}
60+
wg.Add(len(chatTimeList))
4961
for i := 0; i < len(chatTimeList) && i < rankSize; i++ {
50-
text.WriteString("第")
51-
text.WriteString(strconv.Itoa(i + 1))
52-
text.WriteString("名:")
53-
text.WriteString(ctx.CardOrNickName(chatTimeList[i].UserID))
54-
text.WriteString(" - ")
55-
text.WriteString(strconv.FormatInt(chatTimeList[i].TodayMessage, 10))
56-
text.WriteString("条,共")
57-
text.WriteString(strconv.FormatInt(chatTimeList[i].TodayTime/60, 10))
58-
text.WriteString("分")
59-
text.WriteString(strconv.FormatInt(chatTimeList[i].TodayTime%60, 10))
60-
text.WriteString("秒\n")
62+
go func(i int) {
63+
defer wg.Done()
64+
resp, err := http.Get("https://q4.qlogo.cn/g?b=qq&nk=" + strconv.FormatInt(chatTimeList[i].UserID, 10) + "&s=100")
65+
if err != nil {
66+
return
67+
}
68+
defer resp.Body.Close()
69+
img, _, err := image.Decode(resp.Body)
70+
if err != nil {
71+
return
72+
}
73+
rankinfo[i] = &rendercard.RankInfo{
74+
TopLeftText: ctx.CardOrNickName(chatTimeList[i].UserID),
75+
BottomLeftText: "消息数: " + strconv.FormatInt(chatTimeList[i].TodayMessage, 10) + " 条",
76+
RightText: strconv.FormatInt(chatTimeList[i].TodayTime/60, 10) + "分" + strconv.FormatInt(chatTimeList[i].TodayTime%60, 10) + "秒",
77+
Avatar: img,
78+
}
79+
}(i)
80+
}
81+
wg.Wait()
82+
fontbyte, err := file.GetLazyData(text.GlowSansFontFile, control.Md5File, true)
83+
if err != nil {
84+
ctx.SendChain(message.Text("ERROR: ", err))
85+
return
86+
}
87+
img, err := rendercard.DrawRankingCard(fontbyte, "今日水群排行榜", rankinfo)
88+
if err != nil {
89+
ctx.SendChain(message.Text("ERROR: ", err))
90+
return
6191
}
62-
ctx.SendChain(message.Text(text.String()))
92+
sendimg, err := imgfactory.ToBytes(img)
93+
if err != nil {
94+
ctx.SendChain(message.Text("ERROR: ", err))
95+
return
96+
}
97+
if id := ctx.SendChain(message.ImageBytes(sendimg)); id.ID() == 0 {
98+
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
99+
}
100+
63101
})
64102
}

0 commit comments

Comments
 (0)