renderer.go 3.7 KB

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