Major Refactor (#2)
* Major reworks * More refactoring * Refactor feature complete! * Comments * Add versioning
This commit is contained in:
7
core/channel.go
Normal file
7
core/channel.go
Normal file
@ -0,0 +1,7 @@
|
||||
package core
|
||||
|
||||
type Channel struct {
|
||||
Name string
|
||||
ID string
|
||||
Verified bool
|
||||
}
|
14
core/configuration.go
Normal file
14
core/configuration.go
Normal file
@ -0,0 +1,14 @@
|
||||
package core
|
||||
|
||||
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"`
|
||||
}
|
95
core/event.go
Normal file
95
core/event.go
Normal file
@ -0,0 +1,95 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const REMOTE_LOCATION string = "online"
|
||||
|
||||
type Event struct {
|
||||
Name string
|
||||
ID string
|
||||
Location string
|
||||
Completed bool
|
||||
DateTime time.Time
|
||||
|
||||
Organizer *User
|
||||
}
|
||||
|
||||
// Channel returns a channel object associated with an event
|
||||
func (event *Event) Channel() *Channel {
|
||||
|
||||
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{
|
||||
Name: channel,
|
||||
Verified: false,
|
||||
}
|
||||
}
|
||||
|
||||
// GetCityFromLocation returns the city name of an event's location
|
||||
func (event *Event) GetCityFromLocation() string {
|
||||
|
||||
if event.Location == REMOTE_LOCATION {
|
||||
return fmt.Sprint("-", 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)
|
||||
}
|
||||
|
||||
// GetMonthPrefix returns a month in short form
|
||||
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]
|
||||
}
|
27
core/event_test.go
Normal file
27
core/event_test.go
Normal file
@ -0,0 +1,27 @@
|
||||
package core
|
||||
|
||||
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.Channel().Name)
|
||||
|
||||
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.Channel().Name)
|
||||
|
||||
}
|
16
core/organizer.go
Normal file
16
core/organizer.go
Normal file
@ -0,0 +1,16 @@
|
||||
package core
|
||||
|
||||
import "fmt"
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
}
|
||||
|
||||
// Mention generated a Discord mention string for the user
|
||||
func (user *User) Mention() string {
|
||||
if user == nil {
|
||||
return "<NULL>"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("<@%s>", user.ID)
|
||||
}
|
6
core/pointers.go
Normal file
6
core/pointers.go
Normal file
@ -0,0 +1,6 @@
|
||||
package core
|
||||
|
||||
// Bool returns a pointer to a bool
|
||||
func Bool(v bool) *bool {
|
||||
return &v
|
||||
}
|
Reference in New Issue
Block a user