builder.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package ui
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/ayntgl/discordgo"
  7. )
  8. func buildMessage(app *App, m *discordgo.Message) []byte {
  9. var b strings.Builder
  10. switch m.Type {
  11. case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
  12. // Define a new region and assign message ID as the region ID.
  13. // Learn more:
  14. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  15. b.WriteString("[\"")
  16. b.WriteString(m.ID)
  17. b.WriteString("\"]")
  18. // Build the message associated with crosspost, channel follow add, pin, or a reply.
  19. buildReferencedMessage(&b, m.ReferencedMessage, app.Session.State.User.ID)
  20. if app.Config.General.Timestamps {
  21. b.WriteString("[::d]")
  22. b.WriteString(m.Timestamp.Format(time.Stamp))
  23. b.WriteString("[::-]")
  24. b.WriteByte(' ')
  25. }
  26. // Build the author of this message.
  27. buildAuthor(&b, m.Author, app.Session.State.User.ID)
  28. // Build the contents of the message.
  29. buildContent(&b, m, app.Session.State.User.ID)
  30. if m.EditedTimestamp != nil {
  31. b.WriteString(" [::d](edited)[::-]")
  32. }
  33. // Build the embeds associated with the message.
  34. buildEmbeds(&b, m.Embeds)
  35. // Build the message attachments (attached files to the message).
  36. buildAttachments(&b, m.Attachments)
  37. // Tags with no region ID ([""]) do not start new regions. They can
  38. // therefore be used to mark the end of a region.
  39. b.WriteString("[\"\"]")
  40. b.WriteByte('\n')
  41. case discordgo.MessageTypeGuildMemberJoin:
  42. b.WriteString("[#5865F2]")
  43. b.WriteString(m.Author.Username)
  44. b.WriteString("[-] joined the server.")
  45. b.WriteByte('\n')
  46. case discordgo.MessageTypeCall:
  47. b.WriteString("[#5865F2]")
  48. b.WriteString(m.Author.Username)
  49. b.WriteString("[-] started a call.")
  50. b.WriteByte('\n')
  51. case discordgo.MessageTypeChannelPinnedMessage:
  52. b.WriteString("[#5865F2]")
  53. b.WriteString(m.Author.Username)
  54. b.WriteString("[-] pinned a message.")
  55. b.WriteByte('\n')
  56. }
  57. if str := b.String(); str != "" {
  58. b := make([]byte, len(str)+1)
  59. copy(b, str)
  60. return b
  61. }
  62. return nil
  63. }
  64. func buildReferencedMessage(b *strings.Builder, rm *discordgo.Message, clientID string) {
  65. if rm != nil {
  66. b.WriteString(" ╭ ")
  67. b.WriteString("[::d]")
  68. buildAuthor(b, rm.Author, clientID)
  69. if rm.Content != "" {
  70. rm.Content = buildMentions(rm.Content, rm.Mentions, clientID)
  71. b.WriteString(parseMarkdown(rm.Content))
  72. }
  73. b.WriteString("[::-]")
  74. b.WriteByte('\n')
  75. }
  76. }
  77. func buildContent(b *strings.Builder, m *discordgo.Message, clientID string) {
  78. if m.Content != "" {
  79. m.Content = buildMentions(m.Content, m.Mentions, clientID)
  80. b.WriteString(parseMarkdown(m.Content))
  81. }
  82. }
  83. func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
  84. for _, e := range es {
  85. if e.Type != discordgo.EmbedTypeRich {
  86. continue
  87. }
  88. var (
  89. embedBuilder strings.Builder
  90. hasHeading bool
  91. )
  92. prefix := fmt.Sprintf("[#%06X]▐[-] ", e.Color)
  93. b.WriteByte('\n')
  94. embedBuilder.WriteString(prefix)
  95. if e.Author != nil {
  96. hasHeading = true
  97. embedBuilder.WriteString("[::u]")
  98. embedBuilder.WriteString(e.Author.Name)
  99. embedBuilder.WriteString("[::-]")
  100. }
  101. if e.Title != "" {
  102. hasHeading = true
  103. embedBuilder.WriteString("[::b]")
  104. embedBuilder.WriteString(e.Title)
  105. embedBuilder.WriteString("[::-]")
  106. }
  107. if e.Description != "" {
  108. if hasHeading {
  109. embedBuilder.WriteString("\n\n")
  110. }
  111. embedBuilder.WriteString(parseMarkdown(e.Description))
  112. }
  113. if len(e.Fields) != 0 {
  114. if hasHeading || e.Description != "" {
  115. embedBuilder.WriteString("\n\n")
  116. }
  117. for i, ef := range e.Fields {
  118. embedBuilder.WriteString("[::b]")
  119. embedBuilder.WriteString(ef.Name)
  120. embedBuilder.WriteString("[::-]")
  121. embedBuilder.WriteByte('\n')
  122. embedBuilder.WriteString(parseMarkdown(ef.Value))
  123. if i != len(e.Fields)-1 {
  124. embedBuilder.WriteString("\n\n")
  125. }
  126. }
  127. }
  128. if e.Footer != nil {
  129. if hasHeading {
  130. embedBuilder.WriteString("\n\n")
  131. }
  132. embedBuilder.WriteString(e.Footer.Text)
  133. }
  134. b.WriteString(strings.ReplaceAll(embedBuilder.String(), "\n", "\n"+prefix))
  135. }
  136. }
  137. func buildAttachments(b *strings.Builder, as []*discordgo.MessageAttachment) {
  138. for _, a := range as {
  139. b.WriteByte('\n')
  140. b.WriteByte('[')
  141. b.WriteString(a.Filename)
  142. b.WriteString("]: ")
  143. b.WriteString(a.URL)
  144. }
  145. }
  146. func buildMentions(content string, mentions []*discordgo.User, clientID string) string {
  147. for _, mUser := range mentions {
  148. var color string
  149. if mUser.ID == clientID {
  150. color = "[:#5865F2]"
  151. } else {
  152. color = "[#EB459E]"
  153. }
  154. content = strings.NewReplacer(
  155. // <@USER_ID>
  156. "<@"+mUser.ID+">",
  157. color+"@"+mUser.Username+"[-:-]",
  158. // <@!USER_ID>
  159. "<@!"+mUser.ID+">",
  160. color+"@"+mUser.Username+"[-:-]",
  161. ).Replace(content)
  162. }
  163. return content
  164. }
  165. func buildAuthor(b *strings.Builder, u *discordgo.User, clientID string) {
  166. if u.ID == clientID {
  167. b.WriteString("[#57F287]")
  168. } else {
  169. b.WriteString("[#ED4245]")
  170. }
  171. b.WriteString(u.Username)
  172. b.WriteString("[-] ")
  173. // If the message author is a bot account, render the message with bot label
  174. // for distinction.
  175. if u.Bot {
  176. b.WriteString("[#EB459E]BOT[-] ")
  177. }
  178. }