diff --git a/bridge/config/config.go b/bridge/config/config.go index 7079d42f2..6509d839c 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -137,6 +137,7 @@ type Protocol struct { DebugLevel int // only for irc now DeviceID string // matrix DisableWebPagePreview bool // telegram + DontMarkEmbeds bool // discord EditSuffix string // mattermost, slack, discord, telegram EditDisable bool // mattermost, slack, discord, telegram HTMLDisable bool // matrix diff --git a/bridge/discord/handlers.go b/bridge/discord/handlers.go index dc3480ed4..054e3f07c 100644 --- a/bridge/discord/handlers.go +++ b/bridge/discord/handlers.go @@ -1,6 +1,10 @@ package bdiscord import ( + "fmt" + "regexp" + "strings" + "github.com/bwmarrin/discordgo" "github.com/davecgh/go-spew/spew" "github.com/matterbridge-org/matterbridge/bridge/config" @@ -165,7 +169,7 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat // if we have embedded content add it to text if b.GetBool("ShowEmbeds") && m.Message.Embeds != nil { for _, embed := range m.Message.Embeds { - rmsg.Text += handleEmbed(embed) + rmsg.Text += handleEmbed(embed, b.GetBool("DontMarkEmbeds")) } } @@ -281,31 +285,71 @@ func (b *Bdiscord) memberRemove(s *discordgo.Session, m *discordgo.GuildMemberRe b.Remote <- rmsg } -func handleEmbed(embed *discordgo.MessageEmbed) string { - var t []string - var result string - - t = append(t, embed.Title) - t = append(t, embed.Description) - t = append(t, embed.URL) - - i := 0 - for _, e := range t { - if e == "" { - continue +//nolint:funlen +func handleEmbed(embed *discordgo.MessageEmbed, dontMarkEmbeds bool) string { + var ( + t []string + result string + ) + + if strings.TrimSpace(embed.Author.Name) != "" { + if strings.TrimSpace(embed.Author.URL) != "" { + t = append( + t, + fmt.Sprintf( + "[%s](%s)", + strings.TrimSpace(embed.Author.Name), + strings.TrimSpace(embed.Author.URL), + ), + ) + } else { + t = append( + t, + fmt.Sprintf( + "**%s**", + strings.TrimSpace(embed.Author.Name), + ), + ) } + } - i++ - if i == 1 { - result += " embed: " + e - continue + if strings.TrimSpace(embed.Title) != "" { + if strings.TrimSpace(embed.URL) != "" { + re := regexp.MustCompile(`[\\[\\]]`) + t = append( + t, + fmt.Sprintf( + "[%s](%s)", + re.ReplaceAllString(strings.TrimSpace(embed.Title), ""), + strings.TrimSpace(embed.URL), + ), + ) + } else { + t = append( + t, + fmt.Sprintf( + "**%s**", + strings.TrimSpace(embed.Title), + ), + ) } + } - result += " - " + e + if strings.TrimSpace(embed.Description) != "" { + t = append(t, strings.TrimSpace(embed.Description)) } - if result != "" { - result += "\n" + var embedMarker string + if dontMarkEmbeds { + embedMarker = "" + } else { + embedMarker = "\nembed:" + } + + if len(t) > 0 { + result = fmt.Sprintf("%s\n%s\n", embedMarker, strings.Join(t, "\n")) + } else { + result = "" } return result diff --git a/bridge/discord/handlers_test.go b/bridge/discord/handlers_test.go index 915d9b19c..1663b7948 100644 --- a/bridge/discord/handlers_test.go +++ b/bridge/discord/handlers_test.go @@ -8,7 +8,7 @@ import ( ) func TestHandleEmbed(t *testing.T) { - testcases := map[string]struct { + markedTestCases := map[string]struct { embed *discordgo.MessageEmbed result string }{ @@ -20,14 +20,14 @@ func TestHandleEmbed(t *testing.T) { embed: &discordgo.MessageEmbed{ Title: "blah", }, - result: " embed: blah\n", + result: "\nembed:\nblah\n", }, "two": { embed: &discordgo.MessageEmbed{ Title: "blah", Description: "blah2", }, - result: " embed: blah - blah2\n", + result: "\nembed:\nblah\nblah2\n", }, "three": { embed: &discordgo.MessageEmbed{ @@ -35,24 +35,72 @@ func TestHandleEmbed(t *testing.T) { Description: "blah2", URL: "blah3", }, - result: " embed: blah - blah2 - blah3\n", + result: "\nembed:\nblah\nblah2\nblah3\n", }, "twob": { embed: &discordgo.MessageEmbed{ Description: "blah2", URL: "blah3", }, - result: " embed: blah2 - blah3\n", + result: "\nembed:\nblah2\nblah3\n", }, "oneb": { embed: &discordgo.MessageEmbed{ URL: "blah3", }, - result: " embed: blah3\n", + result: "\nembed:\nblah3\n", }, } - for name, tc := range testcases { - assert.Equalf(t, tc.result, handleEmbed(tc.embed), "Testcases %s", name) + unmarkedTestCases := map[string]struct { + embed *discordgo.MessageEmbed + result string + }{ + "allempty": { + embed: &discordgo.MessageEmbed{}, + result: "", + }, + "one": { + embed: &discordgo.MessageEmbed{ + Title: "blah", + }, + result: "\nblah\n", + }, + "two": { + embed: &discordgo.MessageEmbed{ + Title: "blah", + Description: "blah2", + }, + result: "\nblah\nblah2\n", + }, + "three": { + embed: &discordgo.MessageEmbed{ + Title: "blah", + Description: "blah2", + URL: "blah3", + }, + result: "\nblah\nblah2\nblah3\n", + }, + "twob": { + embed: &discordgo.MessageEmbed{ + Description: "blah2", + URL: "blah3", + }, + result: "\nblah2\nblah3\n", + }, + "oneb": { + embed: &discordgo.MessageEmbed{ + URL: "blah3", + }, + result: "\nblah3\n", + }, + } + + for name, tc := range markedTestCases { + assert.Equalf(t, tc.result, handleEmbed(tc.embed, false), "Testcases %s", name) + } + + for name, tc := range unmarkedTestCases { + assert.Equalf(t, tc.result, handleEmbed(tc.embed, true), "Testcases %s", name) } } diff --git a/changelog.md b/changelog.md index 03a9239dd..3f03d840c 100644 --- a/changelog.md +++ b/changelog.md @@ -19,6 +19,10 @@ ## New Features +- discord: + - Embeds are now formatted nicely with markup + - Support for hiding the 'embed:' text at the top of embeds that marks them + To enable add `DontMarkEmbeds=true` to your discord config - general - matterbridge output now colors log level for easier log reading ([#25](https://github.com/matterbridge-org/matterbridge/pull/25)) - new HTTP helpers are common to all bridges, and allow overriding specific settings ([#59](https://github.com/matterbridge-org/matterbridge/pull/59)) diff --git a/docs/protocols/discord/settings.md b/docs/protocols/discord/settings.md index 1fec03b93..12b347789 100644 --- a/docs/protocols/discord/settings.md +++ b/docs/protocols/discord/settings.md @@ -49,6 +49,17 @@ prevents the ping/notification. AllowMention=["everyone", "roles", "users"] ``` +## DontMarkEmbeds + +Stops the 'embed:' text from being added as the first line in an embed message + +- Setting: **OPTIONAL**, **RELOADABLE** +- Format: *boolean* +- Example: + ```toml + DontMarkEmbeds=true + ``` + ## ShowEmbeds Shows title, description and URL of embedded messages (sent by other bots) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 103043619..50dac0333 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -842,7 +842,10 @@ Server="yourservername" AllowMention=["everyone", "roles", "users"] # ShowEmbeds shows the title, description and URL of embedded messages (sent by other bots) -ShowEmbeds=false +ShowEmbeds=true + +# Stops the 'embed:' text from being added as the first line in an embed message +DontMarkEmbeds=true # UseLocalAvatar specifies source bridges for which an avatar should be 'guessed' when an incoming message has no avatar. # This works by comparing the username of the message to an existing Discord user, and using the avatar of the Discord user.