Remove Common, Rework External Chat Support, & Bugfixes (!5)
Co-authored-by: Layla <layla@layla.gg> Reviewed-on: https://gitea.sumulayla.synology.me/layla/birdbot/pulls/5
This commit is contained in:
51
app/bot.go
51
app/bot.go
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/yeslayla/birdbot/common"
|
||||
"github.com/yeslayla/birdbot-common/common"
|
||||
"github.com/yeslayla/birdbot/core"
|
||||
"github.com/yeslayla/birdbot/discord"
|
||||
"github.com/yeslayla/birdbot/mastodon"
|
||||
@ -34,6 +34,8 @@ type Bot struct {
|
||||
onEventUpdatedHandlers [](func(common.Event) error)
|
||||
onEventCompletedHandlers [](func(common.Event) error)
|
||||
|
||||
chatLinks map[string][]string
|
||||
chatHandlers map[string]common.ExternalChatModule
|
||||
channelChats map[string][]common.ExternalChatModule
|
||||
}
|
||||
|
||||
@ -45,6 +47,7 @@ func (app *Bot) Initialize(cfg *core.Config) error {
|
||||
app.eventCategoryID = cfg.Discord.EventCategory
|
||||
app.archiveCategoryID = cfg.Discord.ArchiveCategory
|
||||
app.notificationChannelID = cfg.Discord.NotificationChannel
|
||||
app.chatLinks = cfg.Discord.ChatLinks
|
||||
|
||||
if app.guildID == "" {
|
||||
return fmt.Errorf("discord Guild ID is not set")
|
||||
@ -62,29 +65,21 @@ func (app *Bot) Initialize(cfg *core.Config) error {
|
||||
|
||||
app.Session = discord.New(cfg.Discord.ApplicationID, app.guildID, cfg.Discord.Token, app.Database)
|
||||
|
||||
// Intialize submodules
|
||||
for channelID, chats := range app.channelChats {
|
||||
channel := app.Session.NewChannelFromID(channelID)
|
||||
for _, chat := range chats {
|
||||
app.InitalizeExternalChat(channel, chat)
|
||||
}
|
||||
}
|
||||
|
||||
// Register Event Handlers
|
||||
app.Session.OnReady(app.onReady)
|
||||
app.Session.OnEventCreate(app.onEventCreate)
|
||||
app.Session.OnEventDelete(app.onEventDelete)
|
||||
app.Session.OnEventUpdate(app.onEventUpdate)
|
||||
|
||||
if len(app.channelChats) > 0 {
|
||||
app.Session.OnMessageRecieved(app.onMessageRecieved)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run opens the session with Discord until exit
|
||||
func (app *Bot) Run() error {
|
||||
|
||||
// Intialize submodules
|
||||
app.prepareChat()
|
||||
|
||||
return app.Session.Run()
|
||||
}
|
||||
|
||||
@ -186,10 +181,40 @@ func (app *Bot) onMessageRecieved(d *discord.Discord, channelID string, user com
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// 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),
|
||||
}
|
||||
}
|
||||
|
@ -2,17 +2,20 @@ package app
|
||||
|
||||
import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/yeslayla/birdbot/common"
|
||||
"github.com/yeslayla/birdbot-common/common"
|
||||
)
|
||||
|
||||
type ComponentLoader struct {
|
||||
bot *Bot
|
||||
bot *Bot
|
||||
configDir string
|
||||
}
|
||||
|
||||
func NewComponentLoader(bot *Bot) *ComponentLoader {
|
||||
func NewComponentLoader(bot *Bot, configDir string) *ComponentLoader {
|
||||
return &ComponentLoader{
|
||||
bot: bot,
|
||||
bot: bot,
|
||||
configDir: configDir,
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,13 +52,8 @@ func (loader *ComponentLoader) OnEventComplete(handler func(common.Event) error)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (loader *ComponentLoader) RegisterExternalChat(channelID string, chat common.ExternalChatModule) error {
|
||||
if _, ok := loader.bot.channelChats[channelID]; !ok {
|
||||
loader.bot.channelChats[channelID] = []common.ExternalChatModule{}
|
||||
}
|
||||
|
||||
loader.bot.channelChats[channelID] = append(loader.bot.channelChats[channelID], chat)
|
||||
|
||||
func (loader *ComponentLoader) RegisterExternalChat(ID string, chat common.ExternalChatModule) error {
|
||||
loader.bot.chatHandlers[ID] = chat
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -71,3 +69,7 @@ func (loader *ComponentLoader) Notify(message string) error {
|
||||
func (loader *ComponentLoader) RegisterCommand(name string, config common.ChatCommandConfiguration, handler func(common.User, map[string]any) string) {
|
||||
loader.bot.Session.RegisterCommand(name, config, handler)
|
||||
}
|
||||
|
||||
func (loader *ComponentLoader) GetConfigPath(fileName string) string {
|
||||
return filepath.Join(loader.configDir, "birdbot", fileName)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/yeslayla/birdbot/common"
|
||||
"github.com/yeslayla/birdbot-common/common"
|
||||
"github.com/yeslayla/birdbot/core"
|
||||
)
|
||||
|
||||
|
@ -3,9 +3,10 @@ package app
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
|
||||
"github.com/yeslayla/birdbot/common"
|
||||
"github.com/yeslayla/birdbot-common/common"
|
||||
)
|
||||
|
||||
// LoadPlugin loads a plugin and returns its component if successful
|
||||
@ -49,7 +50,7 @@ func LoadPlugins(directory string) []common.Module {
|
||||
continue
|
||||
}
|
||||
|
||||
if comp := LoadPlugin(path.Name()); comp != nil {
|
||||
if comp := LoadPlugin(filepath.Join(directory, path.Name())); comp != nil {
|
||||
components = append(components, comp)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user