-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
150 lines (135 loc) · 3.29 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/patrickmn/go-cache"
"github.com/robfig/cron/v3"
"github.com/servusdei2018/shards"
"github.com/spf13/viper"
)
type config struct {
BotToken string `mapstructure:"bot-token"`
ShardCount int `mapstructure:"shard-count"`
OwnerChannelID string `mapstructure:"owner-channel-id"`
Author struct {
Name string `mapstructure:"name"`
URL string `mapstructure:"url"`
Icon string `mapstructure:"icon"`
} `mapstructure:"author"`
BroadcastCron string `mapstructure:"broadcast-cron"`
}
var cfg config
var ca *cache.Cache
var loc *time.Location
var Mgr *shards.Manager
func init() {
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName("config")
ca = cache.New(30*time.Minute, 60*time.Minute)
err := viper.ReadInConfig()
if err != nil {
log.Panic(err)
}
err = viper.Unmarshal(&cfg)
if err != nil {
log.Panic(err)
}
loc, err = time.LoadLocation("Asia/Bangkok")
if err != nil {
log.Panic(err)
}
initDb()
//initCache()
checkContent, err := os.ReadFile("covid19_check_result.json")
if err != nil {
log.Panic(err)
}
provinceContent, err := os.ReadFile("provinces.json")
if err != nil {
log.Panic(err)
}
var provincesData []province
err = json.Unmarshal(provinceContent, &provincesData)
if err != nil {
log.Panic(err)
}
provinces = make(map[string]string)
//most used province
provinces["bkk"] = "bangkok"
provinces["กรุงเทพ"] = "bangkok"
provinces["กทม"] = "bangkok"
for _, v := range provincesData {
provinces[v.Slug] = v.Slug
provinces[v.Title] = v.Slug
}
err = json.Unmarshal(checkContent, &checkResults)
if err != nil {
log.Panic(err)
}
}
func main() {
var err error
Mgr, err = shards.New("Bot " + cfg.BotToken)
if err != nil {
fmt.Println("[ERROR] Error creating manager,", err)
return
}
Mgr.AddHandler(messageCreate)
Mgr.AddHandler(checkReactionAdd)
Mgr.AddHandler(checkReactionRemove)
Mgr.RegisterIntent(discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentsDirectMessageReactions | discordgo.IntentsGuildMessageReactions)
err = Mgr.Start()
if err != nil {
fmt.Println("[ERROR] Error starting manager,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
//broadcastSubs()
c := cron.New()
c.AddFunc(fmt.Sprintf("CRON_TZ=%s", cfg.BroadcastCron), broadcast)
c.Start()
/*
now := time.Now().In(loc)
noon := time.Date(now.Year(), now.Month(), now.Day(), 11, 59, 59, 0, loc)
if now.After(noon) {
broadcasted, err := getTodayBroadcastStatus()
if err != nil {
fmt.Printf("Error getting today broadcast status, skipping.\n")
}
if !broadcasted {
broadcast()
}
}*/
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
c.Stop()
db.Close()
fmt.Println("[INFO] Stopping shard manager...")
Mgr.Shutdown()
}
func broadcast() {
retryCount := 0
for {
err := broadcastSubs()
if err != nil {
fmt.Printf("Error cron %s\n", err.Error())
retryCount++
if retryCount > 5 {
break
}
time.Sleep(5 * time.Minute)
continue
}
break
}
}