fix: properly switch to a newly set custom account
continuous-integration/drone/tag Build is failing Details

This commit is contained in:
Ash Keel 2024-04-20 16:19:56 +02:00
parent fe02999663
commit dac126e891
No known key found for this signature in database
GPG Key ID: 53A9E9A6035DD109
1 changed files with 25 additions and 11 deletions

View File

@ -24,6 +24,7 @@ import (
type Module struct {
Config Config
streamerAPI *helix.Client
ctx context.Context
db database.Database
api *helix.Client
@ -40,21 +41,13 @@ type Module struct {
}
func Setup(ctx context.Context, db database.Database, api *helix.Client, user helix.User, logger *slog.Logger, templater template.Engine) *Module {
customUserClient, customUserInfo, err := GetCustomUser(db)
if err != nil {
if !errors.Is(err, database.ErrEmptyKey) {
logger.Error("Failed to get custom user, falling back to streamer account", log.Error(err))
}
customUserClient = api
customUserInfo = user
}
mod := &Module{
ctx: ctx,
db: db,
api: customUserClient,
streamerAPI: api,
api: api,
streamer: user,
user: customUserInfo,
user: user,
logger: logger,
templater: templater,
lastMessage: sync.NewRWSync(time.Now()),
@ -76,6 +69,14 @@ func Setup(ctx context.Context, db database.Database, api *helix.Client, user he
}
}
// Set custom user (and set hook to reload when it changes)
mod.setCustomUser()
if err := db.SubscribeKeyContext(ctx, CustomAccountKey, func(value string) {
mod.setCustomUser()
}); err != nil {
logger.Error("Could not subscribe to custom account changes", log.Error(err))
}
if err := db.SubscribeKeyContext(ctx, eventsub.EventKeyPrefix+helix.EventSubTypeChannelChatMessage, mod.onChatMessage); err != nil {
logger.Error("Could not subscribe to chat messages", log.Error(err))
}
@ -106,6 +107,19 @@ func Setup(ctx context.Context, db database.Database, api *helix.Client, user he
return mod
}
func (mod *Module) setCustomUser() {
customUserClient, customUserInfo, err := GetCustomUser(mod.db)
if err != nil {
if !errors.Is(err, database.ErrEmptyKey) {
mod.logger.Error("Failed to get custom user, falling back to streamer account", log.Error(err))
}
customUserClient = mod.streamerAPI
customUserInfo = mod.streamer
}
mod.api = customUserClient
mod.user = customUserInfo
}
func (mod *Module) onChatMessage(newValue string) {
var chatMessage struct {
Event helix.EventSubChannelChatMessageEvent `json:"event"`