This commit is contained in:
2023-03-31 20:49:50 +00:00
parent 6e5450aa80
commit 5e6a433b92
15 changed files with 75 additions and 46 deletions

View File

@ -1,57 +0,0 @@
package events
import (
"fmt"
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/mastodon"
)
type AnnounceEventsComponent struct {
bot common.ComponentManager
mastodon *mastodon.Mastodon
guildID string
}
func NewAnnounceEventsComponent(mastodon *mastodon.Mastodon, guildID string) *AnnounceEventsComponent {
return &AnnounceEventsComponent{
mastodon: mastodon,
guildID: guildID,
}
}
func (c *AnnounceEventsComponent) Initialize(birdbot common.ComponentManager) error {
c.bot = birdbot
_ = birdbot.OnEventCreate(c.OnEventCreate)
_ = birdbot.OnEventDelete(c.OnEventDelete)
return nil
}
func (c *AnnounceEventsComponent) OnEventCreate(e common.Event) error {
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))
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
}
func (c *AnnounceEventsComponent) OnEventDelete(e common.Event) error {
_ = 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
}

View File

@ -1,83 +0,0 @@
package events
import (
"log"
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
)
type ManageEventChannelsComponent struct {
session *discord.Discord
categoryID string
archiveCategoryID string
}
func NewManageEventChannelsComponent(categoryID string, archiveCategoryID string, session *discord.Discord) *ManageEventChannelsComponent {
return &ManageEventChannelsComponent{
session: session,
categoryID: categoryID,
archiveCategoryID: archiveCategoryID,
}
}
func (c *ManageEventChannelsComponent) Initialize(birdbot common.ComponentManager) error {
_ = birdbot.OnEventCreate(c.OnEventCreate)
_ = birdbot.OnEventComplete(c.OnEventComplete)
_ = birdbot.OnEventDelete(c.OnEventDelete)
return nil
}
func (c *ManageEventChannelsComponent) OnEventCreate(e common.Event) error {
channel, err := c.session.NewChannelFromName(core.GenerateChannel(e).Name)
if err != nil {
log.Print("Failed to create channel for event: ", err)
}
if c.categoryID != "" {
err = c.session.MoveChannelToCategory(channel, c.categoryID)
if err != nil {
log.Printf("Failed to move channel to events category '%s': %v", channel.Name, err)
}
}
return nil
}
func (c *ManageEventChannelsComponent) OnEventDelete(e common.Event) error {
_, err := c.session.DeleteChannel(core.GenerateChannel(e))
if err != nil {
log.Print("Failed to create channel for event: ", err)
}
return nil
}
func (c *ManageEventChannelsComponent) OnEventComplete(e common.Event) error {
channel := core.GenerateChannel(e)
if c.archiveCategoryID != "" {
if err := c.session.MoveChannelToCategory(channel, c.archiveCategoryID); err != nil {
log.Print("Failed to move channel to archive category: ", err)
}
if err := c.session.ArchiveChannel(channel); err != nil {
log.Print("Failed to archive channel: ", err)
}
log.Printf("Archived channel: '%s'", channel.Name)
} else {
// Delete Channel
_, err := c.session.DeleteChannel(channel)
if err != nil {
log.Print("Failed to delete channel: ", err)
}
log.Printf("Deleted channel: '%s'", channel.Name)
}
return nil
}

View File

@ -1,40 +0,0 @@
package events
import (
"log"
"strings"
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/discord"
)
type RecurringEventsComponent struct {
session *discord.Discord
}
func NewRecurringEventsComponent() *RecurringEventsComponent {
return &RecurringEventsComponent{}
}
func (c *RecurringEventsComponent) Initialize(birdbot common.ComponentManager) error {
_ = birdbot.OnEventComplete(c.OnEventComplete)
return nil
}
func (c *RecurringEventsComponent) OnEventComplete(e common.Event) error {
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
}