Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ type Protocol struct {
PrefixMessagesWithNick bool // mattemost, slack
PreserveThreading bool // slack
Protocol string // all protocols
QuoteDisable bool // telegram
QuoteFormat string // telegram
QuoteLengthLimit int // telegram
QuoteDisable bool // telegram,discord
QuoteFormat string // telegram,discord
QuoteLengthLimit int // telegram,discord
RealName string // IRC
RecoveryKey string // matrix
RejoinDelay int // IRC
Expand Down
44 changes: 44 additions & 0 deletions bridge/discord/handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bdiscord

import (
"strings"

"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
"github.com/matterbridge-org/matterbridge/bridge/config"
Expand Down Expand Up @@ -83,6 +85,45 @@ func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdat
}
}

func (b *Bdiscord) handleQuote(s *discordgo.Session, m *discordgo.Message, msg string) string {
if b.GetBool("QuoteDisable") {
return msg
}
if m.MessageReference == nil {
return msg
}
refMsgRef := m.MessageReference
refMsg, err := s.ChannelMessage(refMsgRef.ChannelID, refMsgRef.MessageID)
Copy link
Copy Markdown
Collaborator

@selfhoster1312 selfhoster1312 Jan 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a blocking network request, and should not be performed on the main thread (because it will block processing other events if the discord API takes some time). There's many ways to work around that. One would be instead of returning string from the method, you could return a boolean indicating whether the method is in fact handling a reply:

  • if it is, just ignore it in the createMessage method, and start a new goroutine in handleQuote
  • if it's not, keep going with the original msg

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative could be to store the last ~100 messages or so per channel, so we don't have to do a network query at all.

if err != nil {
b.Log.Errorf("Error getting quoted message %s:%s: %s", refMsgRef.ChannelID, refMsgRef.MessageID, err)
return msg
}

quoteMessage := refMsg.Content
quoteNick := refMsg.Author.Username
fromWebhook := m.WebhookID != ""
if !fromWebhook && b.GetBool("UseDiscriminator") {
quoteNick += "#" + refMsg.Author.Discriminator
}

format := b.GetString("quoteformat")
if format == "" {
format = "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"
}
quoteMessagelength := len([]rune(quoteMessage))
if b.GetInt("QuoteLengthLimit") != 0 && quoteMessagelength >= b.GetInt("QuoteLengthLimit") {
runes := []rune(quoteMessage)
quoteMessage = string(runes[0:b.GetInt("QuoteLengthLimit")])
if quoteMessagelength > b.GetInt("QuoteLengthLimit") {
quoteMessage += "..."
}
}
format = strings.ReplaceAll(format, "{MESSAGE}", m.Content)
format = strings.ReplaceAll(format, "{QUOTENICK}", quoteNick)
format = strings.ReplaceAll(format, "{QUOTEMESSAGE}", quoteMessage)
return format
}

func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //nolint:unparam
if m.GuildID != b.guildID {
b.Log.Debugf("Ignoring messageCreate because it originates from a different guild")
Expand Down Expand Up @@ -184,6 +225,9 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
// Replace emotes
rmsg.Text = replaceEmotes(rmsg.Text)

// Handle Reply thread
rmsg.Text = b.handleQuote(s, m.Message, rmsg.Text)

// Add our parent id if it exists, and if it's not referring to a message in another channel
if ref := m.MessageReference; ref != nil && ref.ChannelID == m.ChannelID {
rmsg.ParentID = ref.MessageID
Expand Down
12 changes: 12 additions & 0 deletions matterbridge.toml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,18 @@ MessageClipped="<clipped message>"
# Default 1
MessageSplitMaxCount=3

#Disable quoted/reply messages
#OPTIONAL (default false)
QuoteDisable=false

#Set the max. quoted length if 0 the whole message will be quoted
#OPTIONAL (default 0)
QuoteLengthLimit=0

#Format quoted/reply messages
#OPTIONAL (default "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})")
QuoteFormat="{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"

###################################################################
#telegram section
###################################################################
Expand Down
Loading