stonebot/main.go

71 lines
1.7 KiB
Go

package main
import (
"flag"
"os"
"os/signal"
"syscall"
"git.canopymc.net/Etzelia/stonebot/database"
"git.canopymc.net/Etzelia/stonebot/discord"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/fftoml"
"go.jolheiser.com/beaver"
"go.jolheiser.com/beaver/color"
)
func main() {
fs := flag.NewFlagSet("stonebot", flag.ExitOnError)
fs.String("config", "stonebot.toml", "Path to config file")
verboseFlag := fs.Bool("verbose", false, "Verbose logging")
tokenFlag := fs.String("token", "", "Discord bot token")
prefixFlag := fs.String("prefix", "!", "Discord bot prefix")
dbFlag := fs.String("db", "stonebot.db", "Path to DB")
if err := ff.Parse(fs, os.Args[1:],
ff.WithEnvVarPrefix("STONEBOT"),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(fftoml.Parser),
ff.WithAllowMissingConfigFile(true),
); err != nil {
beaver.Fatal(err)
}
beaver.Console.Format = beaver.FormatOptions{
TimePrefix: true,
StackPrefix: true,
StackLimit: 15,
LevelPrefix: true,
LevelColor: true,
}
color.Fatal = color.Error
if *tokenFlag == "" {
beaver.Fatal("discord token is required")
}
if *verboseFlag {
beaver.Console.Level = beaver.DEBUG
}
db, err := database.New(*dbFlag)
if err != nil {
beaver.Fatalf("could not connect to database: %v", err)
}
defer db.Close()
bot, err := discord.New(*tokenFlag, *prefixFlag, db)
if err != nil {
beaver.Fatalf("could not run bot: %v", err)
}
defer bot.Close()
if err := bot.Open(); err != nil {
beaver.Fatalf("could not open bot connection: %v", err)
}
beaver.Info("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}