mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 21:50:28 +00:00
Make this work for all possible cases. Add tests
This commit is contained in:
parent
d4acdf2f89
commit
281016a501
@ -103,7 +103,7 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
|||||||
// if we have embedded content add it to text
|
// if we have embedded content add it to text
|
||||||
if b.GetBool("ShowEmbeds") && m.Message.Embeds != nil {
|
if b.GetBool("ShowEmbeds") && m.Message.Embeds != nil {
|
||||||
for _, embed := range m.Message.Embeds {
|
for _, embed := range m.Message.Embeds {
|
||||||
rmsg.Text = rmsg.Text + "embed: " + embed.Title + " - " + embed.Description + " - " + embed.URL + "\n"
|
rmsg.Text += handleEmbed(embed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,3 +192,33 @@ func (b *Bdiscord) memberRemove(s *discordgo.Session, m *discordgo.GuildMemberRe
|
|||||||
b.Log.Debugf("<= Message is %#v", rmsg)
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
||||||
b.Remote <- rmsg
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
i++
|
||||||
|
if i == 1 {
|
||||||
|
result += "embed: " + e
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
result += " - " + e
|
||||||
|
}
|
||||||
|
|
||||||
|
if result != "" {
|
||||||
|
result += "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
58
bridge/discord/handlers_test.go
Normal file
58
bridge/discord/handlers_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package bdiscord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matterbridge/discordgo"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandleEmbed(t *testing.T) {
|
||||||
|
testcases := map[string]struct {
|
||||||
|
embed *discordgo.MessageEmbed
|
||||||
|
result string
|
||||||
|
}{
|
||||||
|
"allempty": {
|
||||||
|
embed: &discordgo.MessageEmbed{},
|
||||||
|
result: "",
|
||||||
|
},
|
||||||
|
"one": {
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "blah",
|
||||||
|
},
|
||||||
|
result: "embed: blah\n",
|
||||||
|
},
|
||||||
|
"two": {
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "blah",
|
||||||
|
Description: "blah2",
|
||||||
|
},
|
||||||
|
result: "embed: blah - blah2\n",
|
||||||
|
},
|
||||||
|
"three": {
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
Title: "blah",
|
||||||
|
Description: "blah2",
|
||||||
|
URL: "blah3",
|
||||||
|
},
|
||||||
|
result: "embed: blah - blah2 - blah3\n",
|
||||||
|
},
|
||||||
|
"twob": {
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
Description: "blah2",
|
||||||
|
URL: "blah3",
|
||||||
|
},
|
||||||
|
result: "embed: blah2 - blah3\n",
|
||||||
|
},
|
||||||
|
"oneb": {
|
||||||
|
embed: &discordgo.MessageEmbed{
|
||||||
|
URL: "blah3",
|
||||||
|
},
|
||||||
|
result: "embed: blah3\n",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range testcases {
|
||||||
|
assert.Equalf(t, tc.result, handleEmbed(tc.embed), "Testcases %s", name)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user