Replace user IDs with usernames in Mentions (#356)

Co-authored-by: Ayyan <119342035+ayn2op@users.noreply.github.com>
This commit is contained in:
cyberme0w 2024-03-17 10:53:37 +00:00 • committed by GitHub
parent 3a2f383d5c
commit c8b6e7f183
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 6 deletions

View file

@ -1,6 +1,7 @@
package cmd
import (
"strings"
"fmt"
"io"
"log"
@ -111,9 +112,28 @@ func (mt *MessagesText) createHeader(w io.Writer, m discord.Message, isReply boo
}
}
func parseIDsToUsernames(m discord.Message) string {
var toReplace []string
for _, mention := range m.Mentions {
toReplace = append(toReplace,
fmt.Sprintf("<@%s>", mention.User.ID.String()),
fmt.Sprintf("__**@%s**__", mention.User.Username),
)
}
return strings.NewReplacer(toReplace...).Replace(m.Content)
}
func (mt *MessagesText) createBody(w io.Writer, m discord.Message, isReply bool) {
var body string
if len(m.Mentions) > 0 {
body = parseIDsToUsernames(m)
} else {
body = m.Content
}
if isReply { fmt.Fprint(w, "[::d]") }
fmt.Fprint(w, markdown.Parse(tview.Escape(m.Content)))
fmt.Fprint(w, markdown.Parse(tview.Escape(body)))
if isReply { fmt.Fprint(w, "[::-]") }
}

View file

@ -13,10 +13,10 @@ var (
)
func Parse(input string) string {
input = boldRe.ReplaceAllString(input, "[::b]$1[::-]")
input = italicRe.ReplaceAllString(input, "[::i]$1[::-]")
input = underlineRe.ReplaceAllString(input, "[::u]$1[::-]")
input = strikethroughRe.ReplaceAllString(input, "[::s]$1[::-]")
input = codeblockRe.ReplaceAllString(input, "[::r]$1[::-]")
input = boldRe.ReplaceAllString(input, "[::b]$1[::B]")
input = italicRe.ReplaceAllString(input, "[::i]$1[::I]")
input = underlineRe.ReplaceAllString(input, "[::u]$1[::U]")
input = strikethroughRe.ReplaceAllString(input, "[::s]$1[::S]")
input = codeblockRe.ReplaceAllString(input, "[::r]$1[::R]")
return input
}