Skip to content

Commit d438615

Browse files
committed
Discord: Download files, don't send as URLs
And make that as an option
1 parent bbc85c6 commit d438615

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

bridge/discord/discord.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type Bdiscord struct {
3636
userMemberMap map[string]*discordgo.Member
3737
nickMemberMap map[string]*discordgo.Member
3838

39+
// Never send Discord's attachments as URLs, always download and re-upload them to the destination as regular files
40+
alwaysDownloadFiles bool
41+
3942
// Webhook specific logic
4043
useAutoWebhooks bool
4144
transmitter *transmitter.Transmitter
@@ -57,6 +60,8 @@ func New(cfg *bridge.Config) bridge.Bridger {
5760
b.nickMemberMap = make(map[string]*discordgo.Member)
5861
b.channelInfoMap = make(map[string]*config.ChannelInfo)
5962

63+
b.alwaysDownloadFiles = b.GetBool("AlwaysDownloadFiles")
64+
6065
b.useAutoWebhooks = b.GetBool("AutoWebhooks")
6166
if b.useAutoWebhooks {
6267
b.Log.Debug("Using automatic webhooks")

bridge/discord/handlers.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/bwmarrin/discordgo"
55
"github.com/davecgh/go-spew/spew"
66
"github.com/matterbridge-org/matterbridge/bridge/config"
7+
"github.com/matterbridge-org/matterbridge/bridge/helper"
78
)
89

910
func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { //nolint:unparam
@@ -98,15 +99,44 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
9899
return
99100
}
100101

102+
rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID, Extra: make(map[string][]interface{})}
103+
101104
// add the url of the attachments to content
102105
if len(m.Attachments) > 0 {
106+
first := true
103107
for _, attach := range m.Attachments {
104-
m.Content = m.Content + "\n" + attach.URL
108+
if b.alwaysDownloadFiles {
109+
var url, name, caption string
110+
111+
url = attach.URL
112+
name = attach.Filename
113+
114+
err := helper.HandleDownloadSize(b.Log, &rmsg, name, int64(attach.Size), b.General)
115+
if err != nil {
116+
return
117+
}
118+
data, err := helper.DownloadFile(url)
119+
if err != nil {
120+
return
121+
}
122+
123+
if first {
124+
caption = m.Content
125+
if caption == "" {
126+
caption = name
127+
}
128+
first = false
129+
} else {
130+
caption = ""
131+
}
132+
133+
helper.HandleDownloadData(b.Log, &rmsg, name, caption, "", data, b.General)
134+
} else {
135+
m.Content = m.Content + "\n" + attach.URL
136+
}
105137
}
106138
}
107139

108-
rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID}
109-
110140
b.Log.Debugf("== Receiving event %#v", m.Message)
111141

112142
if m.Content != "" {
@@ -139,7 +169,7 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
139169
}
140170

141171
// no empty messages
142-
if rmsg.Text == "" {
172+
if rmsg.Text == "" && len(m.Attachments) == 0 {
143173
return
144174
}
145175

0 commit comments

Comments
 (0)