@@ -3,15 +3,21 @@ package chatcount
3
3
4
4
import (
5
5
"fmt"
6
+ "image"
7
+ "net/http"
6
8
"strconv"
7
- "strings "
9
+ "sync "
8
10
9
11
zero "github.com/wdvxdr1123/ZeroBot"
10
12
"github.com/wdvxdr1123/ZeroBot/message"
11
13
14
+ "github.com/FloatTech/floatbox/file"
15
+ "github.com/FloatTech/imgfactory"
16
+ "github.com/FloatTech/rendercard"
12
17
ctrl "github.com/FloatTech/zbpctrl"
13
18
"github.com/FloatTech/zbputils/control"
14
19
"github.com/FloatTech/zbputils/ctxext"
20
+ "github.com/FloatTech/zbputils/img/text"
15
21
)
16
22
17
23
const (
@@ -43,22 +49,54 @@ func init() {
43
49
})
44
50
engine .OnFullMatch ("查看水群排名" , zero .OnlyGroup ).Limit (ctxext .LimitByGroup ).SetBlock (true ).
45
51
Handle (func (ctx * zero.Ctx ) {
46
- text := strings.Builder {}
47
- text .WriteString ("今日水群排行榜:\n " )
48
52
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 ))
49
61
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
61
91
}
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
+
63
101
})
64
102
}
0 commit comments