MCPcopy Index your code
hub / github.com/FloatTech/ZeroBot-Plugin / init

Function init

plugin/score/sign_in.go:56–295  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

54)
55
56func init() {
57 cachePath := engine.DataFolder() + "cache/"
58 go func() {
59 sdb = initialize(engine.DataFolder() + "score.db")
60 ok := file.IsExist(cachePath)
61 if !ok {
62 err := os.MkdirAll(cachePath, 0777)
63 if err != nil {
64 panic(err)
65 }
66 return
67 }
68 files, err := os.ReadDir(cachePath)
69 if err == nil {
70 for _, f := range files {
71 if !strings.Contains(f.Name(), time.Now().Format("20060102")) {
72 _ = os.Remove(cachePath + f.Name())
73 }
74 }
75 }
76 }()
77 engine.OnRegex(`^签到\s?(\d*)$`).Limit(ctxext.LimitByUser).SetBlock(true).Handle(func(ctx *zero.Ctx) {
78 // 选择key
79 key := ctx.State["regex_matched"].([]string)[1]
80 gid := ctx.Event.GroupID
81 if gid < 0 {
82 // 个人用户设为负数
83 gid = -ctx.Event.UserID
84 }
85 k := uint8(0)
86 if key == "" {
87 k = uint8(ctx.State["manager"].(*ctrl.Control[*zero.Ctx]).GetData(gid))
88 } else {
89 kn, err := strconv.Atoi(key)
90 if err != nil {
91 ctx.SendChain(message.Text("ERROR: ", err))
92 return
93 }
94 k = uint8(kn)
95 }
96 if int(k) >= len(styles) {
97 ctx.SendChain(message.Text("ERROR: 未找到签到设定: ", key))
98 return
99 }
100 uid := ctx.Event.UserID
101 today := time.Now().Format("20060102")
102 // 签到图片
103 drawedFile := cachePath + strconv.FormatInt(uid, 10) + today + "signin.png"
104 picFile := cachePath + strconv.FormatInt(uid, 10) + today + ".png"
105 // 获取签到时间
106 si := sdb.GetSignInByUID(uid)
107 siUpdateTimeStr := si.UpdatedAt.Format("20060102")
108 switch {
109 case si.Count >= signinMax && siUpdateTimeStr == today:
110 // 如果签到时间是今天
111 ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("今天你已经签到过了!"))
112 if file.IsExist(drawedFile) {
113 trySendImage(drawedFile, ctx)

Callers

nothing calls this directly

Calls 10

trySendImageFunction · 0.85
getrankFunction · 0.85
FormatMethod · 0.80
GetSignInByUIDMethod · 0.80
GetScoreByUIDMethod · 0.80
GetScoreRankByTopNMethod · 0.80
initializeFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected