|
1 | 1 | package bdiscord
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "path" |
| 6 | + |
4 | 7 | "github.com/42wim/matterbridge/bridge/config"
|
| 8 | + "github.com/42wim/matterbridge/bridge/helper" |
5 | 9 | "github.com/bwmarrin/discordgo"
|
6 | 10 | "github.com/davecgh/go-spew/spew"
|
7 | 11 | )
|
@@ -98,15 +102,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
98 | 102 | return
|
99 | 103 | }
|
100 | 104 |
|
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{}), |
106 | 111 | }
|
107 | 112 |
|
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 |
| - |
110 | 113 | b.Log.Debugf("== Receiving event %#v", m.Message)
|
111 | 114 |
|
112 | 115 | if m.Content != "" {
|
@@ -138,8 +141,21 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
138 | 141 | }
|
139 | 142 | }
|
140 | 143 |
|
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 { |
143 | 159 | return
|
144 | 160 | }
|
145 | 161 |
|
@@ -279,3 +295,17 @@ func handleEmbed(embed *discordgo.MessageEmbed) string {
|
279 | 295 |
|
280 | 296 | return result
|
281 | 297 | }
|
| 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 | +} |
0 commit comments