Recieving messages via the external link

This commit is contained in:
2023-06-17 22:27:14 +00:00
parent b252d5e62e
commit 77d41bb945
11 changed files with 164 additions and 15 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/yeslayla/birdbot/core"
"github.com/yeslayla/birdbot/discord"
"github.com/yeslayla/birdbot/mastodon"
"github.com/yeslayla/birdbot/persistence"
)
var Version string
@ -17,6 +18,8 @@ type Bot struct {
Session *discord.Discord
Mastodon *mastodon.Mastodon
Database persistence.Database
// Discord Objects
guildID string
eventCategoryID string
@ -31,7 +34,7 @@ type Bot struct {
onEventUpdatedHandlers [](func(common.Event) error)
onEventCompletedHandlers [](func(common.Event) error)
gameModules []common.ChatSyncModule
channelChats map[string][]common.ExternalChatModule
}
// Initalize creates the discord session and registers handlers
@ -57,7 +60,7 @@ func (app *Bot) Initialize(cfg *core.Config) error {
cfg.Mastodon.Username, cfg.Mastodon.Password)
}
app.Session = discord.New(cfg.Discord.ApplicationID, app.guildID, cfg.Discord.Token)
app.Session = discord.New(cfg.Discord.ApplicationID, app.guildID, cfg.Discord.Token, app.Database)
// Register Event Handlers
app.Session.OnReady(app.onReady)
@ -65,6 +68,10 @@ func (app *Bot) Initialize(cfg *core.Config) error {
app.Session.OnEventDelete(app.onEventDelete)
app.Session.OnEventUpdate(app.onEventUpdate)
if len(app.channelChats) > 0 {
app.Session.OnMessageRecieved(app.onMessageRecieved)
}
return nil
}
@ -160,7 +167,20 @@ func (app *Bot) onEventComplete(d *discord.Discord, event common.Event) {
}
// NewBot creates a new bot instance
func NewBot() *Bot {
return &Bot{}
func (app *Bot) onMessageRecieved(d *discord.Discord, channel string, user common.User, message string) {
chats, ok := app.channelChats[channel]
if !ok {
return
}
for _, chat := range chats {
chat.RecieveMessage(user, message)
}
}
// NewBot creates a new bot instance
func NewBot(db persistence.Database) *Bot {
return &Bot{
Database: db,
}
}

View File

@ -50,7 +50,7 @@ func (loader *ComponentLoader) OnEventComplete(handler func(common.Event) error)
return nil
}
func (loader *ComponentLoader) RegisterChatSyncModule(ID string, plugin common.ChatSyncModule) error {
func (loader *ComponentLoader) RegisterExternalChat(ID string, plugin common.ExternalChatModule) error {
return fmt.Errorf("unimplemented")
}