discord.go 3.1 KB

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