birdbot/main.go

113 lines
2.9 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"
2023-06-19 03:51:13 -04:00
"path/filepath"
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
)
2024-03-04 00:42:06 +01:00
const PluginsDirectory = "/home/layla/.config/birdbot/plugins"
2023-03-31 20:21:49 +00:00
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")
flag.StringVar(&db_file, "db", defaultDBPath, "Path to store persistent data")
flag.BoolVar(&version, "v", false, "List version")
2022-10-27 01:55:23 +00:00
flag.Parse()
2023-06-19 03:51:13 -04:00
// Use given config dir
configDir = filepath.Dir(config_file)
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)
}
bot := app.NewBot(db)
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)
}
loader := app.NewComponentLoader(bot, configDir)
2023-03-30 23:51:05 -04:00
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
}
if cfg.Features.RecurringEvents.IsEnabled() {
loader.LoadComponent(modules.NewRecurringEventsComponent(bot.Session))
}
if cfg.Features.StatusPortal.IsEnabled() {
loader.LoadComponent(modules.NewStatusComponent(cfg.StatusPortal.URL))
}
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
}
if cfg.Features.Feedback.IsEnabled() {
loader.LoadComponent(modules.NewFeedbackWebhookComponent(cfg.Feedback.WebhookURL, modules.FeedbackWebhookConfiguration{
PayloadType: cfg.Feedback.PayloadType,
SuccessMessage: cfg.Feedback.SuccessMessage,
FailureMessage: cfg.Feedback.FailureMessage,
2023-06-16 02:08:29 +00: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)
}
}