98 lines
1.6 KiB
Go
98 lines
1.6 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/joho/godotenv"
|
|
"github.com/robfig/cron/v3"
|
|
"github.com/urfave/cli/v3"
|
|
"go.destrealm.net/logging/v3"
|
|
"go.destrealm.net/vfs"
|
|
|
|
"code.destrealm.net/zancarius/justusbot/internal/models"
|
|
"code.destrealm.net/zancarius/justusbot/internal/settings"
|
|
)
|
|
|
|
type App struct {
|
|
bot *bot
|
|
ctx context.Context
|
|
db *models.Database
|
|
fs vfs.VFS
|
|
logger *logging.Log
|
|
settings *settings.Settings
|
|
|
|
cron *cron.Cron
|
|
}
|
|
|
|
func NewApp(ctx context.Context, root vfs.VFS, conf *settings.Settings) (*App, error) {
|
|
app := &App{
|
|
ctx: ctx,
|
|
db: models.OpenDatabase(ctx, conf),
|
|
fs: root,
|
|
logger: logging.Logger(),
|
|
settings: conf,
|
|
}
|
|
|
|
if err := app.db.Open(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return app, nil
|
|
}
|
|
|
|
func (a *App) loadCron() error {
|
|
a.cron = cron.New()
|
|
a.cron.AddFunc(fmt.Sprintf("TZ=%s %s",
|
|
a.settings.System.TimeZone,
|
|
a.settings.DailyScripture.Schedule), a.scripture)
|
|
|
|
a.cron.Start()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) loadEnv() error {
|
|
// Load .env from VFS.
|
|
fp, err := a.fs.Open(".env")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buf, err := io.ReadAll(fp)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env, err := godotenv.UnmarshalBytes(buf)
|
|
for k, v := range env {
|
|
switch k {
|
|
case "TOKEN":
|
|
a.settings.Service.BotToken = v
|
|
}
|
|
}
|
|
|
|
//if chats, err := a.db.
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) scripture() {
|
|
|
|
}
|
|
|
|
func (a *App) Start(ctx context.Context, cmd *cli.Command) error {
|
|
if err := a.loadEnv(); err != nil {
|
|
return err
|
|
}
|
|
if err := a.loadCron(); err != nil {
|
|
return err
|
|
}
|
|
|
|
bot := NewBot(ctx, a.settings)
|
|
bot.Run()
|
|
|
|
return nil
|
|
}
|
|
|
|
type chats struct {
|
|
}
|