Initial commit
This commit is contained in:
205
app/bot.go
Normal file
205
app/bot.go
Normal file
@ -0,0 +1,205 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
discord *discordgo.Session
|
||||
|
||||
// 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{}
|
||||
err := cleanenv.ReadConfig(config_path, cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Load directly from config
|
||||
app.guildID = cfg.Discord.GuildID
|
||||
app.eventCategoryID = cfg.Discord.EventCategory
|
||||
app.archiveCategoryID = cfg.Discord.ArchiveCategory
|
||||
app.notificationChannelID = cfg.Discord.NotificationChannel
|
||||
|
||||
if app.guildID == "" {
|
||||
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)
|
||||
}
|
||||
|
||||
// Register Event Handlers
|
||||
app.discord.AddHandler(app.onReady)
|
||||
app.discord.AddHandler(app.onEventCreate)
|
||||
app.discord.AddHandler(app.onEventDelete)
|
||||
app.discord.AddHandler(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)
|
||||
signal.Notify(app.stop, os.Interrupt)
|
||||
<-app.stop
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop triggers a graceful shutdown of the app
|
||||
func (app *Bot) Stop() {
|
||||
log.Print("Shuting down...")
|
||||
app.stop <- os.Kill
|
||||
}
|
||||
|
||||
// Notify sends a message to the notification channe;
|
||||
func (app *Bot) Notify(message string) {
|
||||
if app.notificationChannelID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
_, err := app.discord.ChannelMessageSend(app.notificationChannelID, message)
|
||||
|
||||
log.Println("Notification: ", message)
|
||||
if err != nil {
|
||||
log.Println("Failed notification: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Bot) onReady(s *discordgo.Session, r *discordgo.Ready) {
|
||||
log.Print("BirdBot is ready!")
|
||||
}
|
||||
|
||||
func (app *Bot) onEventCreate(s *discordgo.Session, r *discordgo.GuildScheduledEventCreate) {
|
||||
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())
|
||||
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 {
|
||||
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))
|
||||
}
|
||||
|
||||
func (app *Bot) onEventDelete(s *discordgo.Session, r *discordgo.GuildScheduledEventDelete) {
|
||||
|
||||
// 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())
|
||||
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()))
|
||||
}
|
||||
|
||||
func (app *Bot) onEventUpdate(s *discordgo.Session, r *discordgo.GuildScheduledEventUpdate) {
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Pass event onwards
|
||||
switch r.Status {
|
||||
case discordgo.GuildScheduledEventStatusCompleted:
|
||||
app.onEventComplete(s, event)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Bot) onEventComplete(s *discordgo.Session, event *Event) {
|
||||
channel_name := event.GetChannelName()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
log.Printf("Archived channel: '%s'", channel_name)
|
||||
} else {
|
||||
|
||||
// Delete Channel
|
||||
_, err := DeleteChannel(s, app.guildID, channel_name)
|
||||
if err != nil {
|
||||
log.Print("Failed to delete channel: ", err)
|
||||
}
|
||||
|
||||
log.Printf("Deleted channel: '%s'", channel_name)
|
||||
}
|
||||
}
|
||||
|
||||
func NewBot() *Bot {
|
||||
return &Bot{}
|
||||
}
|
68
app/channel.go
Normal file
68
app/channel.go
Normal file
@ -0,0 +1,68 @@
|
||||
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)
|
||||
}
|
14
app/configuration.go
Normal file
14
app/configuration.go
Normal file
@ -0,0 +1,14 @@
|
||||
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"`
|
||||
ArchiveCategory string `yaml:"archive_category"`
|
||||
NotificationChannel string `yaml:"notification_channel"`
|
||||
}
|
86
app/event.go
Normal file
86
app/event.go
Normal file
@ -0,0 +1,86 @@
|
||||
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() - 1
|
||||
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 "unknown"
|
||||
}
|
||||
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 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]
|
||||
}
|
Reference in New Issue
Block a user