birdbot/app/bot.go

169 lines
3.9 KiB
Go
Raw Normal View History

2022-10-27 01:55:23 +00:00
package app
import (
"fmt"
"log"
2023-03-30 23:51:05 -04:00
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
2022-11-04 06:45:20 +00:00
"github.com/yeslayla/birdbot/mastodon"
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 {
2023-03-30 23:51:05 -04:00
Session *discord.Discord
Mastodon *mastodon.Mastodon
2022-10-27 01:55:23 +00:00
// Discord Objects
guildID string
eventCategoryID string
archiveCategoryID string
notificationChannelID string
2023-03-30 23:51:05 -04:00
onReadyHandlers [](func() error)
onNotifyHandlers [](func(string) error)
onEventCreatedHandlers [](func(common.Event) error)
onEventDeletedHandlers [](func(common.Event) error)
onEventUpdatedHandlers [](func(common.Event) error)
onEventCompletedHandlers [](func(common.Event) error)
gameModules []common.ChatSyncModule
2022-10-27 01:55:23 +00:00
}
// Initalize creates the discord session and registers handlers
2023-03-30 23:51:05 -04:00
func (app *Bot) Initialize(cfg *core.Config) error {
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")
}
2022-11-04 06:45:20 +00:00
if cfg.Mastodon.ClientID != "" && cfg.Mastodon.ClientSecret != "" &&
cfg.Mastodon.Username != "" && cfg.Mastodon.Password != "" &&
cfg.Mastodon.Server != "" {
2023-03-30 23:51:05 -04:00
app.Mastodon = mastodon.NewMastodon(cfg.Mastodon.Server, cfg.Mastodon.ClientID, cfg.Mastodon.ClientSecret,
2022-11-04 06:45:20 +00:00
cfg.Mastodon.Username, cfg.Mastodon.Password)
}
2023-03-30 23:51:05 -04:00
app.Session = discord.New(app.guildID, cfg.Discord.Token)
2022-10-27 01:55:23 +00:00
// Register Event Handlers
2023-03-30 23:51:05 -04:00
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
btn := app.Session.NewButton("test", "Click Me")
btn.OnClick(func(user common.User) {
print("clicked")
})
2022-10-27 01:55:23 +00:00
return nil
}
// Run opens the session with Discord until exit
func (app *Bot) Run() error {
2023-03-30 23:51:05 -04:00
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...")
2023-03-30 23:51:05 -04:00
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)
2023-03-30 23:51:05 -04:00
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
2023-03-30 23:51:05 -04: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
}
2023-03-30 23:51:05 -04:00
for _, handler := range app.onNotifyHandlers {
if err := handler(message); err != nil {
log.Println(err)
}
}
2022-10-27 01:55:23 +00:00
}
2023-03-30 23:51:05 -04:00
func (app *Bot) onReady(d *discord.Discord) {
app.Session.SetStatus(fmt.Sprintf("with fire! (%s)", Version))
2022-10-27 01:55:23 +00:00
2023-03-30 23:51:05 -04:00
for _, handler := range app.onReadyHandlers {
if err := handler(); err != nil {
log.Println(err)
2022-10-27 01:55:23 +00:00
}
}
2023-03-30 23:51:05 -04:00
}
2022-10-27 01:55:23 +00:00
2023-03-30 23:51:05 -04:00
func (app *Bot) onEventCreate(d *discord.Discord, event common.Event) {
2022-11-04 06:45:20 +00:00
2023-03-30 23:51:05 -04:00
log.Print("Event Created: '", event.Name, "':'", event.Location, "'")
for _, handler := range app.onEventCreatedHandlers {
if err := handler(event); err != nil {
log.Println(err)
2022-11-04 06:45:20 +00:00
}
}
2023-03-30 23:51:05 -04:00
2022-10-27 01:55:23 +00:00
}
2023-03-30 23:51:05 -04:00
func (app *Bot) onEventDelete(d *discord.Discord, event common.Event) {
2022-10-27 01:55:23 +00:00
2023-03-30 23:51:05 -04:00
for _, handler := range app.onEventDeletedHandlers {
if err := handler(event); err != nil {
log.Println(err)
}
2022-10-27 01:55:23 +00:00
}
2023-03-30 23:51:05 -04:00
}
func (app *Bot) onEventUpdate(d *discord.Discord, event common.Event) {
2022-11-04 06:45:20 +00:00
2023-03-30 23:51:05 -04:00
for _, handler := range app.onEventUpdatedHandlers {
if err := handler(event); err != nil {
log.Println(err)
2022-11-04 06:45:20 +00:00
}
}
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
}
}
2023-03-30 23:51:05 -04:00
func (app *Bot) onEventComplete(d *discord.Discord, event common.Event) {
2022-10-27 01:55:23 +00:00
2023-03-30 23:51:05 -04:00
for _, handler := range app.onEventCompletedHandlers {
if err := handler(event); err != nil {
log.Println(err)
2022-10-27 01:55:23 +00:00
}
}
2022-10-27 01:55:23 +00:00
}
2023-03-31 20:49:50 +00:00
// NewBot creates a new bot instance
2022-10-27 01:55:23 +00:00
func NewBot() *Bot {
return &Bot{}
}