diff --git a/cmd/bot/main.go b/cmd/bot/main.go index d45ec15..2414728 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -43,6 +43,56 @@ func main() { }, }, Commands: []*cli.Command{ + { + Name: "announcements", + Description: "Announcement management", + Commands: []*cli.Command{ + { + Name: "add", + Description: "Adds an announcement to the queue.", + Action: cmdHost.Announcements().AddAnnouncement, + Flags: []cli.Flag{ + &cli.Int64Flag{ + Name: "chatid", + Aliases: []string{"c"}, + Usage: "Specifies the chat ID. Required.", + }, + &cli.BoolFlag{ + Name: "reminders", + Aliases: []string{"r"}, + Usage: "Enables reminders for this announcement. Reminders will be triggered one week prior and one day prior by default.", + }, + &cli.StringFlag{ + Name: "title", + Aliases: []string{"t"}, + Usage: "Announcement title (optional).", + }, + &cli.StringFlag{ + Name: "content", + Aliases: []string{"a"}, + Usage: "Announcement content (required).", + }, + }, + }, + { + Name: "weekly", + Description: "Lists this week's announcements.", + Action: cmdHost.Announcements().ListThisWeeksAnnouncements, + }, + { + Name: "today", + Description: "Lists the announcements for today.", + Action: cmdHost.Announcements().ListTodaysAnnouncements, + Flags: []cli.Flag{ + &cli.Int64Flag{ + Name: "chatid", + Aliases: []string{"c"}, + Usage: "Specifies the chat ID. Required.", + }, + }, + }, + }, + }, { Name: "birthdays", Description: "Birthday management", diff --git a/internal/app/app.go b/internal/app/app.go index 01b3e76..d7f4323 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -94,8 +94,6 @@ func (a *App) loadEnv() error { } func (a *App) daily() { - fmt.Println("daily trigger") - chats, err := a.db.Chats().ListChats() if err != nil { a.logger.Error("unable to retrieve chats:", err) @@ -125,6 +123,22 @@ func (a *App) daily() { 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") diff --git a/internal/commands/common.go b/internal/commands/common.go index 848802f..f8db6cb 100644 --- a/internal/commands/common.go +++ b/internal/commands/common.go @@ -10,13 +10,14 @@ import ( ) type commands struct { - ctx context.Context - root vfs.VFS - birthday *birthday - chats *chats - meta *metadata - verse *verse - settings *settings.Settings + ctx context.Context + root vfs.VFS + announcement *announcement + birthday *birthday + chats *chats + meta *metadata + verse *verse + settings *settings.Settings } func NewCommandHost(ctx context.Context, root vfs.VFS) (*commands, error) { @@ -56,6 +57,16 @@ func (c *commands) Initalizer() *installer { return i } +func (c *commands) Announcements() *announcement { + if c.announcement == nil { + c.announcement = &announcement{ + fs: c.root, + settings: c.settings, + } + } + return c.announcement +} + func (c *commands) Birthdays() *birthday { if c.birthday == nil { c.birthday = &birthday{ diff --git a/internal/models/db.go b/internal/models/db.go index a46ad35..dafcbbc 100644 --- a/internal/models/db.go +++ b/internal/models/db.go @@ -20,9 +20,10 @@ type Database struct { db *sqlx.DB conf *settings.Settings - birthdays *BirthdayModel - chats *ChatModel - verses *VerseModel + announcements *AnnouncementModel + birthdays *BirthdayModel + chats *ChatModel + verses *VerseModel } func OpenDatabase(ctx context.Context, conf *settings.Settings) *Database { @@ -60,6 +61,13 @@ func (d *Database) Migrate(migrations vfs.VFS) error { return migrator.Up() } +func (d *Database) Announcements() *AnnouncementModel { + if d.announcements == nil { + d.announcements = NewAnnouncementModel(d.ctx, d.db) + } + return d.announcements +} + func (d *Database) Birthdays() *BirthdayModel { if d.birthdays == nil { d.birthdays = NewBirthdayModel(d.ctx, d.db)