/
init.go
48 lines (42 loc) · 978 Bytes
/
init.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
package main
import (
"log"
"math/rand"
"time"
"github.com/carlescere/scheduler"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"gopkg.in/redis.v3"
)
var r *redis.Client
func init() {
viper.SetConfigName("conf")
viper.AddConfigPath(".")
viper.WatchConfig()
viper.ReadInConfig()
viper.Unmarshal(&conf)
viper.OnConfigChange(func(e fsnotify.Event) {
if err := viper.Unmarshal(&conf); err != nil {
log.Println(err.Error())
} else {
log.Println("config auto reload!")
}
})
r = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: conf.RedisPass,
})
scheduler.Every().Day().At("00:00").Run(func() {
if time.Now().Day() == 1 {
length := r.ZCard("Shan8Bot:K").Val()
if length < 25 {
return
}
i := rand.Int63n(length-25) + 25
for _, v := range r.ZRangeWithScores("Shan8Bot:K", i, i).Val() {
log.Println("add 100K for ", v.Member)
r.ZIncrBy("Shan8Bot:K", 100, v.Member.(string))
}
}
})
}