birdbot/app/bot.go

169 lines
4.2 KiB
Go
Raw Normal View History

2022-10-27 01:55:23 +00:00
package app
import (
2022-10-27 02:39:22 +00:00
"errors"
2022-10-27 01:55:23 +00:00
"fmt"
"log"
"os"
"github.com/ilyakaznacheev/cleanenv"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
2022-10-27 01:55:23 +00:00
)
var Version string
var Build string
2022-10-27 01:55:23 +00:00
type Bot struct {
session *discord.Discord
2022-10-27 01:55:23 +00:00
// Discord Objects
guildID string
eventCategoryID string
archiveCategoryID string
notificationChannelID string
}
// Initalize creates the discord session and registers handlers
func (app *Bot) Initialize(config_path string) error {
log.Printf("Using config: %s", config_path)
cfg := &core.Config{}
2022-10-27 01:55:23 +00:00
2022-10-27 02:39:22 +00:00
_, err := os.Stat(config_path)
if errors.Is(err, os.ErrNotExist) {
log.Printf("Config file not found: '%s'", config_path)
2022-10-27 03:17:34 +00:00
err := cleanenv.ReadEnv(cfg)
2022-10-27 02:39:22 +00:00
if err != nil {
2022-10-27 03:17:34 +00:00
return err
2022-10-27 02:39:22 +00:00
}
} else {
err := cleanenv.ReadConfig(config_path, cfg)
if err != nil {
return err
}
}
2022-10-27 01:55:23 +00:00
// 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")
}
app.session = discord.New(app.guildID, cfg.Discord.Token)
2022-10-27 01:55:23 +00:00
// Register Event Handlers
app.session.OnReady(app.onReady)
app.session.OnEventCreate(app.onEventCreate)
app.session.OnEventDelete(app.onEventDelete)
app.session.OnEventUpdate(app.onEventUpdate)
2022-10-27 01:55:23 +00:00
return nil
}
// Run opens the session with Discord until exit
func (app *Bot) Run() error {
return app.session.Run()
2022-10-27 01:55:23 +00:00
}
// Stop triggers a graceful shutdown of the app
func (app *Bot) Stop() {
log.Print("Shuting down...")
app.session.Stop()
2022-10-27 01:55:23 +00:00
}
// Notify sends a message to the notification channe;
func (app *Bot) Notify(message string) {
if app.notificationChannelID == "" {
log.Println(message)
2022-10-27 01:55:23 +00:00
return
}
log.Print("Notification: ", message)
channel := app.session.NewChannelFromID(app.notificationChannelID)
if channel == nil {
log.Printf("Failed notification: channel was not found with ID '%v'", app.notificationChannelID)
}
2022-10-27 01:55:23 +00:00
err := app.session.SendMessage(channel, message)
2022-10-27 01:55:23 +00:00
if err != nil {
log.Print("Failed notification: ", err)
2022-10-27 01:55:23 +00:00
}
}
func (app *Bot) onReady(d *discord.Discord) {
app.Notify(fmt.Sprintf("BirdBot %s is ready!", Version))
2022-10-27 01:55:23 +00:00
}
func (app *Bot) onEventCreate(d *discord.Discord, event *core.Event) {
2022-10-27 03:36:56 +00:00
2022-10-27 01:55:23 +00:00
log.Print("Event Created: '", event.Name, "':'", event.Location, "'")
channel, err := app.session.NewChannelFromName(event.Channel().Name)
2022-10-27 01:55:23 +00:00
if err != nil {
log.Print("Failed to create channel for event: ", err)
}
if app.eventCategoryID != "" {
err = app.session.MoveChannelToCategory(channel, app.eventCategoryID)
if err != nil {
2022-10-27 01:55:23 +00:00
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, event.ID)
app.Notify(fmt.Sprintf("%s is organizing an event '%s': %s", event.Organizer.Mention(), event.Name, eventURL))
2022-10-27 01:55:23 +00:00
}
func (app *Bot) onEventDelete(d *discord.Discord, event *core.Event) {
2022-10-27 01:55:23 +00:00
_, err := app.session.DeleteChannel(event.Channel())
2022-10-27 01:55:23 +00:00
if err != nil {
log.Print("Failed to create channel for event: ", err)
}
app.Notify(fmt.Sprintf("%s cancelled '%s' on %s, %d!", event.Organizer.Mention(), event.Name, event.DateTime.Month().String(), event.DateTime.Day()))
2022-10-27 01:55:23 +00:00
}
func (app *Bot) onEventUpdate(d *discord.Discord, event *core.Event) {
2022-10-27 01:55:23 +00:00
// Pass event onwards
if event.Completed {
app.onEventComplete(d, event)
2022-10-27 01:55:23 +00:00
}
}
func (app *Bot) onEventComplete(d *discord.Discord, event *core.Event) {
2022-10-27 03:36:56 +00:00
channel := event.Channel()
2022-10-27 01:55:23 +00:00
if app.archiveCategoryID != "" {
if err := app.session.MoveChannelToCategory(channel, app.archiveCategoryID); err != nil {
log.Print("Failed to move channel to archive category: ", err)
2022-10-27 01:55:23 +00:00
}
if err := app.session.ArchiveChannel(channel); err != nil {
log.Print("Failed to archive channel: ", err)
2022-10-27 01:55:23 +00:00
}
log.Printf("Archived channel: '%s'", channel.Name)
2022-10-27 01:55:23 +00:00
} else {
// Delete Channel
_, err := app.session.DeleteChannel(channel)
2022-10-27 01:55:23 +00:00
if err != nil {
log.Print("Failed to delete channel: ", err)
}
log.Printf("Deleted channel: '%s'", channel.Name)
2022-10-27 01:55:23 +00:00
}
}
func NewBot() *Bot {
return &Bot{}
}