Replace embed comparison with general compare helper

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-07 19:27:20 +02:00
parent c54d497b17
commit f83598f387
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 103 additions and 40 deletions

View file

@ -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
}

View file

@ -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
}