69 lines
1.1 KiB
Go
69 lines
1.1 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"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/settings"
|
|
)
|
|
|
|
type App struct {
|
|
bot *bot
|
|
ctx context.Context
|
|
db *sqlx.DB
|
|
fs vfs.VFS
|
|
logger *logging.Log
|
|
settings *settings.Settings
|
|
|
|
cron *cron.Cron
|
|
}
|
|
|
|
func NewApp(root vfs.VFS, conf *settings.Settings) *App {
|
|
return &App{
|
|
fs: root,
|
|
logger: logging.Logger(),
|
|
settings: conf,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (a *App) Start(ctx context.Context, cmd *cli.Command) error {
|
|
if err := a.loadEnv(); err != nil {
|
|
return err
|
|
}
|
|
|
|
bot := NewBot(ctx, a.settings)
|
|
bot.Run()
|
|
|
|
return nil
|
|
}
|
|
|
|
type chats struct {
|
|
}
|