فهرست منبع

Replace user IDs with usernames in Mentions (#356)

Co-authored-by: Ayyan <119342035+ayn2op@users.noreply.github.com>
cyberme0w 2 سال پیش
والد
کامیت
c8b6e7f183
2فایلهای تغییر یافته به همراه26 افزوده شده و 6 حذف شده
  1. 21 1
      cmd/messages_text.go
  2. 5 5
      internal/markdown/markdown.go

+ 21 - 1
cmd/messages_text.go

@@ -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, "[::-]") }
 }
 

+ 5 - 5
internal/markdown/markdown.go

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