discord.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package util
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/rigormorrtiss/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 = parseMessageMentions(
  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 = parseMessageMentions(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. fmt.Fprintln(v, b.String())
  62. case discordgo.MessageTypeGuildMemberJoin:
  63. b.WriteString("[#5865F2]")
  64. b.WriteString(m.Author.Username)
  65. b.WriteString("[-]")
  66. b.WriteString(" joined the server")
  67. fmt.Fprintln(v, b.String())
  68. }
  69. }
  70. func parseMessageMentions(
  71. content string,
  72. mentions []*discordgo.User,
  73. clientID string,
  74. ) string {
  75. for _, mUser := range mentions {
  76. var color string
  77. if mUser.ID == clientID {
  78. color = "[:#5865F2]"
  79. } else {
  80. color = "[#EB459E]"
  81. }
  82. content = strings.NewReplacer(
  83. // <@USER_ID>
  84. "<@"+mUser.ID+">",
  85. color+"@"+mUser.Username+"[-:-]",
  86. // <@!USER_ID>
  87. "<@!"+mUser.ID+">",
  88. color+"@"+mUser.Username+"[-:-]",
  89. ).Replace(content)
  90. }
  91. return content
  92. }
  93. func parseAuthor(b *strings.Builder, u *discordgo.User, clientID string) {
  94. // If the message author is the client, modify the text color for
  95. // distinction.
  96. if u.ID == clientID {
  97. b.WriteString("[#57F287]")
  98. } else {
  99. b.WriteString("[#ED4245]")
  100. }
  101. b.WriteString(u.Username)
  102. b.WriteString("[-] ")
  103. // If the message author is a bot account, render the message with bot label
  104. // for distinction.
  105. if u.Bot {
  106. b.WriteString("[#EB459E]BOT[-] ")
  107. }
  108. }
  109. type loginResponse struct {
  110. MFA bool `json:"mfa"`
  111. SMS bool `json:"sms"`
  112. Ticket string `json:"ticket"`
  113. Token string `json:"token"`
  114. }
  115. // Login creates a new request to the `/login` endpoint for essential login
  116. // information.
  117. func Login(
  118. s *discordgo.Session,
  119. email, password string,
  120. ) (*loginResponse, error) {
  121. data := struct {
  122. Email string `json:"email"`
  123. Password string `json:"password"`
  124. }{email, password}
  125. resp, err := s.RequestWithBucketID(
  126. "POST",
  127. discordgo.EndpointLogin,
  128. data,
  129. discordgo.EndpointLogin,
  130. )
  131. if err != nil {
  132. return nil, err
  133. }
  134. var lr loginResponse
  135. err = json.Unmarshal(resp, &lr)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return &lr, nil
  140. }
  141. // TOTP creates a new request to `/mfa/totp` endpoint for time-based one-time
  142. // passcode for essential login information
  143. func TOTP(s *discordgo.Session, code, ticket string) (*loginResponse, error) {
  144. data := struct {
  145. Code string `json:"code"`
  146. Ticket string `json:"ticket"`
  147. }{code, ticket}
  148. e := discordgo.EndpointAuth + "mfa/totp"
  149. resp, err := s.RequestWithBucketID("POST", e, data, e)
  150. if err != nil {
  151. return nil, err
  152. }
  153. var lr loginResponse
  154. err = json.Unmarshal(resp, &lr)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return &lr, nil
  159. }
  160. // HasPermission returns a boolean representing whether the provided user has
  161. // given permissions or not.
  162. func HasPermission(
  163. s *discordgo.State,
  164. uID string,
  165. cID string,
  166. perm int64,
  167. ) bool {
  168. p, err := s.UserChannelPermissions(uID, cID)
  169. if err != nil {
  170. return false
  171. }
  172. return p&perm == perm
  173. }