birdbot/components/announce_events.go

62 lines
1.7 KiB
Go
Raw Normal View History

2023-03-31 22:49:50 +02:00
package components
2023-03-31 05:51:05 +02:00
import (
"fmt"
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/mastodon"
)
2023-03-31 22:49:50 +02:00
type announceEventsComponent struct {
2023-03-31 05:51:05 +02:00
bot common.ComponentManager
mastodon *mastodon.Mastodon
guildID string
}
2023-03-31 22:49:50 +02:00
// NewAnnounceEventsComponent creates a new component
func NewAnnounceEventsComponent(mastodon *mastodon.Mastodon, guildID string) common.Component {
return &announceEventsComponent{
2023-03-31 05:51:05 +02:00
mastodon: mastodon,
guildID: guildID,
}
}
2023-03-31 22:49:50 +02:00
// Initialize registers event listeners
func (c *announceEventsComponent) Initialize(birdbot common.ComponentManager) error {
2023-03-31 05:51:05 +02:00
c.bot = birdbot
_ = birdbot.OnEventCreate(c.OnEventCreate)
2023-03-31 22:21:49 +02:00
_ = birdbot.OnEventDelete(c.OnEventDelete)
2023-03-31 05:51:05 +02:00
return nil
}
2023-03-31 22:49:50 +02:00
// OnEventCreate notifies about the event creation to given providers
func (c *announceEventsComponent) OnEventCreate(e common.Event) error {
2023-03-31 05:51:05 +02:00
eventURL := fmt.Sprintf("https://discordapp.com/events/%s/%s", c.guildID, e.ID)
c.bot.Notify(fmt.Sprintf("%s is organizing an event '%s': %s", e.Organizer.DiscordMention(), e.Name, eventURL))
2023-03-31 22:49:50 +02:00
// Toot an announcement if Mastodon is configured
2023-03-31 05:51:05 +02:00
if c.mastodon != nil {
err := c.mastodon.Toot(fmt.Sprintf("A new event has been organized '%s': %s", e.Name, eventURL))
if err != nil {
fmt.Println("Failed to send Mastodon Toot:", err)
}
}
return nil
}
2023-03-31 22:49:50 +02:00
func (c *announceEventsComponent) OnEventDelete(e common.Event) error {
2023-03-31 05:51:05 +02:00
_ = c.bot.Notify(fmt.Sprintf("%s cancelled '%s' on %s, %d!", e.Organizer.DiscordMention(), e.Name, e.DateTime.Month().String(), e.DateTime.Day()))
if c.mastodon != nil {
err := c.mastodon.Toot(fmt.Sprintf("'%s' cancelled on %s, %d!", e.Name, e.DateTime.Month().String(), e.DateTime.Day()))
if err != nil {
fmt.Println("Failed to send Mastodon Toot:", err)
}
}
return nil
}