builder.go 5.5 KB

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