builder.go 5.2 KB

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