From f83598f3873107089b48f10f4e79c9d9c5554f42 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 7 Aug 2021 19:27:20 +0200 Subject: [PATCH] Replace embed comparison with general compare helper Signed-off-by: Knut Ahlers --- helpers.go | 103 +++++++++++++++++++++++++++++++++++++++++- mod_streamSchedule.go | 40 +--------------- 2 files changed, 103 insertions(+), 40 deletions(-) diff --git a/helpers.go b/helpers.go index 20df432..f719439 100644 --- a/helpers.go +++ b/helpers.go @@ -1,6 +1,10 @@ package main -import "time" +import ( + "time" + + "github.com/bwmarrin/discordgo" +) var ( ptrBoolFalse = ptrBool(false) @@ -13,3 +17,100 @@ func ptrDuration(v time.Duration) *time.Duration { return &v } func ptrInt64(v int64) *int64 { return &v } func ptrString(v string) *string { return &v } func ptrTime(v time.Time) *time.Time { return &v } + +func isDiscordMessageEmbedEqual(a, b *discordgo.MessageEmbed) bool { + checks := [][2]interface{}{ + // Base-Struct + {a.Type, b.Type}, + {a.URL, b.URL}, + {a.Title, b.Title}, + {a.Description, b.Description}, + // We ignore the timestamp here as it would represent the post time of the new embed + {a.Color, b.Color}, + } + + if a.Footer == nil && b.Footer != nil || a.Footer != nil && b.Footer == nil { + return false + } + if a.Image == nil && b.Image != nil || a.Image != nil && b.Image == nil { + return false + } + if a.Thumbnail == nil && b.Thumbnail != nil || a.Thumbnail != nil && b.Thumbnail == nil { + return false + } + if a.Video == nil && b.Video != nil || a.Video != nil && b.Video == nil { + return false + } + if a.Provider == nil && b.Provider != nil || a.Provider != nil && b.Provider == nil { + return false + } + if a.Author == nil && b.Author != nil || a.Author != nil && b.Author == nil { + return false + } + + if a.Footer != nil { + checks = append(checks, [][2]interface{}{ + {a.Footer.IconURL, b.Footer.IconURL}, + {a.Footer.Text, b.Footer.Text}, + }...) + } + + if a.Image != nil { + checks = append(checks, [][2]interface{}{ + {a.Image.URL, b.Image.URL}, + {a.Image.Width, b.Image.Width}, + {a.Image.Height, b.Image.Height}, + }...) + } + + if a.Thumbnail != nil { + checks = append(checks, [][2]interface{}{ + {a.Thumbnail.URL, b.Thumbnail.URL}, + {a.Thumbnail.Width, b.Thumbnail.Width}, + {a.Thumbnail.Height, b.Thumbnail.Height}, + }...) + } + + if a.Video != nil { + checks = append(checks, [][2]interface{}{ + {a.Video.URL, b.Video.URL}, + {a.Video.Width, b.Video.Width}, + {a.Video.Height, b.Video.Height}, + }...) + } + + if a.Provider != nil { + checks = append(checks, [][2]interface{}{ + {a.Provider.URL, b.Provider.URL}, + {a.Provider.Name, b.Provider.Name}, + }...) + } + + if a.Author != nil { + checks = append(checks, [][2]interface{}{ + {a.Author.URL, b.Author.URL}, + {a.Author.Name, b.Author.Name}, + {a.Author.IconURL, b.Author.IconURL}, + }...) + } + + if len(a.Fields) != len(b.Fields) { + return false + } + + for i := range a.Fields { + checks = append(checks, [][2]interface{}{ + {a.Fields[i].Name, b.Fields[i].Name}, + {a.Fields[i].Value, b.Fields[i].Value}, + {a.Fields[i].Inline, b.Fields[i].Inline}, + }...) + } + + for _, p := range checks { + if p[0] != p[1] { + return false + } + } + + return true +} diff --git a/mod_streamSchedule.go b/mod_streamSchedule.go index 8df5598..03a9aa1 100644 --- a/mod_streamSchedule.go +++ b/mod_streamSchedule.go @@ -146,7 +146,7 @@ func (m modStreamSchedule) cronUpdateSchedule() { if managedMsg != nil { oldEmbed := managedMsg.Embeds[0] - if !m.embedNeedsUpdate(oldEmbed, msgEmbed) { + if isDiscordMessageEmbedEqual(oldEmbed, msgEmbed) { log.Debug("Stream Schedule is up-to-date") return } @@ -186,41 +186,3 @@ func (m modStreamSchedule) formatGermanShort(t time.Time) string { return strings.Join([]string{wd, t.In(tz).Format("02.01. 15:04"), "Uhr"}, " ") } - -func (m modStreamSchedule) embedNeedsUpdate(o, n *discordgo.MessageEmbed) bool { - if o.Title != n.Title { - return true - } - - if o.Description != n.Description { - return true - } - - if o.Thumbnail != nil && n.Thumbnail == nil || o.Thumbnail == nil && n.Thumbnail != nil { - return true - } - - if o.Thumbnail != nil && o.Thumbnail.URL != n.Thumbnail.URL { - return true - } - - if len(o.Fields) != len(n.Fields) { - return true - } - - for i := range o.Fields { - if o.Fields[i].Name != n.Fields[i].Name { - return true - } - - if o.Fields[i].Value != n.Fields[i].Value { - return true - } - - if o.Fields[i].Inline != n.Fields[i].Inline { - return true - } - } - - return false -}