birdbot/events/manage_event_channels.go

84 lines
2.1 KiB
Go
Raw Normal View History

2023-03-31 05:51:05 +02:00
package events
import (
"log"
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
)
type ManageEventChannelsComponent struct {
2023-03-31 22:21:49 +02:00
session *discord.Discord
categoryID string
archiveCategoryID string
2023-03-31 05:51:05 +02:00
}
2023-03-31 22:21:49 +02:00
func NewManageEventChannelsComponent(categoryID string, archiveCategoryID string, session *discord.Discord) *ManageEventChannelsComponent {
2023-03-31 05:51:05 +02:00
return &ManageEventChannelsComponent{
2023-03-31 22:21:49 +02:00
session: session,
categoryID: categoryID,
archiveCategoryID: archiveCategoryID,
2023-03-31 05:51:05 +02:00
}
}
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)
2023-03-31 22:21:49 +02:00
if c.archiveCategoryID != "" {
2023-03-31 05:51:05 +02:00
2023-03-31 22:21:49 +02:00
if err := c.session.MoveChannelToCategory(channel, c.archiveCategoryID); err != nil {
2023-03-31 05:51:05 +02:00
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
}