-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (68 loc) · 2.38 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
package main
import (
"os"
"time"
"github.com/go-redis/redis/v8"
"github.com/hellodhlyn/delivery-tracker"
"github.com/kz/discordrus"
log "github.com/sirupsen/logrus"
"github.com/webdonalds/discord-bot/commands"
"github.com/webdonalds/discord-bot/crons"
"github.com/webdonalds/discord-bot/repositories"
)
func getEnvOrDefault(key, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}
func main() {
// Initialize connections
rdb := redis.NewClient(&redis.Options{
Addr: getEnvOrDefault("REDIS_URL", "127.0.0.1:6379"),
Password: getEnvOrDefault("REDIS_PASSWORD", ""),
})
botToken := os.Getenv("DISCORD_BOT_TOKEN")
githubKey := os.Getenv("GITHUB_API_KEY")
bot, err := NewBot(botToken, githubKey)
if err != nil {
panic(err)
}
trackClient, _ := deliverytracker.NewClient()
// Initialize repositories
dtRepo, _ := repositories.NewRedisDeliveryTrackRepository(rdb)
molluRepo, _ := repositories.NewRedisMolluRepository(rdb)
// Register commands and crons
bot.AddCommand(commands.NewPingCommand())
bot.AddCommand(commands.NewDeliveryCommand(dtRepo, trackClient))
bot.AddCommand(commands.NewHelpCommand())
bot.AddCommand(commands.NewTimerCommand())
bot.AddCommand(commands.NewExchangeCommand())
bot.AddCommand(commands.NewRandomCommand())
bot.AddCommand(commands.NewAirQualityCommand())
bot.AddCommand(commands.NewStockCommand())
bot.AddCommand(commands.NewMolluCommand(molluRepo))
bot.AddCron(crons.NewBreakingNewsCron())
bot.AddCron(crons.NewDeliveryTrackCron(dtRepo, trackClient))
bot.AddCron(crons.NewMolluCron(molluRepo))
err = bot.Listen()
if err != nil {
panic(err)
}
}
func init() {
log.SetFormatter(&log.JSONFormatter{})
loc, _ := time.LoadLocation("Asia/Seoul")
log.AddHook(discordrus.NewHook(
// Use environment variable for security reasons
os.Getenv("ERROR_LOG_WEBHOOK_URL"),
// Set minimum level to DebugLevel to receive all log entries
log.WarnLevel,
&discordrus.Opts{
Username: "ErrorBot",
DisableTimestamp: false, // Setting this to true will disable timestamps from appearing in the footer
TimestampFormat: "Jan 2 15:04:05.00000 MST", // The timestamp takes this format; if it is unset, it will take log' default format
TimestampLocale: loc, // The timestamp uses this locale; if it is unset, it will use time.Local
},
))
}