justusbot/internal/app/app.go

164 lines
3.3 KiB
Go
Raw Normal View History

package app
import (
2026-10-05 15:43:35 -06:00
"context"
2026-10-20 15:17:16 -06:00
"fmt"
2026-10-16 00:15:03 -06:00
"io"
"strings"
2026-10-05 15:43:35 -06:00
2026-10-16 00:15:03 -06:00
"github.com/joho/godotenv"
2026-10-05 15:43:35 -06:00
"github.com/robfig/cron/v3"
2026-10-16 00:15:03 -06:00
"github.com/urfave/cli/v3"
"go.destrealm.net/logging/v3"
"go.destrealm.net/vfs"
2026-10-05 15:43:35 -06:00
"code.destrealm.net/zancarius/justusbot/internal/bible"
2026-10-20 15:17:16 -06:00
"code.destrealm.net/zancarius/justusbot/internal/models"
2026-10-05 15:43:35 -06:00
"code.destrealm.net/zancarius/justusbot/internal/settings"
)
2026-10-16 00:15:03 -06:00
type App struct {
bot *bot
ctx context.Context
2026-10-20 15:17:16 -06:00
db *models.Database
2026-10-16 00:15:03 -06:00
fs vfs.VFS
logger *logging.Log
2026-10-05 15:43:35 -06:00
settings *settings.Settings
cron *cron.Cron
}
2026-10-20 15:17:16 -06:00
func NewApp(ctx context.Context, root vfs.VFS, conf *settings.Settings) (*App, error) {
app := &App{
ctx: ctx,
db: models.OpenDatabase(ctx, conf),
2026-10-16 00:15:03 -06:00
fs: root,
logger: logging.Logger(),
settings: conf,
2026-10-05 15:43:35 -06:00
}
2026-10-20 15:17:16 -06:00
if err := app.db.Open(); err != nil {
return nil, err
}
return app, nil
}
func (a *App) loadCron() error {
a.cron = cron.New()
2026-10-20 20:17:29 -06:00
// Scripture schedule.
2026-10-20 15:17:16 -06:00
a.cron.AddFunc(fmt.Sprintf("TZ=%s %s",
a.settings.System.TimeZone,
a.settings.DailyScripture.Schedule), a.scripture)
fmt.Println("using schedule:", fmt.Sprintf("TZ=%s %s", a.settings.System.TimeZone, a.settings.DailyScripture.Schedule))
2026-10-20 15:17:16 -06:00
a.cron.Start()
2026-10-20 20:17:29 -06:00
fmt.Println("cron started")
2026-10-20 15:17:16 -06:00
return nil
2026-10-05 15:43:35 -06:00
}
2026-10-16 00:15:03 -06:00
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
}
}
2026-10-20 15:17:16 -06:00
//if chats, err := a.db.
2026-10-16 00:15:03 -06:00
return nil
}
2026-10-20 15:17:16 -06:00
func (a *App) scripture() {
fmt.Println("scripture send triggered")
fmt.Println("sending to chats:")
chats, err := a.db.Chats().ListChats()
if err == nil {
for _, chat := range chats {
fmt.Println(chat)
}
} else {
fmt.Println(" !!! error !!!")
fmt.Println(err)
return
}
2026-10-20 15:17:16 -06:00
vv, err := a.db.Verses().RandomVerseRotate()
if err != nil {
fmt.Println("error retrieving random verse:", err)
return
}
ref := bible.NewBibleRef()
ref.Book = vv.Book
ref.Chapter = vv.Chapter
ref.Verses.Start = vv.VerseStart
ref.Verses.Stop = vv.VerseStop
vv.Translation = "bsb"
tnfs := a.fs.Clone()
if err := tnfs.Chdir("data"); err != nil {
fmt.Println("unable to change dir:", err)
return
}
tns, err := bible.LoadTranslations(tnfs)
if err != nil {
fmt.Println("unable to load translations:", err)
return
}
if !tns.HasTranslation(vv.Translation) {
fmt.Println("no such translation:", vv.Translation)
return
}
bbl, err := tns.OpenBible(vv.Translation)
if err != nil {
fmt.Println("unable to open Bible:", err)
return
}
verse, err := bbl.FromRef(ref)
if err != nil {
fmt.Println("error reading verses:", err)
return
}
verse.SkipLineBreaks = true
verse.IncludeVerseNum = false
fmt.Println(verse.String())
fmt.Println("to bot:", a.bot)
for _, chat := range chats {
2026-10-20 20:17:29 -06:00
a.bot.SendMarkdownMessage(chat.ID,
fmt.Sprintf("**<u>Daily Scripture:</u><br>%s**\n\n%s (*%s*)", ref.String(), verse.String(), strings.ToUpper(vv.Translation)))
}
2026-10-20 15:17:16 -06:00
}
2026-10-16 00:15:03 -06:00
func (a *App) Start(ctx context.Context, cmd *cli.Command) error {
if err := a.loadEnv(); err != nil {
return err
}
2026-10-20 15:17:16 -06:00
if err := a.loadCron(); err != nil {
return err
}
2026-10-16 00:15:03 -06:00
2026-10-20 20:17:29 -06:00
fmt.Println("context:", ctx, a.ctx)
a.bot = NewBot(ctx, a.settings)
a.bot.Run()
2026-10-16 00:15:03 -06:00
2026-10-05 15:43:35 -06:00
return nil
}
type chats struct {
}