birdbot/main.go

97 lines
2.4 KiB
Go
Raw Normal View History

2022-10-27 01:55:23 +00:00
package main
import (
2023-03-30 23:51:05 -04:00
"errors"
2022-10-27 01:55:23 +00:00
"flag"
"fmt"
2022-10-27 01:55:23 +00:00
"log"
"os"
"path"
2022-10-27 01:55:23 +00:00
2023-03-30 23:51:05 -04:00
"github.com/ilyakaznacheev/cleanenv"
2022-10-27 03:57:34 +00:00
"github.com/yeslayla/birdbot/app"
2023-03-30 23:51:05 -04:00
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/modules"
"github.com/yeslayla/birdbot/persistence"
2022-10-27 01:55:23 +00:00
)
2023-03-31 20:21:49 +00:00
const PluginsDirectory = "./plugins"
2022-10-27 01:55:23 +00:00
func main() {
configDir, _ := os.UserConfigDir()
defaultConfigPath := path.Join(configDir, "birdbot", "config.yaml")
2023-04-01 22:25:54 +00:00
defaultDBPath := path.Join(configDir, "birdbot", "birdbot.db")
2022-10-27 01:55:23 +00:00
var config_file string
2023-04-01 22:25:54 +00:00
var db_file string
var version bool
flag.StringVar(&config_file, "c", defaultConfigPath, "Path to config file")
2023-04-01 22:25:54 +00:00
flag.StringVar(&db_file, "db", defaultDBPath, "Path to store persistant data")
flag.BoolVar(&version, "v", false, "List version")
2022-10-27 01:55:23 +00:00
flag.Parse()
if version {
fmt.Printf("BirdBot %s (%s)\n", app.Version, app.Build)
return
}
2023-03-30 23:51:05 -04:00
log.Printf("Using config: %s", config_file)
cfg := &core.Config{}
_, err := os.Stat(config_file)
if errors.Is(err, os.ErrNotExist) {
log.Printf("Config file not found: '%s'", config_file)
err := cleanenv.ReadEnv(cfg)
if err != nil {
log.Fatal(err)
}
} else {
err := cleanenv.ReadConfig(config_file, cfg)
if err != nil {
log.Fatal(err)
}
}
2023-04-01 22:25:54 +00:00
db := persistence.NewSqlite3Database(db_file)
if err := db.MigrateUp(); err != nil {
log.Fatal("Failed to migrate db: ", err)
}
2022-10-27 01:55:23 +00:00
bot := app.NewBot()
2023-03-30 23:51:05 -04:00
if err := bot.Initialize(cfg); err != nil {
2022-10-27 01:55:23 +00:00
log.Fatal("Failed to initialize: ", err)
}
2023-03-30 23:51:05 -04:00
loader := app.NewComponentLoader(bot)
2023-03-31 20:21:49 +00:00
if cfg.Features.AnnounceEvents.IsEnabledByDefault() {
loader.LoadComponent(modules.NewAnnounceEventsComponent(bot.Mastodon, cfg.Discord.NotificationChannel))
2023-03-30 23:51:05 -04:00
}
2023-03-31 20:21:49 +00:00
if cfg.Features.ManageEventChannels.IsEnabledByDefault() {
loader.LoadComponent(modules.NewManageEventChannelsComponent(cfg.Discord.EventCategory, cfg.Discord.ArchiveCategory, bot.Session))
2023-03-30 23:51:05 -04:00
}
2023-03-31 20:21:49 +00:00
if cfg.Features.ReccurringEvents.IsEnabledByDefault() {
loader.LoadComponent(modules.NewRecurringEventsComponent())
}
if cfg.Features.RoleSelection.IsEnabledByDefault() {
for _, v := range cfg.Discord.RoleSelections {
loader.LoadComponent(modules.NewRoleSelectionComponent(bot.Session, db, v))
}
2023-03-30 23:51:05 -04:00
}
2023-03-31 20:21:49 +00:00
if _, err := os.Stat(PluginsDirectory); !os.IsNotExist(err) {
2023-03-31 20:49:50 +00:00
components := app.LoadPlugins(PluginsDirectory)
2023-03-31 20:21:49 +00:00
for _, comp := range components {
loader.LoadComponent(comp)
}
}
2022-10-27 01:55:23 +00:00
if err := bot.Run(); err != nil {
log.Fatal(err)
}
}