builder.go 5.1 KB

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