Discord Components & Role Selection (#5)
This commit is contained in:
		
							
								
								
									
										61
									
								
								events/announce_events.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								events/announce_events.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,61 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										89
									
								
								events/manage_event_channels.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								events/manage_event_channels.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,89 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								events/recurring_events.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								events/recurring_events.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user