This repository has been archived by the owner on Feb 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
prepare.go
66 lines (57 loc) · 1.55 KB
/
prepare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package plugin
import (
"strconv"
"time"
"github.com/jqs7/Jqs7Bot/conf"
"github.com/jqs7/bb"
)
type Prepare struct{ bb.Base }
func (p *Prepare) Run() {
p.autoRule()
p.analytics()
if time.Since(p.Message.Time()) > time.Hour {
panic("out dated message")
}
}
func (p *Prepare) autoRule() {
if p.Message.NewChatParticipant.ID != 0 {
rc := conf.Redis
chatIDStr := strconv.Itoa(p.ChatID)
if rc.Exists("tgGroupAutoRule:" + chatIDStr).Val() {
p.NewMessage(p.Message.NewChatParticipant.ID,
"欢迎加入 "+p.Message.Chat.Title+"\n 以下是群组规则:").Send()
if rc.Exists("tgGroupRule:" + chatIDStr).Val() {
p.NewMessage(
p.Message.NewChatParticipant.ID,
rc.Get("tgGroupRule:"+chatIDStr).Val(),
).Send()
}
}
}
}
func (p *Prepare) analytics() {
rc := conf.Redis
day, month := true, false
key := func(getDay bool) string {
return "tgAnalytics:" + GetDate(getDay, 0)
}
totalKey := func(getDay bool) string {
return "tgTotalAnalytics:" + GetDate(getDay, 0)
}
rc.HSet("tgUsersID", strconv.Itoa(p.Message.From.ID),
FromUserName(p.Message.From))
rc.HSet("tgUsersName", FromUserName(p.Message.From),
strconv.Itoa(p.Message.From.ID))
switch {
case rc.TTL(key(day)).Val() < 0:
rc.Expire(key(day), time.Hour*(24*3+3))
case rc.TTL(key(month)).Val() < 0:
rc.Expire(key(month), time.Hour*(24*30*3+3))
}
if p.FromGroup || p.FromSuperGroup {
rc.Incr(totalKey(day))
rc.ZIncrBy(key(day), 1, strconv.Itoa(p.Message.From.ID))
rc.Incr(totalKey(month))
rc.ZIncrBy(key(month), 1, strconv.Itoa(p.Message.From.ID))
}
}