discord.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package util
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/diamondburned/arikawa/v3/discord"
  6. "github.com/diamondburned/arikawa/v3/state"
  7. "github.com/rivo/tview"
  8. )
  9. func WriteMessage(v *tview.TextView, s *state.State, m discord.Message) {
  10. var b strings.Builder
  11. // > AUTHOR_USERNAME MESSAGE_CONTENT
  12. writeReferencedMessage(&b, m.ReferencedMessage)
  13. // AUTHOR_USERNAME MESSAGE_CONTENT
  14. writeAuthor(&b, s, m.Author)
  15. if m.Content != "" {
  16. b.WriteString(m.Content)
  17. }
  18. if m.EditedTimestamp.IsValid() {
  19. b.WriteString(" [::d](edited)[-:-:-]")
  20. }
  21. // TODO(rigormorrtiss): display the message embed using "special" format
  22. if len(m.Embeds) > 0 {
  23. b.WriteString("\n<EMBED(S)>")
  24. }
  25. writeAttachments(&b, m.Attachments)
  26. fmt.Fprintln(v, b.String())
  27. }
  28. func writeAttachments(b *strings.Builder, attachments []discord.Attachment) {
  29. for i := range attachments {
  30. b.WriteString("\n")
  31. b.WriteString(attachments[i].URL)
  32. }
  33. }
  34. func writeAuthor(b *strings.Builder, s *state.State, u discord.User) {
  35. if s.Ready().User.ID == u.ID {
  36. b.WriteString("[#ffb86c::b]")
  37. b.WriteString(u.Username)
  38. } else {
  39. b.WriteString("[#ff5555::b]")
  40. b.WriteString(u.Username)
  41. }
  42. b.WriteString("[-:-:-] ")
  43. if u.Bot {
  44. b.WriteString("[#bd93f9]BOT[-:-:-] ")
  45. }
  46. }
  47. func writeReferencedMessage(b *strings.Builder, rm *discord.Message) {
  48. if rm != nil {
  49. b.WriteRune('\u256D')
  50. b.WriteString(" [#ff5555::d]")
  51. b.WriteString(rm.Author.Username)
  52. b.WriteString("[-:-:] ")
  53. b.WriteString(rm.Content)
  54. b.WriteString("\n")
  55. b.WriteString("[-:-:-]")
  56. }
  57. }