discord.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package util
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/diamondburned/arikawa/v3/discord"
  6. "github.com/rivo/tview"
  7. )
  8. // WriteMessage parses and writes the parsed message to the provided textview.
  9. func WriteMessage(v *tview.TextView, clientID discord.UserID, m discord.Message) {
  10. var b strings.Builder
  11. switch m.Type {
  12. case discord.DefaultMessage, discord.InlinedReplyMessage:
  13. parseMessage(v, &b, m, clientID)
  14. fmt.Fprintln(v, b.String())
  15. case discord.ThreadStarterMessage:
  16. parseMessage(v, &b, *m.ReferencedMessage, clientID)
  17. fmt.Fprintln(v, b.String())
  18. case discord.GuildMemberJoinMessage:
  19. b.WriteString("[#5865F2]")
  20. b.WriteString(m.Author.Username)
  21. b.WriteString("[-]")
  22. b.WriteString(" joined the server")
  23. fmt.Fprintln(v, b.String())
  24. }
  25. }
  26. func parseMessage(v *tview.TextView, b *strings.Builder, m discord.Message, clientID discord.UserID) {
  27. // $ ╭ AUTHOR_USERNAME (BOT) MESSAGE_CONTENT*linebreak*
  28. parseReferencedMessage(b, clientID, m.ReferencedMessage)
  29. // $ AUTHOR_USERNAME (BOT)*spacee*
  30. parseAuthor(b, clientID, m.Author)
  31. // $ MESSAGE_CONTENT
  32. parseContent(b, m, clientID)
  33. // $ *space*(edited)
  34. parseEditedTimestamp(b, m.EditedTimestamp)
  35. // $ *linebreak*EMBED
  36. parseEmbeds(b, m.Embeds)
  37. // $ *linebreak*ATTACHMENT_URL
  38. parseAttachments(b, m.Attachments)
  39. }
  40. func parseContent(b *strings.Builder, m discord.Message, clientID discord.UserID) {
  41. if m.Content != "" {
  42. m.Content = parseMessageMentions(m.Content, m.Mentions, clientID)
  43. b.WriteString(m.Content)
  44. }
  45. }
  46. func parseEditedTimestamp(b *strings.Builder, t discord.Timestamp) {
  47. if t.IsValid() {
  48. b.WriteString(" [::d](edited)[::-]")
  49. }
  50. }
  51. func parseMessageMentions(content string, mentions []discord.GuildUser, clientID discord.UserID) string {
  52. for _, mUser := range mentions {
  53. var color string
  54. if mUser.ID == clientID {
  55. color = "[#000000:#FEE75C]"
  56. } else {
  57. color = "[:#5865F2]"
  58. }
  59. content = strings.NewReplacer(
  60. // <@USER_ID>
  61. "<@"+mUser.ID.String()+">",
  62. color+"@"+mUser.Username+"[-:-]",
  63. // <@!USER_ID>
  64. "<@!"+mUser.ID.String()+">",
  65. color+"@"+mUser.Username+"[-:-]",
  66. ).Replace(content)
  67. }
  68. return content
  69. }
  70. func parseEmbeds(b *strings.Builder, embeds []discord.Embed) {
  71. for range embeds {
  72. b.WriteString("\n<EMBED>")
  73. }
  74. }
  75. func parseAttachments(b *strings.Builder, attachments []discord.Attachment) {
  76. for _, a := range attachments {
  77. b.WriteString("\n[")
  78. b.WriteString(a.Filename)
  79. b.WriteString("]: ")
  80. b.WriteString(a.URL)
  81. }
  82. }
  83. func parseAuthor(b *strings.Builder, clientID discord.UserID, u discord.User) {
  84. if u.ID == clientID {
  85. b.WriteString("[#57F287]")
  86. } else {
  87. b.WriteString("[#ED4245]")
  88. }
  89. b.WriteString(u.Username)
  90. b.WriteString("[-] ")
  91. if u.Bot {
  92. b.WriteString("[#EB459E]BOT[-] ")
  93. }
  94. }
  95. func parseReferencedMessage(b *strings.Builder, clientID discord.UserID, rm *discord.Message) {
  96. if rm != nil {
  97. b.WriteString(" ╭ ")
  98. if rm.Author.ID == clientID {
  99. b.WriteString("[#57F287::d]")
  100. } else {
  101. b.WriteString("[#ED4245::d]")
  102. }
  103. b.WriteString(rm.Author.Username)
  104. b.WriteString("[-] ")
  105. if rm.Author.Bot {
  106. b.WriteString("[#EB459E]BOT[-] ")
  107. }
  108. if rm.Content != "" {
  109. rm.Content = parseMessageMentions(rm.Content, rm.Mentions, clientID)
  110. b.WriteString(rm.Content)
  111. }
  112. b.WriteString("[::-]\n")
  113. }
  114. }