MCPcopy Create free account
hub / github.com/FloatTech/ZeroBot-Plugin / updateChatTime

Method updateChatTime

plugin/chatcount/model.go:86–138  ·  view source on GitHub ↗

updateChatTime 更新发言时间,todayTime的单位是分钟

(gid, uid int64)

Source from the content-addressed store, hash-verified

84
85// updateChatTime 更新发言时间,todayTime的单位是分钟
86func (ctdb *chattimedb) updateChatTime(gid, uid int64) (remindTime int64, remindFlag bool) {
87 ctdb.chatmu.Lock()
88 defer ctdb.chatmu.Unlock()
89 db := ctdb.db
90 now := time.Now()
91 keyword := fmt.Sprintf("%v_%v", gid, uid)
92 ts, ok := ctdb.userTimestampMap.Load(keyword)
93 if !ok {
94 ctdb.userTimestampMap.Store(keyword, now.Unix())
95 ctdb.userTodayMessageMap.Store(keyword, 1)
96 return
97 }
98 lastTime := time.Unix(ts, 0)
99 todayTime, _ := ctdb.userTodayTimeMap.Load(keyword)
100 totayMessage, _ := ctdb.userTodayMessageMap.Load(keyword)
101 // 这个消息数是必须统计的
102 ctdb.userTodayMessageMap.Store(keyword, totayMessage+1)
103 st := chatTime{
104 GroupID: gid,
105 UserID: uid,
106 TotalTime: todayTime,
107 TotalMessage: totayMessage,
108 }
109
110 // 如果不是同一天,把TotalTime,TotalMessage重置
111 if lastTime.YearDay() != now.YearDay() {
112 if err := db.Model(&st).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
113 if gorm.IsRecordNotFoundError(err) {
114 db.Model(&st).Create(&st)
115 }
116 } else {
117 db.Model(&st).Where("group_id = ? and user_id = ?", gid, uid).Update(
118 map[string]any{
119 "total_time": st.TotalTime + todayTime,
120 "total_message": st.TotalMessage + totayMessage,
121 })
122 }
123 ctdb.userTimestampMap.Store(keyword, now.Unix())
124 ctdb.userTodayTimeMap.Delete(keyword)
125 ctdb.userTodayMessageMap.Delete(keyword)
126 return
127 }
128
129 userChatTime := int64(now.Sub(lastTime).Seconds())
130 // 当聊天时间在一定范围内的话,则计入时长
131 if userChatTime < chatInterval {
132 ctdb.userTodayTimeMap.Store(keyword, todayTime+userChatTime)
133 remindTime = (todayTime + userChatTime) / 60
134 remindFlag = l.level(int((todayTime+userChatTime)/60)) > l.level(int(todayTime/60))
135 }
136 ctdb.userTimestampMap.Store(keyword, now.Unix())
137 return
138}
139
140// getChatTime 获得用户聊天时长和消息次数,todayTime,totalTime的单位是秒,todayMessage,totalMessage单位是条数
141func (ctdb *chattimedb) getChatTime(gid, uid int64) (todayTime, todayMessage, totalTime, totalMessage int64) {

Callers 1

initFunction · 0.80

Calls 1

levelMethod · 0.80

Tested by

no test coverage detected