From 77c1fa9b81674e2b319ed01eddee0ed3ec97709d Mon Sep 17 00:00:00 2001 From: Layla Date: Sat, 10 Jun 2023 08:44:31 +0000 Subject: [PATCH] Fixed recurring events by passing through Discord session --- core/configuration.go | 2 +- events/announce_events.go | 61 ---------------------- events/manage_event_channels.go | 89 --------------------------------- events/recurring_events.go | 43 ---------------- main.go | 4 +- modules/recurring_events.go | 6 ++- 6 files changed, 7 insertions(+), 198 deletions(-) delete mode 100644 events/announce_events.go delete mode 100644 events/manage_event_channels.go delete mode 100644 events/recurring_events.go diff --git a/core/configuration.go b/core/configuration.go index 2bb2bc0..f22904e 100644 --- a/core/configuration.go +++ b/core/configuration.go @@ -47,7 +47,7 @@ type MastodonConfig struct { type Features struct { ManageEventChannels Feature `yaml:"manage_event_channels" env:"BIRD_EVENT_CHANNELS"` AnnounceEvents Feature `yaml:"announce_events" env:"BIRD_ANNOUNCE_EVENTS"` - ReccurringEvents Feature `yaml:"recurring_events" env:"BIRD_RECURRING_EVENTS"` + RecurringEvents Feature `yaml:"recurring_events" env:"BIRD_RECURRING_EVENTS"` RoleSelection Feature `yaml:"role_selection" env:"BIRD_ROLE_SELECTION"` LoadGamePlugins Feature `yaml:"load_game_plugins" env:"BIRD_LOAD_GAME_PLUGINS"` } diff --git a/events/announce_events.go b/events/announce_events.go deleted file mode 100644 index 792da3b..0000000 --- a/events/announce_events.go +++ /dev/null @@ -1,61 +0,0 @@ -package events - -import ( - "fmt" - - "github.com/yeslayla/birdbot/common" - "github.com/yeslayla/birdbot/mastodon" -) - -type announceEventsComponent struct { - bot common.ModuleManager - mastodon *mastodon.Mastodon - guildID string -} - -// NewAnnounceEventsComponent creates a new component -func NewAnnounceEventsComponent(mastodon *mastodon.Mastodon, guildID string) common.Module { - return &announceEventsComponent{ - mastodon: mastodon, - guildID: guildID, - } -} - -// Initialize registers event listeners -func (c *announceEventsComponent) Initialize(birdbot common.ModuleManager) error { - c.bot = birdbot - - _ = birdbot.OnEventCreate(c.OnEventCreate) - _ = birdbot.OnEventDelete(c.OnEventDelete) - - return nil -} - -// OnEventCreate notifies about the event creation to given providers -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)) - - // Toot an announcement if Mastodon is configured - 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 -} diff --git a/events/manage_event_channels.go b/events/manage_event_channels.go deleted file mode 100644 index 98b0ece..0000000 --- a/events/manage_event_channels.go +++ /dev/null @@ -1,89 +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 -} - -// NewManageEventChannelsComponent creates a new component -func NewManageEventChannelsComponent(categoryID string, archiveCategoryID string, session *discord.Discord) common.Module { - return &manageEventChannelsComponent{ - session: session, - categoryID: categoryID, - archiveCategoryID: archiveCategoryID, - } -} - -// Initialize registers event listeners -func (c *manageEventChannelsComponent) Initialize(birdbot common.ModuleManager) error { - _ = birdbot.OnEventCreate(c.OnEventCreate) - _ = birdbot.OnEventComplete(c.OnEventComplete) - _ = birdbot.OnEventDelete(c.OnEventDelete) - - return nil -} - -// OnEventCreate creates a new channel for an event and moves it to a given category -func (c *manageEventChannelsComponent) OnEventCreate(e common.Event) error { - channel, err := c.session.NewChannelFromName(core.GenerateChannelFromEvent(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 -} - -// OnEventDelete deletes the channel associated with the given event -func (c *manageEventChannelsComponent) OnEventDelete(e common.Event) error { - _, err := c.session.DeleteChannel(core.GenerateChannelFromEvent(e)) - if err != nil { - log.Print("Failed to create channel for event: ", err) - } - return nil -} - -// OnEventComplete archives a given event channel if not given -// an archive category will delete the channel instead -func (c *manageEventChannelsComponent) OnEventComplete(e common.Event) error { - channel := core.GenerateChannelFromEvent(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 -} diff --git a/events/recurring_events.go b/events/recurring_events.go deleted file mode 100644 index bc0fb86..0000000 --- a/events/recurring_events.go +++ /dev/null @@ -1,43 +0,0 @@ -package events - -import ( - "log" - "strings" - - "github.com/yeslayla/birdbot/common" - "github.com/yeslayla/birdbot/discord" -) - -type recurringEventsComponent struct { - session *discord.Discord -} - -// NewRecurringEventsComponent creates a new component instance -func NewRecurringEventsComponent() common.Module { - return &recurringEventsComponent{} -} - -// Initialize registers event listeners -func (c *recurringEventsComponent) Initialize(birdbot common.ModuleManager) error { - _ = birdbot.OnEventComplete(c.OnEventComplete) - - return nil -} - -// OnEventComplete checks for keywords before creating a new event -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 -} diff --git a/main.go b/main.go index 046d5f4..4b51fbc 100644 --- a/main.go +++ b/main.go @@ -73,8 +73,8 @@ func main() { if cfg.Features.ManageEventChannels.IsEnabledByDefault() { loader.LoadComponent(modules.NewManageEventChannelsComponent(cfg.Discord.EventCategory, cfg.Discord.ArchiveCategory, bot.Session)) } - if cfg.Features.ReccurringEvents.IsEnabledByDefault() { - loader.LoadComponent(modules.NewRecurringEventsComponent()) + if cfg.Features.RecurringEvents.IsEnabledByDefault() { + loader.LoadComponent(modules.NewRecurringEventsComponent(bot.Session)) } if cfg.Features.RoleSelection.IsEnabledByDefault() { diff --git a/modules/recurring_events.go b/modules/recurring_events.go index 908cb79..e3cce36 100644 --- a/modules/recurring_events.go +++ b/modules/recurring_events.go @@ -13,8 +13,10 @@ type recurringEventsModule struct { } // NewRecurringEventsComponent creates a new component instance -func NewRecurringEventsComponent() common.Module { - return &recurringEventsModule{} +func NewRecurringEventsComponent(session *discord.Discord) common.Module { + return &recurringEventsModule{ + session: session, + } } // Initialize registers event listeners