Skip to content

Commit

Permalink
Add option for using a naitive upload when relaying from discord
Browse files Browse the repository at this point in the history
Resolves 42wim#1965
  • Loading branch information
yousefmansy1 committed Feb 24, 2023
1 parent 476205c commit 014c008
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ type Protocol struct {
URL string // mattermost, slack // DEPRECATED
UseAPI bool // mattermost, slack
UseLocalAvatar []string // discord
UseNativeUpload bool // discord
UseSASL bool // IRC
UseTLS bool // IRC
UseDiscriminator bool // discord
Expand Down
48 changes: 39 additions & 9 deletions bridge/discord/handlers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package bdiscord

import (
"fmt"
"path"

"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
)
Expand Down Expand Up @@ -98,15 +102,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
return
}

// add the url of the attachments to content
if len(m.Attachments) > 0 {
for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL
}
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{}),
}

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}

b.Log.Debugf("== Receiving event %#v", m.Message)

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

// no empty messages
if rmsg.Text == "" {
if len(m.Attachments) > 0 {
if b.Config.GetBool("UseNativeUpload") {
b.handleDownloadFile(&rmsg, m)
} else {
// add the url of the attachments to content
for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL
}
}
}

hasAttachment := len(rmsg.Extra["file"]) > 0

// no empty messages unless has attachment
if rmsg.Text == "" && !hasAttachment {
return
}

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

return result
}

func (b *Bdiscord) handleDownloadFile(rmsg *config.Message, m *discordgo.MessageCreate) error {
for _, attach := range m.Attachments {
data, err := helper.DownloadFile(attach.URL)

if err != nil {
return fmt.Errorf("download %s failed %#v", attach.URL, err)
}

helper.HandleDownloadData(b.Log, rmsg, path.Base(attach.URL), rmsg.Text, attach.URL, data, b.General)
}

return nil
}
3 changes: 3 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,9 @@ ShowEmbeds=false
# Example: ["irc"]
UseLocalAvatar=[]

# UseNativeUpload will use a naitive upload when relaying from discord to other platforms rather than using public links
UseNativeUpload=false

# UseUserName shows the username instead of the server nickname
UseUserName=false

Expand Down

0 comments on commit 014c008

Please sign in to comment.