renderer.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/ayntgl/discordgo"
  6. )
  7. var boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`)
  8. var italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`)
  9. var underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`)
  10. var strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
  11. func renderMessage(m *discordgo.Message) {
  12. var b strings.Builder
  13. switch m.Type {
  14. case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
  15. // Define a new region and assign message ID as the region ID.
  16. // Learn more:
  17. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  18. b.WriteString("[\"")
  19. b.WriteString(m.ID)
  20. b.WriteString("\"]")
  21. // Render the message associated with crosspost, channel follow add,
  22. // pin, or a reply.
  23. if rm := m.ReferencedMessage; rm != nil {
  24. b.WriteString(" ╭ ")
  25. b.WriteString("[::d]")
  26. parseAuthor(&b, rm.Author)
  27. if rm.Content != "" {
  28. rm.Content = parseMentions(rm.Content, rm.Mentions)
  29. b.WriteString(parseMarkdown(rm.Content))
  30. }
  31. b.WriteString("[::-]")
  32. b.WriteByte('\n')
  33. }
  34. // Render the author of the message.
  35. parseAuthor(&b, m.Author)
  36. // If the message content is not empty, parse the message mentions
  37. // (users mentioned in the message) and render the message content.
  38. if m.Content != "" {
  39. m.Content = parseMentions(m.Content, m.Mentions)
  40. b.WriteString(parseMarkdown(m.Content))
  41. }
  42. // If the edited timestamp of the message is not empty; it implies that
  43. // the message has been edited, hence render the message with edited
  44. // label for distinction
  45. if m.EditedTimestamp != "" {
  46. b.WriteString(" [::d](edited)[::-]")
  47. }
  48. // TODO: render message embeds
  49. for range m.Embeds {
  50. b.WriteString("\n<EMBED>")
  51. }
  52. // Render the message attachments (attached files to the message).
  53. for _, a := range m.Attachments {
  54. b.WriteString("\n[")
  55. b.WriteString(a.Filename)
  56. b.WriteString("]: ")
  57. b.WriteString(a.URL)
  58. }
  59. // Tags with no region ID ([""]) do not start new regions. They can
  60. // therefore be used to mark the end of a region.
  61. b.WriteString("[\"\"]")
  62. b.WriteByte('\n')
  63. case discordgo.MessageTypeGuildMemberJoin:
  64. b.WriteString("[#5865F2]")
  65. b.WriteString(m.Author.Username)
  66. b.WriteString("[-] joined the server")
  67. b.WriteByte('\n')
  68. }
  69. if str := b.String(); str != "" {
  70. b := make([]byte, len(str)+1)
  71. copy(b, str)
  72. messagesTextView.Write(b)
  73. }
  74. }
  75. func parseMentions(content string, mentions []*discordgo.User) string {
  76. for _, mUser := range mentions {
  77. var color string
  78. if mUser.ID == session.State.User.ID {
  79. color = "[:#5865F2]"
  80. } else {
  81. color = "[#EB459E]"
  82. }
  83. content = strings.NewReplacer(
  84. // <@USER_ID>
  85. "<@"+mUser.ID+">",
  86. color+"@"+mUser.Username+"[-:-]",
  87. // <@!USER_ID>
  88. "<@!"+mUser.ID+">",
  89. color+"@"+mUser.Username+"[-:-]",
  90. ).Replace(content)
  91. }
  92. return content
  93. }
  94. func parseAuthor(b *strings.Builder, u *discordgo.User) {
  95. if u.ID == session.State.User.ID {
  96. b.WriteString("[#57F287]")
  97. } else {
  98. b.WriteString("[#ED4245]")
  99. }
  100. b.WriteString(u.Username)
  101. b.WriteString("[-] ")
  102. // If the message author is a bot account, render the message with bot label
  103. // for distinction.
  104. if u.Bot {
  105. b.WriteString("[#EB459E]BOT[-] ")
  106. }
  107. }
  108. func parseMarkdown(md string) string {
  109. var res string
  110. res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
  111. res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
  112. res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
  113. res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
  114. return res
  115. }