2023-03-31 22:49:50 +02:00
|
|
|
package components
|
2023-03-31 05:51:05 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/yeslayla/birdbot/common"
|
|
|
|
"github.com/yeslayla/birdbot/discord"
|
|
|
|
)
|
|
|
|
|
2023-03-31 22:49:50 +02:00
|
|
|
type recurringEventsComponent struct {
|
2023-03-31 05:51:05 +02:00
|
|
|
session *discord.Discord
|
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:50 +02:00
|
|
|
// NewRecurringEventsComponent creates a new component instance
|
|
|
|
func NewRecurringEventsComponent() common.Component {
|
|
|
|
return &recurringEventsComponent{}
|
2023-03-31 05:51:05 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:50 +02:00
|
|
|
// Initialize registers event listeners
|
|
|
|
func (c *recurringEventsComponent) Initialize(birdbot common.ComponentManager) error {
|
2023-03-31 05:51:05 +02:00
|
|
|
_ = birdbot.OnEventComplete(c.OnEventComplete)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 22:49:50 +02:00
|
|
|
// OnEventComplete checks for keywords before creating a new event
|
|
|
|
func (c *recurringEventsComponent) OnEventComplete(e common.Event) error {
|
2023-03-31 05:51:05 +02:00
|
|
|
|
|
|
|
if strings.Contains(strings.ToLower(e.Description), "recurring weekly") {
|
|
|
|
startTime := e.DateTime.AddDate(0, 0, 7)
|
|
|
|
finishTime := e.CompleteDateTime.AddDate(0, 0, 7)
|
|
|
|
nextEvent := e
|
|
|
|
nextEvent.DateTime = startTime
|
|
|
|
nextEvent.CompleteDateTime = finishTime
|
|
|
|
|
|
|
|
if err := c.session.CreateEvent(nextEvent); err != nil {
|
|
|
|
log.Print("Failed to create recurring event: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|