Skip to content

Commit 4c09867

Browse files
committed
Support Discord reply
1 parent c4157a4 commit 4c09867

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

bridge/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ type Protocol struct {
138138
PrefixMessagesWithNick bool // mattemost, slack
139139
PreserveThreading bool // slack
140140
Protocol string // all protocols
141-
QuoteDisable bool // telegram
142-
QuoteFormat string // telegram
143-
QuoteLengthLimit int // telegram
141+
QuoteDisable bool // telegram,discord
142+
QuoteFormat string // telegram,discord
143+
QuoteLengthLimit int // telegram,discord
144144
RealName string // IRC
145145
RejoinDelay int // IRC
146146
ReplaceMessages [][]string // all protocols

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/42wim/matterbridge/bridge/config"
57
"github.com/bwmarrin/discordgo"
68
"github.com/davecgh/go-spew/spew"
@@ -82,6 +84,45 @@ func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdat
8284
}
8385
}
8486

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

197+
// Handle Reply thread
198+
rmsg.Text = b.handleQuote(s, m.Message, rmsg.Text)
199+
156200
// Add our parent id if it exists, and if it's not referring to a message in another channel
157201
if ref := m.MessageReference; ref != nil && ref.ChannelID == m.ChannelID {
158202
rmsg.ParentID = ref.MessageID

matterbridge.toml.sample

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,18 @@ MessageClipped="<clipped message>"
936936
# Default 1
937937
MessageSplitMaxCount=3
938938

939+
#Disable quoted/reply messages
940+
#OPTIONAL (default false)
941+
QuoteDisable=false
942+
943+
#Set the max. quoted length if 0 the whole message will be quoted
944+
#OPTIONAL (default 0)
945+
QuoteLengthLimit=0
946+
947+
#Format quoted/reply messages
948+
#OPTIONAL (default "{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})")
949+
QuoteFormat="{MESSAGE} (re @{QUOTENICK}: {QUOTEMESSAGE})"
950+
939951
###################################################################
940952
#telegram section
941953
###################################################################

0 commit comments

Comments
 (0)