()
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
| 29 | DisableOnDefault: false, |
| 30 | Brief: "聊天时长统计", |
| 31 | Help: "- 查询水群@xxx\n- 查看水群排名", |
| 32 | PrivateDataFolder: "chatcount", |
| 33 | }) |
| 34 | go func() { |
| 35 | ctdb = initialize(engine.DataFolder() + "chatcount.db") |
| 36 | }() |
| 37 | engine.OnMessage(zero.OnlyGroup).SetBlock(false). |
| 38 | Handle(func(ctx *zero.Ctx) { |
| 39 | remindTime, remindFlag := ctdb.updateChatTime(ctx.Event.GroupID, ctx.Event.UserID) |
| 40 | if remindFlag { |
| 41 | ctx.SendChain(message.At(ctx.Event.UserID), message.Text(fmt.Sprintf("BOT提醒:你今天已经水群%d分钟了!", remindTime))) |
| 42 | } |
| 43 | }) |
| 44 | |
| 45 | engine.OnPrefix(`查询水群`, zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { |
| 46 | param := ctx.State["args"].(string) |
| 47 | var uid int64 |
| 48 | if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" { |
| 49 | uid, _ = strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64) |
| 50 | } else if param == "" { |
| 51 | uid = ctx.Event.UserID |
| 52 | } |
| 53 | name := ctx.NickName() |
| 54 | todayTime, todayMessage, totalTime, totalMessage := ctdb.getChatTime(ctx.Event.GroupID, uid) |
| 55 | ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("%s今天水了%d分%d秒,发了%d条消息;总计水了%d分%d秒,发了%d条消息。", name, todayTime/60, todayTime%60, todayMessage, totalTime/60, totalTime%60, totalMessage))) |
| 56 | }) |
| 57 | engine.OnFullMatch("查看水群排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true). |
| 58 | Handle(func(ctx *zero.Ctx) { |
| 59 | chatTimeList := ctdb.getChatRank(ctx.Event.GroupID) |
| 60 | if len(chatTimeList) == 0 { |
| 61 | ctx.SendChain(message.Text("ERROR: 没有水群数据")) |
| 62 | return |
| 63 | } |
| 64 | rankinfo := make([]*rendercard.RankInfo, len(chatTimeList)) |
| 65 | |
| 66 | wg := &sync.WaitGroup{} |
| 67 | wg.Add(len(chatTimeList)) |
| 68 | for i := 0; i < len(chatTimeList) && i < rankSize; i++ { |
| 69 | go func(i int) { |
| 70 | defer wg.Done() |
| 71 | resp, err := http.Get("https://q4.qlogo.cn/g?b=qq&nk=" + strconv.FormatInt(chatTimeList[i].UserID, 10) + "&s=100") |
| 72 | if err != nil { |
| 73 | return |
| 74 | } |
| 75 | defer resp.Body.Close() |
| 76 | img, _, err := image.Decode(resp.Body) |
| 77 | if err != nil { |
| 78 | return |
| 79 | } |
| 80 | rankinfo[i] = &rendercard.RankInfo{ |
| 81 | TopLeftText: ctx.CardOrNickName(chatTimeList[i].UserID), |
| 82 | BottomLeftText: "消息数: " + strconv.FormatInt(chatTimeList[i].TodayMessage, 10) + " 条", |
| 83 | RightText: strconv.FormatInt(chatTimeList[i].TodayTime/60, 10) + "分" + strconv.FormatInt(chatTimeList[i].TodayTime%60, 10) + "秒", |
| 84 | Avatar: img, |
nothing calls this directly
no test coverage detected