builder.go 5.3 KB

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