|
1 | 1 | package bdiscord
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
| 5 | + |
4 | 6 | "github.com/42wim/matterbridge/bridge/config"
|
5 | 7 | "github.com/bwmarrin/discordgo"
|
6 | 8 | "github.com/davecgh/go-spew/spew"
|
@@ -82,6 +84,45 @@ func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdat
|
82 | 84 | }
|
83 | 85 | }
|
84 | 86 |
|
| 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 | + |
85 | 126 | func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //nolint:unparam
|
86 | 127 | if m.GuildID != b.guildID {
|
87 | 128 | 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
|
153 | 194 | // Replace emotes
|
154 | 195 | rmsg.Text = replaceEmotes(rmsg.Text)
|
155 | 196 |
|
| 197 | + // Handle Reply thread |
| 198 | + rmsg.Text = b.handleQuote(s, m.Message, rmsg.Text) |
| 199 | + |
156 | 200 | // Add our parent id if it exists, and if it's not referring to a message in another channel
|
157 | 201 | if ref := m.MessageReference; ref != nil && ref.ChannelID == m.ChannelID {
|
158 | 202 | rmsg.ParentID = ref.MessageID
|
|
0 commit comments