builder.go 5.3 KB

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