builder.go 4.9 KB

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