From e376c9205c09d6bfdd47f6197908ecc40e9b11ad Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 6 Dec 2022 13:22:14 +0100 Subject: [PATCH] Make embed title and therefore whole embed optional Signed-off-by: Knut Ahlers --- helpers.go | 5 +++++ mod_reactionRole.go | 44 ++++++++++++++++++++++++-------------------- wiki/Home.md | 2 +- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/helpers.go b/helpers.go index a5d1d06..f4e272b 100644 --- a/helpers.go +++ b/helpers.go @@ -20,6 +20,11 @@ func ptrTime(v time.Time) *time.Time { return &v } //nolint:gocognit,gocyclo // This function compares two structs and needs the complexity func isDiscordMessageEmbedEqual(a, b *discordgo.MessageEmbed) bool { + if a == nil || b == nil { + // If one of them is nil, don't do the in-depth analysis + return a == b + } + checks := [][2]interface{}{ // Base-Struct {a.Type, b.Type}, diff --git a/mod_reactionRole.go b/mod_reactionRole.go index 924e67d..6f6d1e2 100644 --- a/mod_reactionRole.go +++ b/mod_reactionRole.go @@ -5,12 +5,13 @@ import ( "strings" "time" - "github.com/Luzifer/go_helpers/v2/env" - "github.com/Luzifer/go_helpers/v2/str" "github.com/bwmarrin/discordgo" "github.com/pkg/errors" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" + + "github.com/Luzifer/go_helpers/v2/env" + "github.com/Luzifer/go_helpers/v2/str" ) /* @@ -59,25 +60,28 @@ func (m modReactionRole) Setup() error { // @attr content optional string "" Message content to post above the embed contentString := m.attrs.MustString("content", ptrStringEmpty) - msgEmbed := &discordgo.MessageEmbed{ - // @attr embed_color optional int64 "0x2ECC71" Integer / HEX representation of the color for the embed - Color: int(m.attrs.MustInt64("embed_color", ptrInt64(streamScheduleDefaultColor))), - // @attr embed_description optional string "" Description for the embed block - Description: strings.TrimSpace(m.attrs.MustString("embed_description", ptrStringEmpty)), - Timestamp: time.Now().Format(time.RFC3339), - // @attr embed_title required string "" Title of the embed - Title: m.attrs.MustString("embed_title", nil), - Type: discordgo.EmbedTypeRich, - } + var msgEmbed *discordgo.MessageEmbed + // @attr embed_title optional string "" Title of the embed (embed will not be added when title is missing) + if title := m.attrs.MustString("embed_title", ptrStringEmpty); title != "" { + msgEmbed = &discordgo.MessageEmbed{ + // @attr embed_color optional int64 "0x2ECC71" Integer / HEX representation of the color for the embed + Color: int(m.attrs.MustInt64("embed_color", ptrInt64(streamScheduleDefaultColor))), + // @attr embed_description optional string "" Description for the embed block + Description: strings.TrimSpace(m.attrs.MustString("embed_description", ptrStringEmpty)), + Timestamp: time.Now().Format(time.RFC3339), + Title: title, + Type: discordgo.EmbedTypeRich, + } - if m.attrs.MustString("embed_thumbnail_url", ptrStringEmpty) != "" { - msgEmbed.Thumbnail = &discordgo.MessageEmbedThumbnail{ - // @attr embed_thumbnail_url optional string "" Publically hosted image URL to use as thumbnail - URL: m.attrs.MustString("embed_thumbnail_url", ptrStringEmpty), - // @attr embed_thumbnail_width optional int64 "" Width of the thumbnail - Width: int(m.attrs.MustInt64("embed_thumbnail_width", ptrInt64Zero)), - // @attr embed_thumbnail_height optional int64 "" Height of the thumbnail - Height: int(m.attrs.MustInt64("embed_thumbnail_height", ptrInt64Zero)), + if m.attrs.MustString("embed_thumbnail_url", ptrStringEmpty) != "" { + msgEmbed.Thumbnail = &discordgo.MessageEmbedThumbnail{ + // @attr embed_thumbnail_url optional string "" Publically hosted image URL to use as thumbnail + URL: m.attrs.MustString("embed_thumbnail_url", ptrStringEmpty), + // @attr embed_thumbnail_width optional int64 "" Width of the thumbnail + Width: int(m.attrs.MustInt64("embed_thumbnail_width", ptrInt64Zero)), + // @attr embed_thumbnail_height optional int64 "" Height of the thumbnail + Height: int(m.attrs.MustInt64("embed_thumbnail_height", ptrInt64Zero)), + } } } diff --git a/wiki/Home.md b/wiki/Home.md index 6b86a27..267374a 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -121,7 +121,6 @@ Creates a post with pre-set reactions and assigns roles on reaction | Attribute | Req. | Type | Default Value | Description | | --------- | :--: | ---- | ------------- | ----------- | | `discord_channel_id` | ✅ | string | | ID of the Discord channel to post the message to | -| `embed_title` | ✅ | string | | Title of the embed | | `reaction_roles` | ✅ | []string | | List of strings in format `emote=role-id[:set]`. `emote` equals an unicode emote (✅) or a custom emote in form `::`. `role-id` is the integer ID of the guilds role to add with this emote. If `:set` is added at the end, the role will only be added but not removed when the reaction is removed. | | `content` | | string | | Message content to post above the embed | | `embed_color` | | int64 | `0x2ECC71` | Integer / HEX representation of the color for the embed | @@ -129,6 +128,7 @@ Creates a post with pre-set reactions and assigns roles on reaction | `embed_thumbnail_height` | | int64 | | Height of the thumbnail | | `embed_thumbnail_url` | | string | | Publically hosted image URL to use as thumbnail | | `embed_thumbnail_width` | | int64 | | Width of the thumbnail | +| `embed_title` | | string | | Title of the embed (embed will not be added when title is missing) | ## Type: `schedule`