Allow for sending chats out from external chat submodule

This commit is contained in:
Layla 2023-06-17 23:33:35 +00:00
parent 77d41bb945
commit be07d4450a
4 changed files with 48 additions and 7 deletions

View File

@ -62,6 +62,14 @@ 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)
@ -167,8 +175,8 @@ func (app *Bot) onEventComplete(d *discord.Discord, event common.Event) {
}
func (app *Bot) onMessageRecieved(d *discord.Discord, channel string, user common.User, message string) {
chats, ok := app.channelChats[channel]
func (app *Bot) onMessageRecieved(d *discord.Discord, channelID string, user common.User, message string) {
chats, ok := app.channelChats[channelID]
if !ok {
return
}
@ -181,6 +189,7 @@ func (app *Bot) onMessageRecieved(d *discord.Discord, channel string, user commo
// NewBot creates a new bot instance
func NewBot(db persistence.Database) *Bot {
return &Bot{
Database: db,
Database: db,
channelChats: make(map[string][]common.ExternalChatModule),
}
}

View File

@ -1,7 +1,6 @@
package app
import (
"fmt"
"log"
"github.com/yeslayla/birdbot/common"
@ -50,8 +49,14 @@ func (loader *ComponentLoader) OnEventComplete(handler func(common.Event) error)
return nil
}
func (loader *ComponentLoader) RegisterExternalChat(ID string, plugin common.ExternalChatModule) error {
return fmt.Errorf("unimplemented")
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)
return nil
}
func (loader *ComponentLoader) CreateEvent(event common.Event) error {

View File

@ -0,0 +1,26 @@
package app
import (
"github.com/yeslayla/birdbot/common"
"github.com/yeslayla/birdbot/core"
)
type ExternalChatManager struct {
chat common.ExternalChatModule
channel *core.Channel
bot *Bot
}
func (manager *ExternalChatManager) SendMessage(user string, message string) {
manager.bot.Session.WebhookSendMessage(manager.channel, user, message)
}
func (app *Bot) InitalizeExternalChat(channel *core.Channel, chat common.ExternalChatModule) {
manager := &ExternalChatManager{
channel: channel,
chat: chat,
bot: app,
}
manager.chat.Initialize(manager)
}

View File

@ -24,5 +24,6 @@ type ModuleManager interface {
// Commands
RegisterCommand(string, ChatCommandConfiguration, func(User, map[string]any) string)
RegisterExternalChat(ID string, chat ExternalChatModule) error
// Submodules
RegisterExternalChat(channelID string, chat ExternalChatModule) error
}