birdbot/modules/recurring_events.go

49 lines
1.2 KiB
Go
Raw Normal View History

package modules
2023-03-30 23:51:05 -04:00
import (
"log"
"strings"
"github.com/yeslayla/birdbot-common/common"
2023-03-30 23:51:05 -04:00
"github.com/yeslayla/birdbot/discord"
)
type recurringEventsModule struct {
2023-03-30 23:51:05 -04:00
session *discord.Discord
}
2023-03-31 20:49:50 +00:00
// NewRecurringEventsComponent creates a new component instance
2024-01-14 18:47:36 +01:00
//
// Deprecated: This recurring events are now a native feature in Discord,
// so this will be removed in the next version.
func NewRecurringEventsComponent(session *discord.Discord) common.Module {
return &recurringEventsModule{
session: session,
}
2023-03-30 23:51:05 -04:00
}
2023-03-31 20:49:50 +00:00
// Initialize registers event listeners
func (c *recurringEventsModule) Initialize(birdbot common.ModuleManager) error {
2023-03-30 23:51:05 -04:00
_ = birdbot.OnEventComplete(c.OnEventComplete)
return nil
}
2023-03-31 20:49:50 +00:00
// OnEventComplete checks for keywords before creating a new event
func (c *recurringEventsModule) OnEventComplete(e common.Event) error {
2023-03-30 23:51:05 -04: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
}