1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-20 02:00:49 +00:00

fix: rework the alert templates so it doesn't break with multiple messages

This commit is contained in:
Ash Keel 2023-03-31 11:03:01 +02:00
parent 8178366038
commit 0eb6000975
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 87 additions and 103 deletions

View file

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The "Give points" dialog will now convert mixed case names to be lowercase instead of adding broken entries. - The "Give points" dialog will now convert mixed case names to be lowercase instead of adding broken entries.
- The loyalty points entry were assigned incorrectly upon startup, this has been fixed - The loyalty points entry were assigned incorrectly upon startup, this has been fixed
- Fixed having multiple messages for an alert use non-compiled templates
## [3.1.3] - 2023-03-14 ## [3.1.3] - 2023-03-14

View file

@ -64,33 +64,26 @@ type BotAlertsConfig struct {
} `json:"cheer"` } `json:"cheer"`
} }
type templateCache struct { type (
follow struct { templateCache map[string]*template.Template
messages map[int]*template.Template templateCacheMap map[templateType]templateCache
} )
subscription struct {
messages map[int]*template.Template type templateType string
variations map[int]map[int]*template.Template
} const (
gift struct { templateTypeSubscription templateType = "subscription"
messages map[int]*template.Template templateTypeFollow templateType = "follow"
variations map[int]map[int]*template.Template templateTypeRaid templateType = "raid"
} templateTypeCheer templateType = "cheer"
raid struct { templateTypeGift templateType = "gift"
messages map[int]*template.Template )
variations map[int]map[int]*template.Template
}
cheer struct {
messages map[int]*template.Template
variations map[int]map[int]*template.Template
}
}
type BotAlertsModule struct { type BotAlertsModule struct {
Config BotAlertsConfig Config BotAlertsConfig
bot *Bot bot *Bot
templates templateCache templates templateCacheMap
cancelAlertSub database.CancelFunc cancelAlertSub database.CancelFunc
cancelTwitchEventSub database.CancelFunc cancelTwitchEventSub database.CancelFunc
@ -99,7 +92,6 @@ type BotAlertsModule struct {
func SetupAlerts(bot *Bot) *BotAlertsModule { func SetupAlerts(bot *Bot) *BotAlertsModule {
mod := &BotAlertsModule{ mod := &BotAlertsModule{
bot: bot, bot: bot,
templates: templateCache{},
} }
// Load config from database // Load config from database
@ -147,7 +139,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
// Assign random message // Assign random message
messageID := rand.Intn(len(mod.Config.Subscription.Messages)) messageID := rand.Intn(len(mod.Config.Subscription.Messages))
tpl, ok := mod.templates.subscription.messages[messageID] tpl, ok := mod.templates[templateTypeSubscription][mod.Config.Subscription.Messages[messageID]]
// If template is broken, write it as is (soft fail, plus we raise attention I guess?) // If template is broken, write it as is (soft fail, plus we raise attention I guess?)
if !ok { if !ok {
mod.bot.WriteMessage(mod.Config.Subscription.Messages[messageID]) mod.bot.WriteMessage(mod.Config.Subscription.Messages[messageID])
@ -155,31 +147,31 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
// Check for variations, either by streak or gifted // Check for variations, either by streak or gifted
if sub.IsGift { if sub.IsGift {
for variationIndex, variation := range mod.Config.Subscription.Variations { for _, variation := range mod.Config.Subscription.Variations {
if variation.IsGifted != nil && *variation.IsGifted { if variation.IsGifted != nil && *variation.IsGifted {
// Get random template from variations
messageID = rand.Intn(len(variation.Messages))
// Make sure template is valid // Make sure template is valid
if varTemplates, ok := mod.templates.subscription.variations[variationIndex]; ok { if temp, ok := mod.templates[templateTypeSubscription][variation.Messages[messageID]]; ok {
if temp, ok := varTemplates[messageID]; ok {
tpl = temp tpl = temp
break break
} }
} }
} }
}
} else if sub.DurationMonths > 0 { } else if sub.DurationMonths > 0 {
minMonths := -1 minMonths := -1
for variationIndex, variation := range mod.Config.Subscription.Variations { for _, variation := range mod.Config.Subscription.Variations {
if variation.MinStreak != nil && sub.DurationMonths >= *variation.MinStreak && sub.DurationMonths >= minMonths { if variation.MinStreak != nil && sub.DurationMonths >= *variation.MinStreak && sub.DurationMonths >= minMonths {
// Get random template from variations
messageID = rand.Intn(len(variation.Messages))
// Make sure template is valid // Make sure template is valid
if varTemplates, ok := mod.templates.subscription.variations[variationIndex]; ok { if temp, ok := mod.templates[templateTypeSubscription][variation.Messages[messageID]]; ok {
if temp, ok := varTemplates[messageID]; ok {
tpl = temp tpl = temp
minMonths = *variation.MinStreak minMonths = *variation.MinStreak
} }
} }
} }
} }
}
writeTemplate(bot, tpl, sub) writeTemplate(bot, tpl, sub)
} }
addPendingSub := func(ev interface{}) { addPendingSub := func(ev interface{}) {
@ -253,7 +245,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
if !mod.Config.Follow.Enabled { if !mod.Config.Follow.Enabled {
return return
} }
// Parse as follow event // Parse as a follow event
var followEv helix.EventSubChannelFollowEvent var followEv helix.EventSubChannelFollowEvent
err := json.Unmarshal(ev.Event, &followEv) err := json.Unmarshal(ev.Event, &followEv)
if err != nil { if err != nil {
@ -263,7 +255,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
// Pick a random message // Pick a random message
messageID := rand.Intn(len(mod.Config.Follow.Messages)) messageID := rand.Intn(len(mod.Config.Follow.Messages))
// Pick compiled template or fallback to plain text // Pick compiled template or fallback to plain text
if tpl, ok := mod.templates.follow.messages[messageID]; ok { if tpl, ok := mod.templates[templateTypeFollow][mod.Config.Follow.Messages[messageID]]; ok {
writeTemplate(bot, tpl, &followEv) writeTemplate(bot, tpl, &followEv)
} else { } else {
bot.WriteMessage(mod.Config.Follow.Messages[messageID]) bot.WriteMessage(mod.Config.Follow.Messages[messageID])
@ -283,7 +275,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
// Pick a random message from base set // Pick a random message from base set
messageID := rand.Intn(len(mod.Config.Raid.Messages)) messageID := rand.Intn(len(mod.Config.Raid.Messages))
tpl, ok := mod.templates.raid.messages[messageID] tpl, ok := mod.templates[templateTypeRaid][mod.Config.Raid.Messages[messageID]]
if !ok { if !ok {
// Broken template! // Broken template!
mod.bot.WriteMessage(mod.Config.Raid.Messages[messageID]) mod.bot.WriteMessage(mod.Config.Raid.Messages[messageID])
@ -292,18 +284,17 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
// If we have variations, loop through all the available variations and pick the one with the highest minimum viewers that are met // If we have variations, loop through all the available variations and pick the one with the highest minimum viewers that are met
if len(mod.Config.Raid.Variations) > 0 { if len(mod.Config.Raid.Variations) > 0 {
minViewers := -1 minViewers := -1
for variationIndex, variation := range mod.Config.Raid.Variations { for _, variation := range mod.Config.Raid.Variations {
if variation.MinViewers != nil && *variation.MinViewers > minViewers && raidEv.Viewers >= *variation.MinViewers { if variation.MinViewers != nil && *variation.MinViewers > minViewers && raidEv.Viewers >= *variation.MinViewers {
messageID = rand.Intn(len(variation.Messages))
// Make sure the template is valid // Make sure the template is valid
if varTemplates, ok := mod.templates.raid.variations[variationIndex]; ok { if temp, ok := mod.templates[templateTypeRaid][variation.Messages[messageID]]; ok {
if temp, ok := varTemplates[messageID]; ok {
tpl = temp tpl = temp
minViewers = *variation.MinViewers minViewers = *variation.MinViewers
} }
} }
} }
} }
}
// Compile template and send // Compile template and send
writeTemplate(bot, tpl, &raidEv) writeTemplate(bot, tpl, &raidEv)
case helix.EventSubTypeChannelCheer: case helix.EventSubTypeChannelCheer:
@ -320,7 +311,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
// Pick a random message from base set // Pick a random message from base set
messageID := rand.Intn(len(mod.Config.Cheer.Messages)) messageID := rand.Intn(len(mod.Config.Cheer.Messages))
tpl, ok := mod.templates.cheer.messages[messageID] tpl, ok := mod.templates[templateTypeCheer][mod.Config.Cheer.Messages[messageID]]
if !ok { if !ok {
// Broken template! // Broken template!
mod.bot.WriteMessage(mod.Config.Raid.Messages[messageID]) mod.bot.WriteMessage(mod.Config.Raid.Messages[messageID])
@ -329,18 +320,17 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
// If we have variations, loop through all the available variations and pick the one with the highest minimum amount that is met // If we have variations, loop through all the available variations and pick the one with the highest minimum amount that is met
if len(mod.Config.Cheer.Variations) > 0 { if len(mod.Config.Cheer.Variations) > 0 {
minAmount := -1 minAmount := -1
for variationIndex, variation := range mod.Config.Cheer.Variations { for _, variation := range mod.Config.Cheer.Variations {
if variation.MinAmount != nil && *variation.MinAmount > minAmount && cheerEv.Bits >= *variation.MinAmount { if variation.MinAmount != nil && *variation.MinAmount > minAmount && cheerEv.Bits >= *variation.MinAmount {
messageID = rand.Intn(len(variation.Messages))
// Make sure the template is valid // Make sure the template is valid
if varTemplates, ok := mod.templates.cheer.variations[variationIndex]; ok { if temp, ok := mod.templates[templateTypeCheer][variation.Messages[messageID]]; ok {
if temp, ok := varTemplates[messageID]; ok {
tpl = temp tpl = temp
minAmount = *variation.MinAmount minAmount = *variation.MinAmount
} }
} }
} }
} }
}
// Compile template and send // Compile template and send
writeTemplate(bot, tpl, &cheerEv) writeTemplate(bot, tpl, &cheerEv)
case helix.EventSubTypeChannelSubscription: case helix.EventSubTypeChannelSubscription:
@ -383,7 +373,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
// Pick a random message from base set // Pick a random message from base set
messageID := rand.Intn(len(mod.Config.GiftSub.Messages)) messageID := rand.Intn(len(mod.Config.GiftSub.Messages))
tpl, ok := mod.templates.gift.messages[messageID] tpl, ok := mod.templates[templateTypeGift][mod.Config.GiftSub.Messages[messageID]]
if !ok { if !ok {
// Broken template! // Broken template!
mod.bot.WriteMessage(mod.Config.GiftSub.Messages[messageID]) mod.bot.WriteMessage(mod.Config.GiftSub.Messages[messageID])
@ -392,24 +382,23 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
// If we have variations, loop through all the available variations and pick the one with the highest minimum cumulative total that are met // If we have variations, loop through all the available variations and pick the one with the highest minimum cumulative total that are met
if len(mod.Config.GiftSub.Variations) > 0 { if len(mod.Config.GiftSub.Variations) > 0 {
if giftEv.IsAnonymous { if giftEv.IsAnonymous {
for variationIndex, variation := range mod.Config.GiftSub.Variations { for _, variation := range mod.Config.GiftSub.Variations {
if variation.IsAnonymous != nil && *variation.IsAnonymous { if variation.IsAnonymous != nil && *variation.IsAnonymous {
messageID = rand.Intn(len(variation.Messages))
// Make sure template is valid // Make sure template is valid
if varTemplates, ok := mod.templates.gift.variations[variationIndex]; ok { if temp, ok := mod.templates[templateTypeGift][variation.Messages[messageID]]; ok {
if temp, ok := varTemplates[messageID]; ok {
tpl = temp tpl = temp
break break
} }
} }
} }
}
} else if giftEv.CumulativeTotal > 0 { } else if giftEv.CumulativeTotal > 0 {
minCumulative := -1 minCumulative := -1
for variationIndex, variation := range mod.Config.GiftSub.Variations { for _, variation := range mod.Config.GiftSub.Variations {
if variation.MinCumulative != nil && *variation.MinCumulative > minCumulative && giftEv.CumulativeTotal >= *variation.MinCumulative { if variation.MinCumulative != nil && *variation.MinCumulative > minCumulative && giftEv.CumulativeTotal >= *variation.MinCumulative {
messageID = rand.Intn(len(variation.Messages))
// Make sure the template is valid // Make sure the template is valid
if varTemplates, ok := mod.templates.gift.variations[variationIndex]; ok { if temp, ok := mod.templates[templateTypeGift][variation.Messages[messageID]]; ok {
if temp, ok := varTemplates[messageID]; ok {
tpl = temp tpl = temp
minCumulative = *variation.MinCumulative minCumulative = *variation.MinCumulative
} }
@ -417,7 +406,6 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
} }
} }
}
// Compile template and send // Compile template and send
writeTemplate(bot, tpl, &giftEv) writeTemplate(bot, tpl, &giftEv)
} }
@ -432,63 +420,58 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
} }
func (m *BotAlertsModule) compileTemplates() { func (m *BotAlertsModule) compileTemplates() {
for index, msg := range m.Config.Follow.Messages { m.templates = templateCacheMap{
m.templates.follow.messages = make(map[int]*template.Template) templateTypeSubscription: make(templateCache),
m.addTemplate(m.templates.follow.messages, index, msg) templateTypeFollow: make(templateCache),
templateTypeRaid: make(templateCache),
templateTypeCheer: make(templateCache),
templateTypeGift: make(templateCache),
} }
for index, msg := range m.Config.Subscription.Messages {
m.templates.subscription.messages = make(map[int]*template.Template) for _, msg := range m.Config.Follow.Messages {
m.addTemplate(m.templates.subscription.messages, index, msg) m.addTemplate(m.templates[templateTypeFollow], msg)
} }
for varIndex, variation := range m.Config.Subscription.Variations { for _, msg := range m.Config.Subscription.Messages {
m.templates.subscription.variations = make(map[int]map[int]*template.Template) m.addTemplate(m.templates[templateTypeSubscription], msg)
for index, msg := range variation.Messages { }
m.templates.subscription.variations[varIndex] = make(map[int]*template.Template) for _, variation := range m.Config.Subscription.Variations {
m.addTemplate(m.templates.subscription.variations[varIndex], index, msg) for _, msg := range variation.Messages {
m.addTemplate(m.templates[templateTypeSubscription], msg)
} }
} }
for index, msg := range m.Config.Raid.Messages { for _, msg := range m.Config.Raid.Messages {
m.templates.raid.messages = make(map[int]*template.Template) m.addTemplate(m.templates[templateTypeRaid], msg)
m.addTemplate(m.templates.raid.messages, index, msg)
} }
for varIndex, variation := range m.Config.Raid.Variations { for _, variation := range m.Config.Raid.Variations {
m.templates.raid.variations = make(map[int]map[int]*template.Template) for _, msg := range variation.Messages {
for index, msg := range variation.Messages { m.addTemplate(m.templates[templateTypeRaid], msg)
m.templates.raid.variations[varIndex] = make(map[int]*template.Template)
m.addTemplate(m.templates.raid.variations[varIndex], index, msg)
} }
} }
for index, msg := range m.Config.Cheer.Messages { for _, msg := range m.Config.Cheer.Messages {
m.templates.cheer.messages = make(map[int]*template.Template) m.addTemplate(m.templates[templateTypeCheer], msg)
m.addTemplate(m.templates.cheer.messages, index, msg)
} }
for varIndex, variation := range m.Config.Cheer.Variations { for _, variation := range m.Config.Cheer.Variations {
m.templates.cheer.variations = make(map[int]map[int]*template.Template) for _, msg := range variation.Messages {
for index, msg := range variation.Messages { m.addTemplate(m.templates[templateTypeCheer], msg)
m.templates.cheer.variations[varIndex] = make(map[int]*template.Template)
m.addTemplate(m.templates.cheer.variations[varIndex], index, msg)
} }
} }
for index, msg := range m.Config.GiftSub.Messages { for _, msg := range m.Config.GiftSub.Messages {
m.templates.gift.messages = make(map[int]*template.Template) m.addTemplate(m.templates[templateTypeGift], msg)
m.addTemplate(m.templates.gift.messages, index, msg)
} }
for varIndex, variation := range m.Config.GiftSub.Variations { for _, variation := range m.Config.GiftSub.Variations {
m.templates.gift.variations = make(map[int]map[int]*template.Template) for _, msg := range variation.Messages {
for index, msg := range variation.Messages { m.addTemplate(m.templates[templateTypeGift], msg)
m.templates.gift.variations[varIndex] = make(map[int]*template.Template)
m.addTemplate(m.templates.gift.variations[varIndex], index, msg)
} }
} }
} }
func (m *BotAlertsModule) addTemplate(templateList map[int]*template.Template, id int, msg string) { func (m *BotAlertsModule) addTemplate(templateList templateCache, msg string) {
tpl, err := template.New("").Funcs(m.bot.customFunctions).Funcs(sprig.TxtFuncMap()).Parse(msg) tpl, err := template.New("").Funcs(m.bot.customFunctions).Funcs(sprig.TxtFuncMap()).Parse(msg)
if err != nil { if err != nil {
m.bot.logger.Error("error compiling template", zap.Error(err)) m.bot.logger.Error("error compiling template", zap.Error(err))
return return
} }
templateList[id] = tpl templateList[msg] = tpl
} }
func (m *BotAlertsModule) Close() { func (m *BotAlertsModule) Close() {