Major Refactor (#2)

* Major reworks

* More refactoring

* Refactor feature complete!

* Comments

* Add versioning
This commit is contained in:
2022-10-28 23:08:17 -04:00
committed by GitHub
parent 49aa4fdedb
commit 696ab7201c
18 changed files with 403 additions and 186 deletions

View File

@ -6,27 +6,28 @@ import (
"log"
"os"
"github.com/bwmarrin/discordgo"
"github.com/ilyakaznacheev/cleanenv"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
)
var Version string
var Build string
type Bot struct {
discord *discordgo.Session
session *discord.Discord
// Discord Objects
guildID string
eventCategoryID string
archiveCategoryID string
notificationChannelID string
// Signal for shutdown
stop chan os.Signal
}
// Initalize creates the discord session and registers handlers
func (app *Bot) Initialize(config_path string) error {
log.Printf("Using config: %s", config_path)
cfg := &Config{}
cfg := &core.Config{}
_, err := os.Stat(config_path)
if errors.Is(err, os.ErrNotExist) {
@ -51,171 +52,114 @@ func (app *Bot) Initialize(config_path string) error {
return fmt.Errorf("discord Guild ID is not set")
}
// Create Discord Session
app.discord, err = discordgo.New(fmt.Sprint("Bot ", cfg.Discord.Token))
if err != nil {
return fmt.Errorf("failed to create Discord session: %v", err)
}
app.session = discord.New(app.guildID, cfg.Discord.Token)
// Register Event Handlers
app.discord.AddHandler(app.onReady)
app.discord.AddHandler(app.onEventCreate)
app.discord.AddHandler(app.onEventDelete)
app.discord.AddHandler(app.onEventUpdate)
app.session.OnReady(app.onReady)
app.session.OnEventCreate(app.onEventCreate)
app.session.OnEventDelete(app.onEventDelete)
app.session.OnEventUpdate(app.onEventUpdate)
return nil
}
// Run opens the session with Discord until exit
func (app *Bot) Run() error {
if err := app.discord.Open(); err != nil {
return fmt.Errorf("failed to open Discord session: %v", err)
}
defer app.discord.Close()
// Keep alive
app.stop = make(chan os.Signal, 1)
<-app.stop
return nil
return app.session.Run()
}
// Stop triggers a graceful shutdown of the app
func (app *Bot) Stop() {
log.Print("Shuting down...")
app.stop <- os.Kill
app.session.Stop()
}
// Notify sends a message to the notification channe;
func (app *Bot) Notify(message string) {
if app.notificationChannelID == "" {
log.Println(message)
return
}
_, err := app.discord.ChannelMessageSend(app.notificationChannelID, message)
log.Print("Notification: ", message)
log.Println("Notification: ", message)
channel := app.session.NewChannelFromID(app.notificationChannelID)
if channel == nil {
log.Printf("Failed notification: channel was not found with ID '%v'", app.notificationChannelID)
}
err := app.session.SendMessage(channel, message)
if err != nil {
log.Println("Failed notification: ", err)
log.Print("Failed notification: ", err)
}
}
func (app *Bot) onReady(s *discordgo.Session, r *discordgo.Ready) {
app.Notify("BirdBot is ready!")
log.Print("BirdBot is ready!")
func (app *Bot) onReady(d *discord.Discord) {
app.Notify(fmt.Sprintf("BirdBot %s is ready!", Version))
}
func (app *Bot) onEventCreate(s *discordgo.Session, r *discordgo.GuildScheduledEventCreate) {
if r.GuildID != app.guildID {
return
}
func (app *Bot) onEventCreate(d *discord.Discord, event *core.Event) {
event := &Event{}
event.Name = r.Name
event.OrganizerID = r.CreatorID
event.DateTime = r.ScheduledStartTime
if r.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
event.Location = REMOTE_LOCATION
} else {
event.Location = r.EntityMetadata.Location
}
log.Print("Event Created: '", event.Name, "':'", event.Location, "'")
channel, err := CreateChannelIfNotExists(s, app.guildID, event.GetChannelName())
channel, err := app.session.NewChannelFromName(event.Channel().Name)
if err != nil {
log.Print("Failed to create channel for event: ", err)
}
if app.eventCategoryID != "" {
if _, err = s.ChannelEdit(channel.ID, &discordgo.ChannelEdit{
ParentID: app.eventCategoryID,
}); err != nil {
err = app.session.MoveChannelToCategory(channel, app.eventCategoryID)
if err != nil {
log.Printf("Failed to move channel to events category '%s': %v", channel.Name, err)
}
}
eventURL := fmt.Sprintf("https://discordapp.com/events/%s/%s", app.guildID, r.ID)
app.Notify(fmt.Sprintf("<@%s> is organizing an event '%s': %s", event.OrganizerID, event.Name, eventURL))
eventURL := fmt.Sprintf("https://discordapp.com/events/%s/%s", app.guildID, event.ID)
app.Notify(fmt.Sprintf("%s is organizing an event '%s': %s", event.Organizer.Mention(), event.Name, eventURL))
}
func (app *Bot) onEventDelete(s *discordgo.Session, r *discordgo.GuildScheduledEventDelete) {
if r.GuildID != app.guildID {
return
}
func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
// Create Event Object
event := &Event{}
event.Name = r.Name
event.OrganizerID = r.CreatorID
event.DateTime = r.ScheduledStartTime
if r.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
event.Location = REMOTE_LOCATION
} else {
event.Location = r.EntityMetadata.Location
}
_, err := DeleteChannel(app.discord, app.guildID, event.GetChannelName())
_, err := app.session.DeleteChannel(event.Channel())
if err != nil {
log.Print("Failed to create channel for event: ", err)
}
app.Notify(fmt.Sprintf("<@%s> cancelled '%s' on %s, %d!", event.OrganizerID, event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
app.Notify(fmt.Sprintf("%s cancelled '%s' on %s, %d!", event.Organizer.Mention(), event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
}
func (app *Bot) onEventUpdate(s *discordgo.Session, r *discordgo.GuildScheduledEventUpdate) {
if r.GuildID != app.guildID {
return
}
// Create Event Object
event := &Event{}
event.Name = r.Name
event.OrganizerID = r.CreatorID
event.DateTime = r.ScheduledStartTime
if r.EntityType != discordgo.GuildScheduledEventEntityTypeExternal {
event.Location = REMOTE_LOCATION
} else {
event.Location = r.EntityMetadata.Location
}
func (app *Bot) onEventUpdate(d *discord.Discord, event *core.Event) {
// Pass event onwards
switch r.Status {
case discordgo.GuildScheduledEventStatusCompleted:
app.onEventComplete(s, event)
if event.Completed {
app.onEventComplete(d, event)
}
}
func (app *Bot) onEventComplete(s *discordgo.Session, event *Event) {
func (app *Bot) onEventComplete(d *discord.Discord, event *core.Event) {
channel_name := event.GetChannelName()
channel := event.Channel()
if app.archiveCategoryID != "" {
// Get Channel ID
id, err := GetChannelID(s, app.guildID, channel_name)
if err != nil {
log.Printf("Failed to archive channel: %v", err)
return
if err := app.session.MoveChannelToCategory(channel, app.archiveCategoryID); err != nil {
log.Print("Failed to move channel to archive category: ", err)
}
// Move to archive category
if _, err := s.ChannelEdit(id, &discordgo.ChannelEdit{
ParentID: app.archiveCategoryID,
}); err != nil {
log.Printf("Failed to move channel to archive category: %v", err)
return
if err := app.session.ArchiveChannel(channel); err != nil {
log.Print("Failed to archive channel: ", err)
}
log.Printf("Archived channel: '%s'", channel_name)
log.Printf("Archived channel: '%s'", channel.Name)
} else {
// Delete Channel
_, err := DeleteChannel(s, app.guildID, channel_name)
_, err := app.session.DeleteChannel(channel)
if err != nil {
log.Print("Failed to delete channel: ", err)
}
log.Printf("Deleted channel: '%s'", channel_name)
log.Printf("Deleted channel: '%s'", channel.Name)
}
}

View File

@ -1,68 +0,0 @@
package app
import (
"fmt"
"log"
"github.com/bwmarrin/discordgo"
)
func CreateChannelIfNotExists(discord *discordgo.Session, guildID string, channel_name string) (*discordgo.Channel, error) {
// Grab channels to query
channels, err := discord.GuildChannels(guildID)
if err != nil {
return nil, fmt.Errorf("failed to list channels when creating new channel: '%s': %v", channel_name, err)
}
for _, channel := range channels {
// Found channel!
if channel.Name == channel_name {
log.Printf("Tried to create channel, but it already exists '%s'", channel_name)
return channel, nil
}
}
// Since a channel was not found, create one
channel, err := discord.GuildChannelCreate(guildID, channel_name, discordgo.ChannelTypeGuildText)
if err != nil {
return nil, fmt.Errorf("failed to created channel '%s': %v", channel_name, err)
}
log.Printf("Created channel: '%s'", channel_name)
return channel, nil
}
func DeleteChannel(discord *discordgo.Session, guildID string, channel_name string) (bool, error) {
channels, err := discord.GuildChannels(guildID)
if err != nil {
return false, fmt.Errorf("failed to list channels when deleting channel: '%s': %v", channel_name, err)
}
for _, channel := range channels {
if channel.Name == channel_name {
_, err = discord.ChannelDelete(channel.ID)
if err != nil {
return false, fmt.Errorf("failed to delete channel: %v", err)
}
return true, nil
}
}
log.Printf("Tried to delete channel, but it didn't exist '%s'", channel_name)
return false, nil
}
func GetChannelID(discord *discordgo.Session, guildID string, channel_name string) (string, error) {
channels, err := discord.GuildChannels(guildID)
if err != nil {
return "", fmt.Errorf("failed to list channels when getting channel id: '%s': %v", channel_name, err)
}
for _, channel := range channels {
if channel.Name == channel_name {
return channel.ID, nil
}
}
return "", fmt.Errorf("failed to get channel id for '%s': channel not found", channel_name)
}

View File

@ -1,14 +0,0 @@
package app
type Config struct {
Discord DiscordConfig `yaml:"discord"`
}
type DiscordConfig struct {
Token string `yaml:"token" env:"DISCORD_TOKEN"`
GuildID string `yaml:"guild_id" env:"DISCORD_GUILD_ID"`
EventCategory string `yaml:"event_category" env:"DISCORD_EVENT_CATEGORY"`
ArchiveCategory string `yaml:"archive_category" env:"DISCORD_ARCHIVE_CATEGORY"`
NotificationChannel string `yaml:"notification_channel" env:"DISCORD_NOTIFICATION_CHANNEL"`
}

View File

@ -1,86 +0,0 @@
package app
import (
"fmt"
"regexp"
"strings"
"time"
)
const REMOTE_LOCATION string = "online"
type Event struct {
Name string
Location string
DateTime time.Time
OrganizerID string
}
func (event *Event) GetChannelName() string {
month := event.GetMonthPrefix()
day := event.DateTime.Day()
city := event.GetCityFromLocation()
channel := fmt.Sprint(month, "-", day, city, "-", event.Name)
channel = strings.ReplaceAll(channel, " ", "-")
channel = strings.ToLower(channel)
re, _ := regexp.Compile(`[^\w\-]`)
channel = re.ReplaceAllString(channel, "")
return channel
}
func (event *Event) GetCityFromLocation() string {
if event.Location == REMOTE_LOCATION {
return REMOTE_LOCATION
}
parts := strings.Split(event.Location, " ")
index := -1
loc := event.Location
for i, v := range parts {
part := strings.ToLower(v)
if part == "mi" || part == "michigan" {
index = i - 1
if index < 0 {
return ""
}
if index > 0 && parts[index] == "," {
index -= 1
}
if index > 1 && strings.Contains(parts[index-2], ",") {
loc = fmt.Sprintf("%s-%s", parts[index-1], parts[index])
break
}
loc = parts[index]
break
}
}
return fmt.Sprint("-", loc)
}
func (event *Event) GetMonthPrefix() string {
month := event.DateTime.Month()
data := map[time.Month]string{
time.January: "jan",
time.February: "feb",
time.March: "march",
time.April: "april",
time.May: "may",
time.June: "june",
time.July: "july",
time.August: "aug",
time.September: "sept",
time.October: "oct",
time.November: "nov",
time.December: "dec",
}
return data[month]
}

View File

@ -1,27 +0,0 @@
package app
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetChannelName(t *testing.T) {
assert := assert.New(t)
event := Event{
Name: "Hello World",
Location: "1234 Place Rd, Ann Arbor, MI 00000",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-ann-arbor-hello-world", event.GetChannelName())
event = Event{
Name: "Hello World",
Location: "Michigan Theater, Ann Arbor",
DateTime: time.Date(2022, time.January, 5, 0, 0, 0, 0, time.UTC),
}
assert.Equal("jan-5-hello-world", event.GetChannelName())
}