Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 80598761884a7123951575c76b07dbb42963d363 Author: Wohlstand <admin@wohlnet.ru> Date: Tue Jun 03 17:54:22 2025 +0000 Discord: Download files, don't send as URLs And make that as an option diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 5b6b7e4..f3e623e 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -366 +369 @@ type Bdiscord struct {   userMemberMap map[string]*discordgo.Member   nickMemberMap map[string]*discordgo.Member   + // Never send Discord's attachments as URLs, always download and re-upload them to the destination as regular files + alwaysDownloadFiles bool +   // Webhook specific logic   useAutoWebhooks bool   transmitter *transmitter.Transmitter @@ -576 +608 @@ func New(cfg *bridge.Config) bridge.Bridger {   b.nickMemberMap = make(map[string]*discordgo.Member)   b.channelInfoMap = make(map[string]*config.ChannelInfo)   + b.alwaysDownloadFiles = b.GetBool("AlwaysDownloadFiles") +   b.useAutoWebhooks = b.GetBool("AutoWebhooks")   if b.useAutoWebhooks {   b.Log.Debug("Using automatic webhooks") diff --git a/bridge/discord/handlers.go b/bridge/discord/handlers.go index b969122..d442fca 100644 --- a/bridge/discord/handlers.go +++ b/bridge/discord/handlers.go @@ -46 +47 @@ import (   "github.com/bwmarrin/discordgo"   "github.com/davecgh/go-spew/spew"   "github.com/matterbridge-org/matterbridge/bridge/config" + "github.com/matterbridge-org/matterbridge/bridge/helper"  )    func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { //nolint:unparam @@ -9815 +9944 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat   return   }   + 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{})} +   // add the url of the attachments to content   if len(m.Attachments) > 0 { + first := true   for _, attach := range m.Attachments { - m.Content = m.Content + "\n" + attach.URL + if b.alwaysDownloadFiles { + var url, name, caption string + + url = attach.URL + name = attach.Filename + + err = helper.HandleDownloadSize(b.Log, &rmsg, name, int64(attach.Size), b.General) + if err != nil { + return + } + data, err := helper.DownloadFile(url) + if err != nil { + return + } + + if first { + caption = m.Content + if caption == "" { + caption = name + } + first = false + } else { + caption = "" + } + + helper.HandleDownloadData(b.Log, &rmsg, name, caption, "", data, b.General) + } else { + 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} -   b.Log.Debugf("== Receiving event %#v", m.Message)     if m.Content != "" { @@ -1397 +1697 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat   }     // no empty messages - if rmsg.Text == "" { + if rmsg.Text == "" && len(m.Attachments) == 0 {   return   }