package app import ( "context" "fmt" "io" "strings" "time" "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/bible" "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 txns *bible.Translations 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 } if txns, err := bible.LoadTranslations(root); err != nil { return nil, err } else { app.txns = txns } return app, nil } func (a *App) loadCron() error { a.cron = cron.New() // Scripture schedule. a.cron.AddFunc(fmt.Sprintf("TZ=%s %s", a.settings.System.TimeZone, a.settings.DailyScripture.Schedule), a.scripture) a.cron.AddFunc(fmt.Sprintf("TZ=%s %s", a.settings.System.TimeZone, a.settings.Scheduler.Announcements.Daily), a.daily) a.cron.Start() fmt.Println("cron started") 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) daily() { chats, err := a.db.Chats().ListChats() if err != nil { a.logger.Error("unable to retrieve chats:", err) return } sb := &strings.Builder{} for _, chat := range chats { if time.Now().Format("Monday") == a.settings.Scheduler.Announcements.Weekly { bdays, err := a.db.Birthdays().BirthdaysThisWeek(chat.ID) if err == nil && len(bdays) > 0 { sb.WriteString("**Birthdays this week (Sunday-to-Sunday):**\n\n") for _, bday := range bdays { d, derr := time.Parse("2006-01-02", bday.Birthdate) if derr == nil { sb.WriteString(" * ") sb.WriteString(bday.Name) sb.WriteString(" (") sb.WriteString(d.Format("Mon, January _2")) sb.WriteString(")\n") } else { a.logger.Error("cannot parse birthday:", bday.Birthdate) } } sb.WriteByte('\n') } else { a.logger.Error("could not retrieve this week's birthdays:", err) } } announce, err := a.db.Announcements().AnnouncementsToday(chat.ID) if err == nil && len(announce) > 0 { sb.WriteString("**📢 Today's Announcements 📢**\n\n") for _, ann := range announce { if ann.Title != "" { sb.WriteString("**") sb.WriteString(ann.Title) sb.WriteString("**") sb.WriteString("\n\n") } sb.WriteString(" * ") sb.WriteString(ann.Content) sb.WriteByte('\n') } sb.WriteByte('\n') } bdays, err := a.db.Birthdays().BirthdaysToday(chat.ID) if err == nil && len(bdays) > 0 { sb.WriteString("**🎂 Today's Birthday") if len(bdays) > 1 { sb.WriteString("s") } sb.WriteString(" 🎂**\n\n") for _, bday := range bdays { sb.WriteString(" * ") sb.WriteString(bday.Name) sb.WriteString("\n") } sb.WriteString("\n🎉🎊🎈Happy Birthday!🎈🎊🎉") } else { a.logger.Error("could not retrieve this week's birthdays:", err) } // When no birthdays are present, an error is raised preventing the code // from getting to this point. // // I'm somewhat concerned that once the error handling is resolved, this // could get triggered without some guards. Considering checkout // sb.Len() or the total birthdays. a.bot.SendMarkdownMessage(chat.ID, sb.String()) } } 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 } 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" verses, err := a.txns.LoadRef(vv.Translation, ref) verses.SkipLineBreaks = true verses.IncludeVerseNum = false fmt.Println(verses.String()) fmt.Println("to bot:", a.bot) for _, chat := range chats { a.bot.SendMarkdownMessage(chat.ID, fmt.Sprintf("**Daily Scripture:
%s**\n\n%s (*%s*)", ref.String(), verses.String(), strings.ToUpper(vv.Translation))) } } 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 } fmt.Println("context:", ctx, a.ctx) a.bot = NewBot(ctx, a.settings) a.bot.Run() return nil } type chats struct { }