renderer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/ayntgl/discordgo"
  6. )
  7. func renderMessages(cID string) {
  8. ms, err := session.ChannelMessages(cID, conf.GetMessagesLimit, "", "", "")
  9. if err != nil {
  10. return
  11. }
  12. for i := len(ms) - 1; i >= 0; i-- {
  13. selectedChannel.Messages = append(selectedChannel.Messages, ms[i])
  14. renderMessage(ms[i])
  15. }
  16. }
  17. func renderMessage(m *discordgo.Message) {
  18. var b strings.Builder
  19. switch m.Type {
  20. case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
  21. // Define a new region and assign message ID as the region ID.
  22. // Learn more:
  23. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  24. b.WriteString("[\"")
  25. b.WriteString(m.ID)
  26. b.WriteString("\"]")
  27. // Render the message associated with crosspost, channel follow add,
  28. // pin, or a reply.
  29. if rm := m.ReferencedMessage; rm != nil {
  30. b.WriteString(" ╭ ")
  31. b.WriteString("[::d]")
  32. parseAuthor(&b, rm.Author)
  33. if rm.Content != "" {
  34. rm.Content = parseMentions(rm.Content, rm.Mentions)
  35. b.WriteString(rm.Content)
  36. }
  37. b.WriteString("[::-]\n")
  38. }
  39. // Render the author of the message.
  40. parseAuthor(&b, m.Author)
  41. // If the message content is not empty, parse the message mentions
  42. // (users mentioned in the message) and render the message content.
  43. if m.Content != "" {
  44. m.Content = parseMentions(m.Content, m.Mentions)
  45. b.WriteString(m.Content)
  46. }
  47. // If the edited timestamp of the message is not empty; it implies that
  48. // the message has been edited, hence render the message with edited
  49. // label for distinction
  50. if m.EditedTimestamp != "" {
  51. b.WriteString(" [::d](edited)[::-]")
  52. }
  53. // TODO: render message embeds
  54. for range m.Embeds {
  55. b.WriteString("\n<EMBED>")
  56. }
  57. // Render the message attachments (attached files to the message).
  58. for _, a := range m.Attachments {
  59. b.WriteString("\n[")
  60. b.WriteString(a.Filename)
  61. b.WriteString("]: ")
  62. b.WriteString(a.URL)
  63. }
  64. // Tags with no region ID ([""]) do not start new regions. They can
  65. // therefore be used to mark the end of a region.
  66. b.WriteString("[\"\"]")
  67. fmt.Fprintln(messagesTextView, b.String())
  68. case discordgo.MessageTypeGuildMemberJoin:
  69. b.WriteString("[#5865F2]")
  70. b.WriteString(m.Author.Username)
  71. b.WriteString("[-] joined the server")
  72. fmt.Fprintln(messagesTextView, b.String())
  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. }