discord.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package util
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/bwmarrin/discordgo"
  7. "github.com/rivo/tview"
  8. )
  9. // WriteMessage parses, renders, and writes a message to the given TextView.
  10. func WriteMessage(v *tview.TextView, m *discordgo.Message, clientID string) {
  11. var b strings.Builder
  12. switch m.Type {
  13. case discordgo.MessageTypeDefault, discordgo.MessageTypeReply:
  14. // Define a new region and assign message ID as the region ID.
  15. // Learn more:
  16. // https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
  17. b.WriteString("[\"")
  18. b.WriteString(m.ID)
  19. b.WriteString("\"]")
  20. // Render the message associated with crosspost, channel follow add,
  21. // pin, or a reply.
  22. if rm := m.ReferencedMessage; rm != nil {
  23. b.WriteString(" ╭ ")
  24. b.WriteString("[::d]")
  25. parseAuthor(&b, rm.Author, clientID)
  26. if rm.Content != "" {
  27. rm.Content = parseMentions(
  28. rm.Content,
  29. rm.Mentions,
  30. clientID,
  31. )
  32. b.WriteString(rm.Content)
  33. }
  34. b.WriteString("[::-]\n")
  35. }
  36. // Render the author of the message.
  37. parseAuthor(&b, m.Author, clientID)
  38. // If the message content is not empty, parse the message mentions
  39. // (users mentioned in the message) and render the message content.
  40. if m.Content != "" {
  41. m.Content = parseMentions(m.Content, m.Mentions, clientID)
  42. b.WriteString(m.Content)
  43. }
  44. // If the edited timestamp of the message is not empty; it implies that
  45. // the message has been edited, hence render the message with edited
  46. // label for distinction
  47. if m.EditedTimestamp != "" {
  48. b.WriteString(" [::d](edited)[::-]")
  49. }
  50. // TODO: render message embeds
  51. for range m.Embeds {
  52. b.WriteString("\n<EMBED>")
  53. }
  54. // Render the message attachments (attached files to the message).
  55. for _, a := range m.Attachments {
  56. b.WriteString("\n[")
  57. b.WriteString(a.Filename)
  58. b.WriteString("]: ")
  59. b.WriteString(a.URL)
  60. }
  61. // Tags with no region ID ([""]) do not start new regions. They can
  62. // therefore be used to mark the end of a region.
  63. b.WriteString("[\"\"]")
  64. fmt.Fprintln(v, b.String())
  65. case discordgo.MessageTypeGuildMemberJoin:
  66. b.WriteString("[#5865F2]")
  67. b.WriteString(m.Author.Username)
  68. b.WriteString("[-] joined the server")
  69. fmt.Fprintln(v, b.String())
  70. }
  71. }
  72. func parseMentions(
  73. content string,
  74. mentions []*discordgo.User,
  75. clientID string,
  76. ) string {
  77. for _, mUser := range mentions {
  78. var color string
  79. if mUser.ID == clientID {
  80. color = "[:#5865F2]"
  81. } else {
  82. color = "[#EB459E]"
  83. }
  84. content = strings.NewReplacer(
  85. // <@USER_ID>
  86. "<@"+mUser.ID+">",
  87. color+"@"+mUser.Username+"[-:-]",
  88. // <@!USER_ID>
  89. "<@!"+mUser.ID+">",
  90. color+"@"+mUser.Username+"[-:-]",
  91. ).Replace(content)
  92. }
  93. return content
  94. }
  95. func parseAuthor(b *strings.Builder, u *discordgo.User, clientID string) {
  96. // If the message author is the client, modify the text color for
  97. // distinction.
  98. if u.ID == clientID {
  99. b.WriteString("[#57F287]")
  100. } else {
  101. b.WriteString("[#ED4245]")
  102. }
  103. b.WriteString(u.Username)
  104. b.WriteString("[-] ")
  105. // If the message author is a bot account, render the message with bot label
  106. // for distinction.
  107. if u.Bot {
  108. b.WriteString("[#EB459E]BOT[-] ")
  109. }
  110. }
  111. type loginResponse struct {
  112. MFA bool `json:"mfa"`
  113. SMS bool `json:"sms"`
  114. Ticket string `json:"ticket"`
  115. Token string `json:"token"`
  116. }
  117. // Login creates a new request to the `/login` endpoint for essential login
  118. // information.
  119. func Login(
  120. s *discordgo.Session,
  121. email, password string,
  122. ) (*loginResponse, error) {
  123. data := struct {
  124. Email string `json:"email"`
  125. Password string `json:"password"`
  126. }{email, password}
  127. resp, err := s.RequestWithBucketID(
  128. "POST",
  129. discordgo.EndpointLogin,
  130. data,
  131. discordgo.EndpointLogin,
  132. )
  133. if err != nil {
  134. return nil, err
  135. }
  136. var lr loginResponse
  137. err = json.Unmarshal(resp, &lr)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return &lr, nil
  142. }
  143. // TOTP creates a new request to `/mfa/totp` endpoint for time-based one-time
  144. // passcode for essential login information
  145. func TOTP(s *discordgo.Session, code, ticket string) (*loginResponse, error) {
  146. data := struct {
  147. Code string `json:"code"`
  148. Ticket string `json:"ticket"`
  149. }{code, ticket}
  150. e := discordgo.EndpointAuth + "mfa/totp"
  151. resp, err := s.RequestWithBucketID("POST", e, data, e)
  152. if err != nil {
  153. return nil, err
  154. }
  155. var lr loginResponse
  156. err = json.Unmarshal(resp, &lr)
  157. if err != nil {
  158. return nil, err
  159. }
  160. return &lr, nil
  161. }