2020-06-09 13:04:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2022-01-19 19:51:12 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2020-06-09 13:04:44 +00:00
|
|
|
|
2022-01-19 19:51:12 +00:00
|
|
|
"git.jojodev.com/Minecraft/canopeas/config"
|
|
|
|
"git.jojodev.com/Minecraft/canopeas/database"
|
|
|
|
"git.jojodev.com/Minecraft/canopeas/discord"
|
2020-06-09 13:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var configFlag string
|
|
|
|
|
|
|
|
func main() {
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
|
2021-05-14 03:27:39 +00:00
|
|
|
flag.StringVar(&configFlag, "config", "canopeas.toml", "Set config path")
|
2020-06-09 13:04:44 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
cfg, err := config.Load(configFlag)
|
|
|
|
if err != nil {
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Fatal().Msgf("could not load config: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db, err := database.Load(cfg.DBPath)
|
|
|
|
if err != nil {
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Fatal().Msgf("could not load database: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bot, err := discord.Bot(cfg, db)
|
|
|
|
if err != nil {
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Fatal().Msgf("could not start Discord bot: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := bot.Open(); err != nil {
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Error().Msgf("error running Discord bot: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Info().Msg("Bot is now running. Press CTRL-C to exit.")
|
2020-06-09 13:04:44 +00:00
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
|
|
<-sc
|
|
|
|
|
|
|
|
if err := bot.Close(); err != nil {
|
2022-01-19 19:51:12 +00:00
|
|
|
log.Error().Msgf("error closing Discord bot: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
}
|