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 was merged in pull request #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),
}
}