Skip to content

Commit 95e86c5

Browse files
committed
Add option for using a naitive upload when relaying from discord
Resolves 42wim#1965
1 parent 24f6747 commit 95e86c5

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

bridge/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ type Protocol struct {
166166
URL string // mattermost, slack // DEPRECATED
167167
UseAPI bool // mattermost, slack
168168
UseLocalAvatar []string // discord
169+
UseNaitiveUpload bool // discord
169170
UseSASL bool // IRC
170171
UseTLS bool // IRC
171172
UseDiscriminator bool // discord

bridge/discord/handlers.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package bdiscord
22

33
import (
4+
"fmt"
5+
"path"
6+
47
"github.com/42wim/matterbridge/bridge/config"
8+
"github.com/42wim/matterbridge/bridge/helper"
59
"github.com/bwmarrin/discordgo"
610
"github.com/davecgh/go-spew/spew"
711
)
@@ -98,15 +102,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
98102
return
99103
}
100104

101-
// add the url of the attachments to content
102-
if len(m.Attachments) > 0 {
103-
for _, attach := range m.Attachments {
104-
m.Content = m.Content + "\n" + attach.URL
105-
}
105+
rmsg := config.Message{
106+
Account: b.Account,
107+
Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg",
108+
UserID: m.Author.ID,
109+
ID: m.ID,
110+
Extra: make(map[string][]interface{}),
106111
}
107112

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-
110113
b.Log.Debugf("== Receiving event %#v", m.Message)
111114

112115
if m.Content != "" {
@@ -138,8 +141,21 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
138141
}
139142
}
140143

141-
// no empty messages
142-
if rmsg.Text == "" {
144+
if len(m.Attachments) > 0 {
145+
if b.Config.GetBool("UseNaitiveUpload") {
146+
b.handleDownloadFile(&rmsg, m)
147+
} else {
148+
// add the url of the attachments to content
149+
for _, attach := range m.Attachments {
150+
m.Content = m.Content + "\n" + attach.URL
151+
}
152+
}
153+
}
154+
155+
hasAttachment := len(rmsg.Extra["file"]) > 0
156+
157+
// no empty messages unless has attachment
158+
if rmsg.Text == "" && !hasAttachment {
143159
return
144160
}
145161

@@ -279,3 +295,17 @@ func handleEmbed(embed *discordgo.MessageEmbed) string {
279295

280296
return result
281297
}
298+
299+
func (b *Bdiscord) handleDownloadFile(rmsg *config.Message, m *discordgo.MessageCreate) error {
300+
for _, attach := range m.Attachments {
301+
data, err := helper.DownloadFile(attach.URL)
302+
303+
if err != nil {
304+
return fmt.Errorf("download %s failed %#v", attach.URL, err)
305+
}
306+
307+
helper.HandleDownloadData(b.Log, rmsg, path.Base(attach.URL), rmsg.Text, attach.URL, data, b.General)
308+
}
309+
310+
return nil
311+
}

matterbridge.toml.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,9 @@ ShowEmbeds=false
907907
# Example: ["irc"]
908908
UseLocalAvatar=[]
909909

910+
# UseNaitiveUpload will use a naitive upload when relaying from discord to other platforms rather than using public links
911+
UseNaitiveUpload=false
912+
910913
# UseUserName shows the username instead of the server nickname
911914
UseUserName=false
912915

0 commit comments

Comments
 (0)