discord.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 (BOT) MESSAGE_CONTENT*linebreak*
  12. writeReferencedMessage(&b, s, m.ReferencedMessage)
  13. // $ AUTHOR_USERNAME (BOT)*spacee*
  14. writeAuthor(&b, s, m.Author)
  15. // $ MESSAGE_CONTENT
  16. writeContent(&b, m.Content)
  17. // $ *space*(edited)
  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. // $ *linebreak*ATTACHMENT_URL
  26. writeAttachments(&b, m.Attachments)
  27. fmt.Fprintln(v, b.String())
  28. }
  29. func writeAttachments(b *strings.Builder, attachments []discord.Attachment) {
  30. for i := range attachments {
  31. b.WriteString("\n")
  32. b.WriteString(attachments[i].URL)
  33. }
  34. }
  35. func writeAuthor(b *strings.Builder, s *state.State, u discord.User) {
  36. if s.Ready().User.ID == u.ID {
  37. b.WriteString("[#59E3E3]")
  38. } else {
  39. b.WriteString("[#E95678]")
  40. }
  41. b.WriteString(u.Username)
  42. b.WriteString("[-:-:-] ")
  43. if u.Bot {
  44. b.WriteString("[#59E3E3]BOT[-:-:-] ")
  45. }
  46. }
  47. func writeReferencedMessage(b *strings.Builder, s *state.State, rm *discord.Message) {
  48. if rm != nil {
  49. b.WriteRune(' ')
  50. b.WriteRune('\u256D')
  51. b.WriteRune(' ')
  52. if s.Ready().User.ID == rm.Author.ID {
  53. b.WriteString("[#59E3E3::d]")
  54. } else {
  55. b.WriteString("[#E95678::d]")
  56. }
  57. b.WriteString(rm.Author.Username)
  58. // Reset foreground
  59. b.WriteString("[-::] ")
  60. writeContent(b, rm.Content)
  61. b.WriteString("[-:-:-]\n")
  62. }
  63. }
  64. func writeContent(b *strings.Builder, c string) {
  65. if c != "" {
  66. c = tview.Escape(c)
  67. b.WriteString(c)
  68. }
  69. }