소스 검색

feat(util): parse referenced messages (#19)

rigormorrtiss 4 년 전
부모
커밋
3df9adabb3
1개의 변경된 파일38개의 추가작업 그리고 15개의 파일을 삭제
  1. 38 15
      util/discord.go

+ 38 - 15
util/discord.go

@@ -12,19 +12,10 @@ import (
 func WriteMessage(v *tview.TextView, s *state.State, m discord.Message) {
 	var b strings.Builder
 
-	if s.Ready().User.ID == m.Author.ID {
-		b.WriteString("[#ffb86c::b]")
-		b.WriteString(m.Author.Username)
-		b.WriteString("[-:-:-] ")
-	} else {
-		b.WriteString("[#ff5555::b]")
-		b.WriteString(m.Author.Username)
-		b.WriteString("[-:-:-] ")
-	}
-
-	if m.Author.Bot {
-		b.WriteString("[#bd93f9]BOT[-:-:-] ")
-	}
+	// > AUTHOR_USERNAME MESSAGE_CONTENT
+	writeReferencedMessage(&b, m.ReferencedMessage)
+	// AUTHOR_USERNAME MESSAGE_CONTENT
+	writeAuthor(&b, s, m.Author)
 
 	if m.Content != "" {
 		b.WriteString(m.Content)
@@ -39,11 +30,43 @@ func WriteMessage(v *tview.TextView, s *state.State, m discord.Message) {
 		b.WriteString("\n<EMBED(S)>")
 	}
 
-	attachments := m.Attachments
+	writeAttachments(&b, m.Attachments)
+
+	fmt.Fprintln(v, b.String())
+}
+
+func writeAttachments(b *strings.Builder, attachments []discord.Attachment) {
 	for i := range attachments {
 		b.WriteString("\n")
 		b.WriteString(attachments[i].URL)
 	}
+}
 
-	fmt.Fprintln(v, b.String())
+func writeAuthor(b *strings.Builder, s *state.State, u discord.User) {
+	if s.Ready().User.ID == u.ID {
+		b.WriteString("[#ffb86c::b]")
+		b.WriteString(u.Username)
+	} else {
+		b.WriteString("[#ff5555::b]")
+		b.WriteString(u.Username)
+	}
+
+	b.WriteString("[-:-:-] ")
+
+	if u.Bot {
+		b.WriteString("[#bd93f9]BOT[-:-:-] ")
+	}
+}
+
+func writeReferencedMessage(b *strings.Builder, rm *discord.Message) {
+	if rm != nil {
+		b.WriteRune('\u256D')
+		b.WriteString(" [#ff5555::d]")
+		b.WriteString(rm.Author.Username)
+		b.WriteString("[-:-:] ")
+
+		b.WriteString(rm.Content)
+		b.WriteString("\n")
+		b.WriteString("[-:-:-]")
+	}
 }