builder.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package ui
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/ayntgl/discordgo"
  7. "github.com/ayntgl/discordo/util"
  8. )
  9. func buildMessage(app *App, m *discordgo.Message) []byte {
  10. var b strings.Builder
  11. switch m.Type {
  12. case discordgo.MessageTypeDefault, discordgo.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 discordgo.MessageTypeGuildMemberJoin:
  43. b.WriteString("[#5865F2]")
  44. b.WriteString(m.Author.Username)
  45. b.WriteString("[-] joined the server.")
  46. b.WriteByte('\n')
  47. case discordgo.MessageTypeCall:
  48. b.WriteString("[#5865F2]")
  49. b.WriteString(m.Author.Username)
  50. b.WriteString("[-] started a call.")
  51. b.WriteByte('\n')
  52. case discordgo.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 *discordgo.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(util.ParseMarkdown(rm.Content))
  73. }
  74. b.WriteString("[::-]")
  75. b.WriteByte('\n')
  76. }
  77. }
  78. func buildContent(b *strings.Builder, m *discordgo.Message, clientID string) {
  79. if m.Content != "" {
  80. m.Content = buildMentions(m.Content, m.Mentions, clientID)
  81. b.WriteString(util.ParseMarkdown(m.Content))
  82. }
  83. }
  84. func buildEmbeds(b *strings.Builder, es []*discordgo.MessageEmbed) {
  85. for _, e := range es {
  86. if e.Type != discordgo.EmbedTypeRich {
  87. continue
  88. }
  89. var embedBuilder strings.Builder
  90. var hasHeading bool
  91. prefix := fmt.Sprintf("[#%06X]▐[-] ", e.Color)
  92. b.WriteByte('\n')
  93. embedBuilder.WriteString(prefix)
  94. if e.Author != nil {
  95. hasHeading = true
  96. embedBuilder.WriteString("[::u]")
  97. embedBuilder.WriteString(e.Author.Name)
  98. embedBuilder.WriteString("[::-]")
  99. }
  100. if e.Title != "" {
  101. hasHeading = true
  102. embedBuilder.WriteString("[::b]")
  103. embedBuilder.WriteString(e.Title)
  104. embedBuilder.WriteString("[::-]")
  105. }
  106. if e.Description != "" {
  107. if hasHeading {
  108. embedBuilder.WriteString("\n\n")
  109. }
  110. embedBuilder.WriteString(util.ParseMarkdown(e.Description))
  111. }
  112. if len(e.Fields) != 0 {
  113. if hasHeading || e.Description != "" {
  114. embedBuilder.WriteString("\n\n")
  115. }
  116. for i, ef := range e.Fields {
  117. embedBuilder.WriteString("[::b]")
  118. embedBuilder.WriteString(ef.Name)
  119. embedBuilder.WriteString("[::-]")
  120. embedBuilder.WriteByte('\n')
  121. embedBuilder.WriteString(util.ParseMarkdown(ef.Value))
  122. if i != len(e.Fields)-1 {
  123. embedBuilder.WriteString("\n\n")
  124. }
  125. }
  126. }
  127. if e.Footer != nil {
  128. if hasHeading {
  129. embedBuilder.WriteString("\n\n")
  130. }
  131. embedBuilder.WriteString(e.Footer.Text)
  132. }
  133. b.WriteString(strings.ReplaceAll(embedBuilder.String(), "\n", "\n"+prefix))
  134. }
  135. }
  136. func buildAttachments(b *strings.Builder, as []*discordgo.MessageAttachment) {
  137. for _, a := range as {
  138. b.WriteByte('\n')
  139. b.WriteByte('[')
  140. b.WriteString(a.Filename)
  141. b.WriteString("]: ")
  142. b.WriteString(a.URL)
  143. }
  144. }
  145. func buildMentions(content string, mentions []*discordgo.User, clientID string) string {
  146. for _, mUser := range mentions {
  147. var color string
  148. if mUser.ID == clientID {
  149. color = "[:#5865F2]"
  150. } else {
  151. color = "[#EB459E]"
  152. }
  153. content = strings.NewReplacer(
  154. // <@USER_ID>
  155. "<@"+mUser.ID+">",
  156. color+"@"+mUser.Username+"[-:-]",
  157. // <@!USER_ID>
  158. "<@!"+mUser.ID+">",
  159. color+"@"+mUser.Username+"[-:-]",
  160. ).Replace(content)
  161. }
  162. return content
  163. }
  164. func buildAuthor(b *strings.Builder, u *discordgo.User, clientID string) {
  165. if u.ID == clientID {
  166. b.WriteString("[#57F287]")
  167. } else {
  168. b.WriteString("[#ED4245]")
  169. }
  170. b.WriteString(u.Username)
  171. b.WriteString("[-] ")
  172. // If the message author is a bot account, render the message with bot label
  173. // for distinction.
  174. if u.Bot {
  175. b.WriteString("[#EB459E]BOT[-] ")
  176. }
  177. }