birdbot/main.go

113 lines
2.9 KiB
Go
Raw Normal View History

2022-10-27 03:55:23 +02:00
package main
import (
2023-03-31 05:51:05 +02:00
"errors"
2022-10-27 03:55:23 +02:00
"flag"
"fmt"
2022-10-27 03:55:23 +02:00
"log"
"os"
"path"
2023-06-19 09:51:13 +02:00
"path/filepath"
2022-10-27 03:55:23 +02:00
2023-03-31 05:51:05 +02:00
"github.com/ilyakaznacheev/cleanenv"
2022-10-27 05:57:34 +02:00
"github.com/yeslayla/birdbot/app"
2023-03-31 05:51:05 +02:00
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/modules"
"github.com/yeslayla/birdbot/persistence"
2022-10-27 03:55:23 +02:00
)
2024-03-09 13:44:35 +01:00
const PluginsDirectory = "./plugins"
2023-03-31 22:21:49 +02:00
2022-10-27 03:55:23 +02:00
func main() {
configDir, _ := os.UserConfigDir()
defaultConfigPath := path.Join(configDir, "birdbot", "config.yaml")
2023-04-02 00:25:54 +02:00
defaultDBPath := path.Join(configDir, "birdbot", "birdbot.db")
2022-10-27 03:55:23 +02:00
var config_file string
2023-04-02 00:25:54 +02: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 03:55:23 +02:00
flag.Parse()
2023-06-19 09:51:13 +02: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-31 05:51:05 +02: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-02 00:25:54 +02: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-31 05:51:05 +02:00
if err := bot.Initialize(cfg); err != nil {
2022-10-27 03:55:23 +02:00
log.Fatal("Failed to initialize: ", err)
}
loader := app.NewComponentLoader(bot, configDir)
2023-03-31 05:51:05 +02:00
2023-03-31 22:21:49 +02:00
if cfg.Features.AnnounceEvents.IsEnabledByDefault() {
loader.LoadComponent(modules.NewAnnounceEventsComponent(bot.Mastodon, cfg.Discord.NotificationChannel))
2023-03-31 05:51:05 +02:00
}
2023-03-31 22:21:49 +02:00
if cfg.Features.ManageEventChannels.IsEnabledByDefault() {
loader.LoadComponent(modules.NewManageEventChannelsComponent(cfg.Discord.EventCategory, cfg.Discord.ArchiveCategory, bot.Session))
2023-03-31 05:51:05 +02: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-31 05:51:05 +02: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 04:08:29 +02:00
}))
}
2023-03-31 22:21:49 +02:00
if _, err := os.Stat(PluginsDirectory); !os.IsNotExist(err) {
2023-03-31 22:49:50 +02:00
components := app.LoadPlugins(PluginsDirectory)
2023-03-31 22:21:49 +02:00
for _, comp := range components {
loader.LoadComponent(comp)
}
}
2022-10-27 03:55:23 +02:00
if err := bot.Run(); err != nil {
log.Fatal(err)
}
}