renderer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 renderMessage(m *discordgo.Message) {
  13. var b strings.Builder
  14. switch m.Type {
  15. case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
  16. // Define a new region and assign message ID as the region ID.
  17. // Learn more:
  18. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  19. b.WriteString("[\"")
  20. b.WriteString(m.ID)
  21. b.WriteString("\"]")
  22. // Render the message associated with crosspost, channel follow add,
  23. // pin, or a reply.
  24. if rm := m.ReferencedMessage; rm != nil {
  25. b.WriteString(" ╭ ")
  26. b.WriteString("[::d]")
  27. parseAuthor(&b, rm.Author)
  28. if rm.Content != "" {
  29. rm.Content = parseMentions(rm.Content, rm.Mentions)
  30. b.WriteString(parseMarkdown(rm.Content))
  31. }
  32. b.WriteString("[::-]\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. fmt.Fprintln(messagesTextView, b.String())
  63. case discordgo.MessageTypeGuildMemberJoin:
  64. b.WriteString("[#5865F2]")
  65. b.WriteString(m.Author.Username)
  66. b.WriteString("[-] joined the server")
  67. fmt.Fprintln(messagesTextView, b.String())
  68. }
  69. }
  70. func parseMentions(content string, mentions []*discordgo.User) string {
  71. for _, mUser := range mentions {
  72. var color string
  73. if mUser.ID == session.State.User.ID {
  74. color = "[:#5865F2]"
  75. } else {
  76. color = "[#EB459E]"
  77. }
  78. content = strings.NewReplacer(
  79. // <@USER_ID>
  80. "<@"+mUser.ID+">",
  81. color+"@"+mUser.Username+"[-:-]",
  82. // <@!USER_ID>
  83. "<@!"+mUser.ID+">",
  84. color+"@"+mUser.Username+"[-:-]",
  85. ).Replace(content)
  86. }
  87. return content
  88. }
  89. func parseAuthor(b *strings.Builder, u *discordgo.User) {
  90. if u.ID == session.State.User.ID {
  91. b.WriteString("[#57F287]")
  92. } else {
  93. b.WriteString("[#ED4245]")
  94. }
  95. b.WriteString(u.Username)
  96. b.WriteString("[-] ")
  97. // If the message author is a bot account, render the message with bot label
  98. // for distinction.
  99. if u.Bot {
  100. b.WriteString("[#EB459E]BOT[-] ")
  101. }
  102. }
  103. func parseMarkdown(md string) string {
  104. var res string
  105. res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
  106. res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
  107. res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
  108. res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
  109. return res
  110. }