External Chat Linking (!4)

This PR adds the functionality for plugins to send and recieve messages linked to a specific channel.

Co-authored-by: Layla <layla@layla.gg>
Reviewed-on: https://gitea.sumulayla.synology.me/layla/birdbot/pulls/4
This commit is contained in:
2023-06-17 19:38:47 -04:00
parent b252d5e62e
commit 73a63fbf4d
12 changed files with 207 additions and 17 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,15 @@ 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)
// 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)
@ -65,6 +76,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 +175,21 @@ 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, channelID string, user common.User, message string) {
chats, ok := app.channelChats[channelID]
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,
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) RegisterChatSyncModule(ID string, plugin common.ChatSyncModule) 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)
}