2026-10-05 11:36:04 -06:00
|
|
|
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"
|
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
|
|
|
|
2026-10-20 15:34:03 -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-05 11:36:04 -06:00
|
|
|
)
|
|
|
|
|
|
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()
|
|
|
|
|
a.cron.AddFunc(fmt.Sprintf("TZ=%s %s",
|
|
|
|
|
a.settings.System.TimeZone,
|
|
|
|
|
a.settings.DailyScripture.Schedule), a.scripture)
|
|
|
|
|
|
|
|
|
|
a.cron.Start()
|
|
|
|
|
|
|
|
|
|
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() {
|
2026-10-20 15:34:03 -06:00
|
|
|
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
|
|
|
|
2026-10-20 15:34:03 -06:00
|
|
|
for _, chat := range chats {
|
|
|
|
|
vv, err := a.db.Verses().RandomVerseForChat(chat.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("error retrieving random verse:", err)
|
|
|
|
|
fmt.Println("chat ID:", chat.ID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ref := bible.NewBibleRef()
|
|
|
|
|
ref.Book = vv.Book
|
|
|
|
|
ref.Chapter = vv.Chapter
|
|
|
|
|
ref.Verses.Start = vv.VerseStart
|
|
|
|
|
ref.Verses.Stop = vv.VerseStop
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
bot := NewBot(ctx, a.settings)
|
|
|
|
|
bot.Run()
|
|
|
|
|
|
2026-10-05 15:43:35 -06:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type chats struct {
|
|
|
|
|
}
|