pepster/main.go

74 lines
1.5 KiB
Go
Raw Permalink Normal View History

2021-07-19 22:06:54 +00:00
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"pepster/models"
)
func main() {
var err error
// open config file
configPath := flag.String("config", "config.toml", "Path to the config file (defaults to config.toml)")
flag.Parse()
var config Config
configData, err := ioutil.ReadFile(*configPath)
if err != nil {
log.Fatal("Failed to read config file", err)
}
if err = toml.Unmarshal(configData, &config); err != nil {
log.Fatal("Invalid toml in config file", err)
}
// open database
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// migrate the schema
models.AutoMigrate(db)
// connect to discord
discord, err := discordgo.New("Bot " + config.BotToken)
if err != nil {
log.Fatal("Could not connect to discord", err)
}
if err = discord.Open(); err != nil {
log.Fatal("Could not connect to discord", err)
}
pepster := &Pepster{
config: &config,
db: db,
discord: discord,
updateStatus: make(chan bool, 5),
}
pepster.Init()
pepster.updateStatus <- true
defer pepster.Close()
// registering command handlers
pepster.registerCommands(pepster.commandsList(), false)
// graceful shutdown handler
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutting down...")
// deferred close
}