birdbot/app/bot.go

221 lines
5.4 KiB
Go
Raw Normal View History

2022-10-27 03:55:23 +02:00
package app
import (
"fmt"
"log"
"github.com/yeslayla/birdbot-common/common"
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
2022-11-04 07:45:20 +01:00
"github.com/yeslayla/birdbot/mastodon"
"github.com/yeslayla/birdbot/persistence"
2022-10-27 03:55:23 +02:00
)
var Version string
var Build string
2022-10-27 03:55:23 +02:00
type Bot struct {
2023-03-31 05:51:05 +02:00
Session *discord.Discord
Mastodon *mastodon.Mastodon
2022-10-27 03:55:23 +02:00
Database persistence.Database
2022-10-27 03:55:23 +02:00
// Discord Objects
guildID string
eventCategoryID string
archiveCategoryID string
notificationChannelID string
2023-03-31 05:51:05 +02: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)
chatLinks map[string][]string
chatHandlers map[string]common.ExternalChatModule
channelChats map[string][]common.ExternalChatModule
2022-10-27 03:55:23 +02:00
}
// Initalize creates the discord session and registers handlers
2023-03-31 05:51:05 +02:00
func (app *Bot) Initialize(cfg *core.Config) error {
2022-10-27 03:55:23 +02: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
app.chatLinks = cfg.Discord.ChatLinks
2022-10-27 03:55:23 +02:00
if app.guildID == "" {
return fmt.Errorf("discord Guild ID is not set")
}
if cfg.Discord.ApplicationID == "" {
return fmt.Errorf("discord Application ID is not set")
}
2022-10-27 03:55:23 +02:00
2022-11-04 07:45:20 +01:00
if cfg.Mastodon.ClientID != "" && cfg.Mastodon.ClientSecret != "" &&
cfg.Mastodon.Username != "" && cfg.Mastodon.Password != "" &&
cfg.Mastodon.Server != "" {
2023-03-31 05:51:05 +02:00
app.Mastodon = mastodon.NewMastodon(cfg.Mastodon.Server, cfg.Mastodon.ClientID, cfg.Mastodon.ClientSecret,
2022-11-04 07:45:20 +01:00
cfg.Mastodon.Username, cfg.Mastodon.Password)
}
app.Session = discord.New(cfg.Discord.ApplicationID, app.guildID, cfg.Discord.Token, app.Database)
2022-10-27 03:55:23 +02:00
// Register Event Handlers
2023-03-31 05:51:05 +02:00
app.Session.OnReady(app.onReady)
app.Session.OnEventCreate(app.onEventCreate)
app.Session.OnEventDelete(app.onEventDelete)
app.Session.OnEventUpdate(app.onEventUpdate)
2022-10-27 03:55:23 +02:00
return nil
}
// Run opens the session with Discord until exit
func (app *Bot) Run() error {
// Intialize submodules
app.prepareChat()
2023-03-31 05:51:05 +02:00
return app.Session.Run()
2022-10-27 03:55:23 +02:00
}
// Stop triggers a graceful shutdown of the app
func (app *Bot) Stop() {
log.Print("Shuting down...")
2023-03-31 05:51:05 +02:00
app.Session.Stop()
2022-10-27 03:55:23 +02:00
}
// Notify sends a message to the notification channe;
func (app *Bot) Notify(message string) {
if app.notificationChannelID == "" {
log.Println(message)
2022-10-27 03:55:23 +02:00
return
}
log.Print("Notification: ", message)
2023-03-31 05:51:05 +02: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 03:55:23 +02:00
2023-03-31 05:51:05 +02:00
err := app.Session.SendMessage(channel, message)
2022-10-27 03:55:23 +02:00
if err != nil {
log.Print("Failed notification: ", err)
2022-10-27 03:55:23 +02:00
}
2023-03-31 05:51:05 +02:00
for _, handler := range app.onNotifyHandlers {
if err := handler(message); err != nil {
log.Println(err)
}
}
2022-10-27 03:55:23 +02:00
}
2023-03-31 05:51:05 +02:00
func (app *Bot) onReady(d *discord.Discord) {
app.Session.SetStatus(fmt.Sprintf("with fire! (%s)", Version))
2022-10-27 03:55:23 +02:00
2023-03-31 05:51:05 +02:00
for _, handler := range app.onReadyHandlers {
if err := handler(); err != nil {
log.Println(err)
2022-10-27 03:55:23 +02:00
}
}
2023-03-31 05:51:05 +02:00
}
2022-10-27 03:55:23 +02:00
2023-03-31 05:51:05 +02:00
func (app *Bot) onEventCreate(d *discord.Discord, event common.Event) {
2022-11-04 07:45:20 +01:00
2023-03-31 05:51:05 +02: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 07:45:20 +01:00
}
}
2023-03-31 05:51:05 +02:00
2022-10-27 03:55:23 +02:00
}
2023-03-31 05:51:05 +02:00
func (app *Bot) onEventDelete(d *discord.Discord, event common.Event) {
2022-10-27 03:55:23 +02:00
2023-03-31 05:51:05 +02:00
for _, handler := range app.onEventDeletedHandlers {
if err := handler(event); err != nil {
log.Println(err)
}
2022-10-27 03:55:23 +02:00
}
2023-03-31 05:51:05 +02:00
}
func (app *Bot) onEventUpdate(d *discord.Discord, event common.Event) {
2022-11-04 07:45:20 +01:00
2023-03-31 05:51:05 +02:00
for _, handler := range app.onEventUpdatedHandlers {
if err := handler(event); err != nil {
log.Println(err)
2022-11-04 07:45:20 +01:00
}
}
2022-10-27 03:55:23 +02:00
// Pass event onwards
if event.Completed {
app.onEventComplete(d, event)
2022-10-27 03:55:23 +02:00
}
}
2023-03-31 05:51:05 +02:00
func (app *Bot) onEventComplete(d *discord.Discord, event common.Event) {
2022-10-27 03:55:23 +02:00
2023-03-31 05:51:05 +02:00
for _, handler := range app.onEventCompletedHandlers {
if err := handler(event); err != nil {
log.Println(err)
2022-10-27 03:55:23 +02:00
}
}
2022-10-27 03:55:23 +02:00
}
func (app *Bot) onMessageRecieved(d *discord.Discord, channelID string, user common.User, message string) {
chats, ok := app.channelChats[channelID]
if !ok {
return
}
for _, chat := range chats {
chat.RecieveMessage(user, message)
}
}
func (app *Bot) prepareChat() {
// Associate channels with chat modules
for channelID, chatHandelerIDs := range app.chatLinks {
if _, ok := app.channelChats[channelID]; !ok {
app.channelChats[channelID] = []common.ExternalChatModule{}
}
for _, chatHandlerID := range chatHandelerIDs {
if handler, ok := app.chatHandlers[chatHandlerID]; ok {
app.channelChats[channelID] = append(app.channelChats[channelID], handler)
}
}
}
// Initialize chat modules
for channelID, chats := range app.channelChats {
channel := app.Session.NewChannelFromID(channelID)
for _, chat := range chats {
app.InitalizeExternalChat(channel, chat)
}
}
// Register listener if needed
if len(app.channelChats) > 0 {
app.Session.OnMessageRecieved(app.onMessageRecieved)
}
}
2023-03-31 22:49:50 +02:00
// NewBot creates a new bot instance
func NewBot(db persistence.Database) *Bot {
return &Bot{
Database: db,
channelChats: make(map[string][]common.ExternalChatModule),
chatHandlers: make(map[string]common.ExternalChatModule),
}
2022-10-27 03:55:23 +02:00
}