Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type Protocol struct {
EditSuffix string // mattermost, slack, discord, telegram
EditDisable bool // mattermost, slack, discord, telegram
EditMaxDays int // discord
EmbedFormat string // discord
HTMLDisable bool // matrix
IconURL string // mattermost, slack
IgnoreFailureOnStart bool // general
Expand Down
63 changes: 46 additions & 17 deletions bridge/discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,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 += b.handleEmbed(embed)
}
}

Expand Down Expand Up @@ -326,27 +326,56 @@ func (b *Bdiscord) memberRemove(s *discordgo.Session, m *discordgo.GuildMemberRe
b.Remote <- rmsg
}

func handleEmbed(embed *discordgo.MessageEmbed) string {
var t []string
func (b *Bdiscord) handleEmbed(embed *discordgo.MessageEmbed) string {
var result string

t = append(t, embed.Title)
t = append(t, embed.Description)
t = append(t, embed.URL)
result = b.GetString("EmbedFormat")

i := 0
for _, e := range t {
if e == "" {
continue
}
if result == "" {
result = " embed: {TITLE} - {DESCRIPTION} - {URL}"
}

i++
if i == 1 {
result += " embed: " + e
continue
}
result = strings.ReplaceAll(result, "{URL}", embed.URL)
result = strings.ReplaceAll(result, "{TITLE}", embed.Title)
result = strings.ReplaceAll(result, "{DESCRIPTION}", embed.Description)
result = strings.ReplaceAll(result, "{TIME}", embed.Timestamp)

if embed.Footer != nil {
result = strings.ReplaceAll(result, "{FOOTER}", embed.Footer.Text)
} else {
result = strings.ReplaceAll(result, "{FOOTER}", "")
}

result += " - " + e
if embed.Image != nil {
result = strings.ReplaceAll(result, "{IMAGE}", embed.Image.URL)
} else {
result = strings.ReplaceAll(result, "{IMAGE}", "")
}

if embed.Thumbnail != nil {
result = strings.ReplaceAll(result, "{THUMBNAIL}", embed.Thumbnail.URL)
} else {
result = strings.ReplaceAll(result, "{THUMBNAIL}", "")
}

if embed.Video != nil {
result = strings.ReplaceAll(result, "{VIDEO}", embed.Video.URL)
} else {
result = strings.ReplaceAll(result, "{VIDEO}", "")
}

if embed.Provider != nil {
result = strings.ReplaceAll(result, "{PROVIDER}", embed.Provider.URL)
} else {
result = strings.ReplaceAll(result, "{PROVIDER}", "")
}

if embed.Author != nil {
result = strings.ReplaceAll(result, "{AUTHOR}", embed.Author.Name)
result = strings.ReplaceAll(result, "{AUTHORURL}", embed.Author.URL)
} else {
result = strings.ReplaceAll(result, "{AUTHOR}", "")
result = strings.ReplaceAll(result, "{AUTHORURL}", "")
}

if result != "" {
Expand Down
27 changes: 22 additions & 5 deletions bridge/discord/handlers_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
package bdiscord

import (
"strings"
"testing"

"github.com/bwmarrin/discordgo"
"github.com/stretchr/testify/assert"
)

func handleEmbed(embed *discordgo.MessageEmbed) string {
var result string

result = " embed: {TITLE} - {DESCRIPTION} - {URL}"

result = strings.ReplaceAll(result, "{URL}", embed.URL)
result = strings.ReplaceAll(result, "{TITLE}", embed.Title)
result = strings.ReplaceAll(result, "{DESCRIPTION}", embed.Description)

if result != "" {
result += "\n"
}

return result
}

func TestHandleEmbed(t *testing.T) {
testcases := map[string]struct {
embed *discordgo.MessageEmbed
result string
}{
"allempty": {
embed: &discordgo.MessageEmbed{},
result: "",
result: " embed: - - \n",
},
"one": {
embed: &discordgo.MessageEmbed{
Title: "blah",
},
result: " embed: blah\n",
result: " embed: blah - - \n",
},
"two": {
embed: &discordgo.MessageEmbed{
Title: "blah",
Description: "blah2",
},
result: " embed: blah - blah2\n",
result: " embed: blah - blah2 - \n",
},
"three": {
embed: &discordgo.MessageEmbed{
Expand All @@ -42,13 +59,13 @@ func TestHandleEmbed(t *testing.T) {
Description: "blah2",
URL: "blah3",
},
result: " embed: blah2 - blah3\n",
result: " embed: - blah2 - blah3\n",
},
"oneb": {
embed: &discordgo.MessageEmbed{
URL: "blah3",
},
result: " embed: blah3\n",
result: " embed: - - blah3\n",
},
}

Expand Down
12 changes: 12 additions & 0 deletions docs/protocols/discord/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ Shows title, description and URL of embedded messages (sent by other bots)
ShowEmbeds=true
```

## EmbedFormat

Sets the format of the embeds attached to Discord messages sent by users (e.g. YouTube videos).
The only available placeholders are `{TITLE}`, `{URL}` (a URL), `{DESCRIPTION}`, `{TIME}`, `{FOOTER}`, `{IMAGE}` (a URL), `{THUMBNAIL}` (a URL), `{VIDEO}` (a URL), `{PROVIDER}` (a URL), `{AUTHOR}`, and `{AUTHORURL}` (a URL). If a parameter isn't in the originating embed it is simply omitted. (e.g. `Hello {AUTHOR}!` becomes `Hello !` if there is no author)

- Setting: **OPTIONAL** (default `" embed: {TITLE} - {DESCRIPTION} - {URL}"`), **RELOADABLE**
- Format: *string*
- Example:
```toml
EmbedFormat="\n# {TITLE} ({URL}) by {AUTHOR} ({AUTHORURL})\n{DESCRIPTION}\n{TIME}\n{FOOTER}"
```

## UseUserName

Shows the username instead of the server nickname
Expand Down
3 changes: 3 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ AllowMention=["everyone", "roles", "users"]
# ShowEmbeds shows the title, description and URL of embedded messages (sent by other bots)
ShowEmbeds=false

# EmbedFormat sets the outgoing format for messages sent by users with embeds (e.g. YouTube videos)
EmbedFormat="\n# {TITLE} ({URL})\n{DESCRIPTION}\n{TIME}"

# 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.
#
Expand Down
Loading