|
1 | 1 | package bdiscord |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
| 5 | + |
4 | 6 | "github.com/bwmarrin/discordgo" |
5 | 7 | "github.com/davecgh/go-spew/spew" |
6 | 8 | "github.com/matterbridge-org/matterbridge/bridge/config" |
@@ -83,6 +85,45 @@ func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdat |
83 | 85 | } |
84 | 86 | } |
85 | 87 |
|
| 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 | + |
86 | 127 | func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //nolint:unparam |
87 | 128 | if m.GuildID != b.guildID { |
88 | 129 | 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 |
184 | 225 | // Replace emotes |
185 | 226 | rmsg.Text = replaceEmotes(rmsg.Text) |
186 | 227 |
|
| 228 | + // Handle Reply thread |
| 229 | + rmsg.Text = b.handleQuote(s, m.Message, rmsg.Text) |
| 230 | + |
187 | 231 | // Add our parent id if it exists, and if it's not referring to a message in another channel |
188 | 232 | if ref := m.MessageReference; ref != nil && ref.ChannelID == m.ChannelID { |
189 | 233 | rmsg.ParentID = ref.MessageID |
|
0 commit comments