Add slightly hacky support for interaction messages

This commit is contained in:
Tulir Asokan 2023-01-28 16:36:12 +02:00
parent 9cc1666d2b
commit 98cd08e889
2 changed files with 13 additions and 5 deletions

View file

@ -21,6 +21,7 @@ import (
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
@ -33,7 +34,14 @@ import (
var discordExtensions = goldmark.WithExtensions(mdext.SimpleSpoiler, mdext.DiscordUnderline)
var escapeFixer = regexp.MustCompile(`\\(__[^_]|\*\*[^*])`)
func (portal *Portal) renderDiscordMarkdown(text string) event.MessageEventContent {
const msgInteractionTemplate = `> <@%s> used /%s
`
func (portal *Portal) renderDiscordMarkdown(text string, interaction *discordgo.MessageInteraction) event.MessageEventContent {
if interaction != nil {
text = fmt.Sprintf(msgInteractionTemplate, interaction.User.ID, interaction.Name) + text
}
return format.HTMLToContent(portal.renderDiscordMarkdownOnlyHTML(text))
}

View file

@ -976,8 +976,8 @@ func (portal *Portal) handleDiscordMessageCreate(user *User, msg *discordgo.Mess
var parts []database.MessagePart
ts, _ := discordgo.SnowflakeTimestamp(msg.ID)
if msg.Content != "" && !isPlainGifMessage(msg) {
content := portal.renderDiscordMarkdown(msg.Content)
if (msg.Content != "" || msg.Interaction != nil) && !isPlainGifMessage(msg) {
content := portal.renderDiscordMarkdown(msg.Content, msg.Interaction)
content.RelatesTo = threadRelation.Copy()
extraContent := map[string]any{
@ -1151,7 +1151,7 @@ func (portal *Portal) handleDiscordMessageUpdate(user *User, msg *discordgo.Mess
deletedAttachment.Delete()
}
if msg.Content == "" || existing[0].AttachmentID != "" {
if (msg.Content == "" && msg.Interaction == nil) || existing[0].AttachmentID != "" {
portal.log.Debugfln("Dropping non-text edit to %s (message on matrix: %t, text on discord: %t)", msg.ID, existing[0].AttachmentID == "", len(msg.Content) > 0)
return
}
@ -1162,7 +1162,7 @@ func (portal *Portal) handleDiscordMessageUpdate(user *User, msg *discordgo.Mess
content = *converted.Content
extraContent = converted.Extra
} else {
content = portal.renderDiscordMarkdown(msg.Content)
content = portal.renderDiscordMarkdown(msg.Content, msg.Interaction)
extraContent = map[string]any{
"com.beeper.linkpreviews": portal.convertDiscordLinkEmbedsToBeeper(intent, msg.Embeds),
}