Fixed recurring events by passing through Discord session (#2)
Co-authored-by: Layla <layla@layla.gg> Reviewed-on: https://gitea.sumulayla.synology.me/layla/birdbot/pulls/2
This commit is contained in:
parent
91a886a81e
commit
a86bdc4a65
@ -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"`
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
4
main.go
4
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() {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user