Compentize Workload (#4)
This commit is contained in:
56
events/announce_events.go
Normal file
56
events/announce_events.go
Normal file
@ -0,0 +1,56 @@
|
||||
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)
|
||||
|
||||
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
|
||||
}
|
81
events/manage_event_channels.go
Normal file
81
events/manage_event_channels.go
Normal file
@ -0,0 +1,81 @@
|
||||
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
|
||||
}
|
||||
|
||||
func NewManageEventChannelsComponent(categoryID string, session *discord.Discord) *ManageEventChannelsComponent {
|
||||
return &ManageEventChannelsComponent{
|
||||
session: session,
|
||||
categoryID: categoryID,
|
||||
}
|
||||
}
|
||||
|
||||
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.categoryID != "" {
|
||||
|
||||
if err := c.session.MoveChannelToCategory(channel, c.categoryID); 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
|
||||
}
|
40
events/recurring_events.go
Normal file
40
events/recurring_events.go
Normal file
@ -0,0 +1,40 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user