Skip to content

Commit

Permalink
Support Discord reply
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoOwO committed Dec 7, 2023
1 parent 56e7bd0 commit b63eccd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bridge/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,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
RejoinDelay int // IRC
ReplaceMessages [][]string // all protocols
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/42wim/matterbridge/bridge/config"
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -82,6 +84,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)
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 @@ -153,6 +194,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 @@ -929,6 +929,18 @@ SyncTopic=false
#Default "<clipped message>"
MessageClipped="<clipped message>"

#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

0 comments on commit b63eccd

Please sign in to comment.