From e91413a2387d720b25caddd9208b48a9ada4ecb2 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Sat, 24 Oct 2026 14:10:24 -0600 Subject: [PATCH] Bug fixes, rendering fixes, and added list all/add birthdays. --- cmd/bot/main.go | 39 +++++++++++ internal/commands/birthdays.go | 116 ++++++++++++++++++++++++++++++++- internal/models/birthdays.go | 39 +++++++++-- 3 files changed, 185 insertions(+), 9 deletions(-) diff --git a/cmd/bot/main.go b/cmd/bot/main.go index a4da95c..0f2a27b 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -47,6 +47,45 @@ func main() { Name: "birthdays", Description: "Birthday management", Commands: []*cli.Command{ + { + Name: "add", + Description: "Adds a birthday to the roster. Chat ID and name required.", + Action: cmdHost.Birthdays().AddBirthday, + Flags: []cli.Flag{ + &cli.Int64Flag{ + Name: "chatid", + Aliases: []string{"c"}, + Usage: "Specifies the chat ID. Required.", + }, + &cli.StringFlag{ + Name: "name", + Aliases: []string{"n"}, + Usage: "User's name. The user does not have to be present in the specified chat.", + }, + &cli.Int64Flag{ + Name: "userid", + Aliases: []string{"u"}, + Usage: "User ID. The user must be present in the specified chat if this flag is provided.", + }, + &cli.IntFlag{ + Name: "birthday-id", + Aliases: []string{"b"}, + Usage: "Birthday ID. Use this flag to update a given birthday. This can be used to change the birthday's date or to attach a user ID with a previously entered birthday.", + }, + }, + }, + { + Name: "list", + Description: "Lists all birthdays in the current roster for the specified chat ID.", + Action: cmdHost.Birthdays().ListBirthdays, + Flags: []cli.Flag{ + &cli.Int64Flag{ + Name: "chatid", + Aliases: []string{"c"}, + Usage: "Specifies the chat ID. Required.", + }, + }, + }, { Name: "weekly", Description: "Lists the weekly birthdays for this week.", diff --git a/internal/commands/birthdays.go b/internal/commands/birthdays.go index 75ebbd7..1ab242f 100644 --- a/internal/commands/birthdays.go +++ b/internal/commands/birthdays.go @@ -2,6 +2,8 @@ package commands import ( "context" + "strconv" + "strings" "time" "code.destrealm.net/zancarius/justusbot/internal/models" @@ -12,11 +14,80 @@ import ( "go.destrealm.net/vfs" ) +var ErrCannotParseDate = errors.NewError("unable to parse date") +var ErrCannotParseMonth = errors.NewError("unable to parse month") +var ErrCannotParseBirthday = errors.NewError("unable to parse birthday") + type birthday struct { fs vfs.VFS settings *settings.Settings } +func (b *birthday) AddBirthday(ctx context.Context, cmd *cli.Command) error { + logger := logging.Get("commands") + logger.SetFormat("{{message}}") + logger.SetLevel(logging.Info) + + chatid := cmd.Int64("chatid") + userid := cmd.Int64("userid") + bdayid := cmd.Int("birthday-id") + name := cmd.String("name") + + _ = userid + _ = bdayid + + var date time.Time + var err error + var month int + + logger.Info("Add birthday:") + logger.Info("=============") + + datearg := strings.Join(cmd.Args().Slice(), " ") + datearg = strings.ReplaceAll(datearg, "sept", "sep") + datearg = strings.ReplaceAll(datearg, "Sept", "Sep") + if date, err = time.Parse("Jan 02", datearg); err == nil { + // Noop. + } else if date, err = time.Parse("Jan _2", datearg); err == nil { + // Noop. + } else if date, err = time.Parse("Jan 2", datearg); err == nil { + // Noop. + } else if date, err = time.Parse("January 02", datearg); err == nil { + // Noop. + } else if date, err = time.Parse("January _2", datearg); err == nil { + // Noop. + } else if date, err = time.Parse("January 2", datearg); err == nil { + // Noop. + } else if date, err = time.Parse("2006-01-02", datearg); err == nil { + // Noop. + } else { + logger.Error("unable to parse given date:", datearg) + return ErrCannotParseDate.Do(err) + } + + month, err = strconv.Atoi(strings.TrimSpace(date.Format("1"))) + if err != nil { + logger.Error("unable to parse month:", month) + return ErrCannotParseMonth.Do(err) + } + + mdb := models.OpenDatabase(ctx, b.settings) + if err := mdb.Open(); err != nil { + logger.Error("connection error:", errors.Unfurl(err)) + return err + } + + mdl := models.NewBirthdayModel(ctx, mdb.DB()) + if err := mdl.AddBirthdayByName(chatid, name, month, date.Day(), 0); err != nil { + logger.Error("unable to add birthday:", err) + return ErrCannotParseBirthday.Do(err) + } + + logger.Infof("Added birthday for %s (%s %s)", name, date.Format("January"), + date.Format("2")) + return nil +} + func (b *birthday) ListWeeklyBirthdays(ctx context.Context, cmd *cli.Command) error { logger := logging.Get("commands") logger.SetFormat("{{message}}") @@ -53,7 +124,48 @@ func (b *birthday) ListWeeklyBirthdays(ctx context.Context, cmd *cli.Command) er logger.Error("cannot parse date:", bday.Birthdate) continue } - logger.Infof(" • %s: %s", bday.Name, d.Format("Mon, January _2")) + logger.Infof(" • %s: %s", bday.Name, d.Format("Mon, January 2")) + } + return nil +} + +func (b *birthday) ListBirthdays(ctx context.Context, cmd *cli.Command) error { + logger := logging.Get("commands") + logger.SetFormat("{{message}}") + logger.SetLevel(logging.Info) + + chatid := cmd.Int64("chatid") + + mdb := models.OpenDatabase(ctx, b.settings) + if err := mdb.Open(); err != nil { + logger.Error("connection error:", errors.Unfurl(err)) + return err + } + + mdl := models.NewBirthdayModel(ctx, mdb.DB()) + bdays, err := mdl.ListBirthdays(chatid) + if err != nil { + if errors.Is(err, models.ErrNoRows) { + logger.Error("no chats configured") + return nil + } + return err + } + if len(bdays) == 0 { + logger.Info("All birthdays for chat:") + logger.Info("=======================================") + logger.Info(" (none)") + return nil + } + logger.Info("All birthdays for chat:") + logger.Info("=======================================") + for _, bday := range bdays { + d, err := time.Parse("2006-01-02", bday.Birthdate) + if err != nil { + logger.Error("cannot parse date:", bday.Birthdate) + continue + } + logger.Infof(" • %s (%s)", bday.Name, d.Format("Mon, January 2")) } return nil } @@ -86,7 +198,7 @@ func (b *birthday) ListTodaysBirthdays(ctx context.Context, cmd *cli.Command) er logger.Info(" (none)") return nil } - logger.Info("Birthdays today:", time.Now().Format("Mon, January _2")) + logger.Info("Birthdays today:", time.Now().Format("Mon, January 2")) logger.Info("=======================================") for _, bday := range bdays { if err != nil { diff --git a/internal/models/birthdays.go b/internal/models/birthdays.go index a7bbad5..8b73e27 100644 --- a/internal/models/birthdays.go +++ b/internal/models/birthdays.go @@ -30,24 +30,23 @@ func NewBirthdayModel(ctx context.Context, db *sqlx.DB) *BirthdayModel { return &BirthdayModel{ctx, db} } -func (bm *BirthdayModel) AddBirthdayByName(chatid int64, name string, month, day, delta int) (*Birthday, error) { - bday := &Birthday{} +func (bm *BirthdayModel) AddBirthdayByName(chatid int64, name string, month, day, delta int) error { q := `insert into birthdays (chatid, name, month, day, leap_delta) values (?, ?, ?, ?, ?)` - err := bm.db.GetContext(bm.ctx, bday, q, chatid, name, month, day, delta) + _, err := bm.db.ExecContext(bm.ctx, q, chatid, name, month, day, delta) if err != nil { - return nil, err + return err } if err != nil { var serr sqlite3.Error if errors.As(err, &serr) { switch serr.ExtendedCode { case sqlite3.ErrConstraintUnique: - return nil, ErrUniqueConstraint.Do(err) + return ErrUniqueConstraint.Do(err) } } - return nil, err + return err } - return bday, nil + return nil } func (bm *BirthdayModel) AddBirthdayByUserID(chatid int64, userid int, month, day int) (*Birthday, error) { @@ -108,3 +107,29 @@ func (bm *BirthdayModel) BirthdaysToday(chatid int64) ([]*Birthday, error) { return bdays, nil } + +func (bm *BirthdayModel) ListBirthdays(chatid int64) ([]*Birthday, error) { + var bdays []*Birthday + q := `select + id, userid, chatid, name, leap_delta, + date(concat(strftime('%Y', + date('now', 'localtime')), '-', + format('%02d', month), '-', + format('%02d', day))) as birthdate + from birthdays where + chatid = ?;` + + err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid) + if err != nil { + var serr sqlite3.Error + if errors.As(err, &serr) { + switch serr.Code { + case sqlite3.ErrMismatch: + return nil, ErrNoRows.Do(err) + } + } + return nil, err + } + + return bdays, nil +}