forked from Southclaws/cj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_main.go
105 lines (95 loc) · 3.35 KB
/
bot_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
package main
import (
"os"
"strconv"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.Logger
func init() {
var config zap.Config
debug := os.Getenv("DEBUG")
if os.Getenv("TESTING") != "" {
config = zap.NewDevelopmentConfig()
config.DisableCaller = true
} else {
config = zap.NewProductionConfig()
config.EncoderConfig.MessageKey = "@message"
config.EncoderConfig.TimeKey = "@timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
if debug != "0" && debug != "" {
dyn := zap.NewAtomicLevel()
dyn.SetLevel(zap.DebugLevel)
config.Level = dyn
}
}
_logger, err := config.Build()
if err != nil {
panic(err)
}
logger = _logger.With(
zap.String("@version", os.Getenv("GIT_HASH")),
zap.Namespace("@fields"),
)
}
// Config stores configuration variables
type Config struct {
MongoHost string
MongoPort string
MongoName string
MongoUser string
MongoPass string
DiscordToken string // discord API token
AdministrativeChannel string // administrative channel where someone can speak as bot
PrimaryChannel string // main channel the bot hangs out in
Heartbeat int // Heartbeat time in minutes, a heartbeat is when the bot chimes in to the server, sometimes with a random message
BotID string // the bot's client ID
GuildID string // the discord channel ID
VerifiedRole string // ID of the role for verified members
NormalRole string // role assigned to all users automatically
DebugUser string // when set, only accept commands from this user
Admin string // user who has control over the bot
LanguageData string // `lang` data directory location, defaults to `./lang`
}
func main() {
Start(Config{
MongoHost: configStrFromEnv("MONGO_HOST"),
MongoPort: configStrFromEnv("MONGO_PORT"),
MongoName: configStrFromEnv("MONGO_NAME"),
MongoUser: configStrFromEnv("MONGO_USER"),
MongoPass: os.Getenv("MONGO_PASS"),
DiscordToken: configStrFromEnv("DISCORD_TOKEN"),
AdministrativeChannel: configStrFromEnv("ADMINISTRATIVE_CHANNEL"),
PrimaryChannel: configStrFromEnv("PRIMARY_CHANNEL"),
Heartbeat: configIntFromEnv("HEARTBEAT"),
BotID: configStrFromEnv("BOT_ID"),
GuildID: configStrFromEnv("GUILD_ID"),
VerifiedRole: configStrFromEnv("VERIFIED_ROLE"),
NormalRole: configStrFromEnv("NORMAL_ROLE"),
DebugUser: os.Getenv("DEBUG_USER"),
Admin: configStrFromEnv("ADMIN"),
LanguageData: configStrFromEnv("LANGUAGE_DATA"),
})
}
func configStrFromEnv(name string) (value string) {
value = os.Getenv(name)
if value == "" {
logger.Fatal("environment variable not set",
zap.String("name", name))
}
return
}
func configIntFromEnv(name string) (value int) {
valueStr := os.Getenv(name)
if valueStr == "" {
logger.Fatal("environment variable not set",
zap.String("name", name))
}
value, err := strconv.Atoi(valueStr)
if err != nil {
logger.Fatal("failed to convert environment variable to int",
zap.Error(err),
zap.String("name", name))
}
return
}