Support Discord reply

This commit is contained in:
Leko 2022-02-02 23:00:54 +08:00 committed by leko
parent 56e7bd01ca
commit b63eccdecf
3 changed files with 59 additions and 3 deletions

View File

@ -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

View File

@ -1,6 +1,8 @@
package bdiscord
import (
"strings"
"github.com/42wim/matterbridge/bridge/config"
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
@ -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")
@ -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

View File

@ -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
###################################################################