Skip to content

Commit 52bf0ee

Browse files
committed
Support Discord Reply
1 parent 643f6b7 commit 52bf0ee

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

bridge/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ type Protocol struct {
178178
PrefixMessagesWithNick bool // mattemost, slack
179179
PreserveThreading bool // slack
180180
Protocol string // all protocols
181-
QuoteDisable bool // telegram
182-
QuoteFormat string // telegram
183-
QuoteLengthLimit int // telegram
181+
QuoteDisable bool // telegram,discord
182+
QuoteFormat string // telegram,discord
183+
QuoteLengthLimit int // telegram,discord
184184
RealName string // IRC
185185
RecoveryKey string // matrix
186186
RejoinDelay int // IRC

bridge/discord/handlers.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bdiscord
22

33
import (
4+
"strings"
5+
46
"github.com/bwmarrin/discordgo"
57
"github.com/davecgh/go-spew/spew"
68
"github.com/matterbridge-org/matterbridge/bridge/config"
@@ -83,6 +85,45 @@ func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdat
8385
}
8486
}
8587

88+
func (b *Bdiscord) handleQuote(s *discordgo.Session, m *discordgo.Message, msg string) string {
89+
if b.GetBool("QuoteDisable") {
90+
return msg
91+
}
92+
if m.MessageReference == nil {
93+
return msg
94+
}
95+
refMsgRef := m.MessageReference
96+
refMsg, err := s.ChannelMessage(refMsgRef.ChannelID, refMsgRef.MessageID)
97+
if err != nil {
98+
b.Log.Errorf("Error getting quoted message %s:%s: %s", refMsgRef.ChannelID, refMsgRef.MessageID, err)
99+
return msg
100+
}
101+
102+
quoteMessage := refMsg.Content
103+
quoteNick := refMsg.Author.Username
104+
fromWebhook := m.WebhookID != ""
105+
if !fromWebhook && b.GetBool("UseDiscriminator") {
106+
quoteNick += "#" + refMsg.Author.Discriminator
107+
}
108+
109+
format := b.GetString("quoteformat")
110+
if format == "" {
111+
format = "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"
112+
}
113+
quoteMessagelength := len([]rune(quoteMessage))
114+
if b.GetInt("QuoteLengthLimit") != 0 && quoteMessagelength >= b.GetInt("QuoteLengthLimit") {
115+
runes := []rune(quoteMessage)
116+
quoteMessage = string(runes[0:b.GetInt("QuoteLengthLimit")])
117+
if quoteMessagelength > b.GetInt("QuoteLengthLimit") {
118+
quoteMessage += "..."
119+
}
120+
}
121+
format = strings.ReplaceAll(format, "{MESSAGE}", m.Content)
122+
format = strings.ReplaceAll(format, "{QUOTENICK}", quoteNick)
123+
format = strings.ReplaceAll(format, "{QUOTEMESSAGE}", quoteMessage)
124+
return format
125+
}
126+
86127
func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //nolint:unparam
87128
if m.GuildID != b.guildID {
88129
b.Log.Debugf("Ignoring messageCreate because it originates from a different guild")
@@ -184,6 +225,9 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
184225
// Replace emotes
185226
rmsg.Text = replaceEmotes(rmsg.Text)
186227

228+
// Handle Reply thread
229+
rmsg.Text = b.handleQuote(s, m.Message, rmsg.Text)
230+
187231
// Add our parent id if it exists, and if it's not referring to a message in another channel
188232
if ref := m.MessageReference; ref != nil && ref.ChannelID == m.ChannelID {
189233
rmsg.ParentID = ref.MessageID

matterbridge.toml.sample

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,18 @@ MessageClipped="<clipped message>"
957957
# Default 1
958958
MessageSplitMaxCount=3
959959

960+
#Disable quoted/reply messages
961+
#OPTIONAL (default false)
962+
QuoteDisable=false
963+
964+
#Set the max. quoted length if 0 the whole message will be quoted
965+
#OPTIONAL (default 0)
966+
QuoteLengthLimit=0
967+
968+
#Format quoted/reply messages
969+
#OPTIONAL (default "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})")
970+
QuoteFormat="{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"
971+
960972
###################################################################
961973
#telegram section
962974
###################################################################

0 commit comments

Comments
 (0)