wsapi.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /******************************************************************************
  2. * Discordgo by Bruce Marriner <bruce@sqls.net>
  3. * A Discord API for Golang.
  4. * See discord.go for more information.
  5. *
  6. * This file contains functions low level functions for interacting
  7. * with the Discord Websocket interface.
  8. */
  9. package discordgo
  10. import (
  11. "encoding/json"
  12. "fmt"
  13. "time"
  14. "github.com/gorilla/websocket"
  15. )
  16. // Basic struct for all Websocket Event messages
  17. type Event struct {
  18. Type string `json:"t"`
  19. State int `json:"s"`
  20. Operation int `json:"o"`
  21. Direction int `json:"dir"`
  22. //Direction of command, 0-received, 1-sent -- thanks Xackery/discord
  23. RawData json.RawMessage `json:"d"`
  24. }
  25. // The Ready Event given after initial connection
  26. type Ready struct {
  27. Version int `json:"v"`
  28. SessionID string `json:"session_id"`
  29. HeartbeatInterval time.Duration `json:"heartbeat_interval"`
  30. User User `json:"user"`
  31. ReadState []ReadState
  32. PrivateChannels []PrivateChannel
  33. Servers []Server
  34. }
  35. // ReadState might need to move? Gives me the read status
  36. // of all my channels when first connecting. I think :)
  37. type ReadState struct {
  38. MentionCount int
  39. LastMessageID int `json:"last_message_id,string"`
  40. ID int `json:"id,string"`
  41. }
  42. // Open a websocket connection to Discord
  43. func Open(session *Session) (conn *websocket.Conn, err error) {
  44. // TODO: See if there's a use for the http response.
  45. //conn, response, err := websocket.DefaultDialer.Dial(session.Gateway, nil)
  46. conn, _, err = websocket.DefaultDialer.Dial(session.Gateway, nil)
  47. if err != nil {
  48. return
  49. }
  50. return
  51. }
  52. // maybe this is SendOrigin? not sure the right name here
  53. // also bson.M vs string interface map? Read about
  54. // how to send JSON the right way.
  55. func Handshake(conn *websocket.Conn, token string) (err error) {
  56. err = conn.WriteJSON(map[string]interface{}{
  57. "op": 2,
  58. "d": map[string]interface{}{
  59. "v": 3,
  60. "token": token,
  61. "properties": map[string]string{
  62. "$os": "linux", // get from os package
  63. "$browser": "Discordgo",
  64. "$device": "Discordgo",
  65. "$referer": "",
  66. "$referring_domain": "",
  67. },
  68. },
  69. })
  70. return
  71. }
  72. func UpdateStatus(conn *websocket.Conn, idleSince, gameId string) (err error) {
  73. err = conn.WriteJSON(map[string]interface{}{
  74. "op": 2,
  75. "d": map[string]interface{}{
  76. "idle_since": idleSince,
  77. "game_id": gameId,
  78. },
  79. })
  80. return
  81. }
  82. // TODO: need a channel or something to communicate
  83. // to this so I can tell it to stop listening
  84. func Listen(conn *websocket.Conn) (err error) {
  85. for {
  86. messageType, message, err := conn.ReadMessage()
  87. if err != nil {
  88. fmt.Println(err)
  89. break
  90. }
  91. go event(conn, messageType, message)
  92. }
  93. return
  94. }
  95. // Not sure how needed this is and where it would be best to call it.
  96. // somewhere.
  97. func Close(conn *websocket.Conn) {
  98. conn.Close()
  99. }
  100. // Front line handler for all Websocket Events. Determines the
  101. // event type and passes the message along to the next handler.
  102. func event(conn *websocket.Conn, messageType int, message []byte) {
  103. //printJSON(message) // TODO: wrap in debug if statement
  104. var event Event
  105. err := json.Unmarshal(message, &event)
  106. if err != nil {
  107. fmt.Println(err)
  108. return
  109. }
  110. switch event.Type {
  111. case "READY":
  112. ready(conn, &event)
  113. case "TYPING_START":
  114. // do stuff
  115. case "MESSAGE_CREATE":
  116. // do stuff
  117. case "MESSAGE_ACK":
  118. // do stuff
  119. case "MESSAGE_UPDATE":
  120. // do stuff
  121. case "MESSAGE_DELETE":
  122. // do stuff
  123. case "PRESENCE_UPDATE":
  124. // do stuff
  125. case "CHANNEL_CREATE":
  126. // do stuff
  127. case "CHANNEL_UPDATE":
  128. // do stuff
  129. case "CHANNEL_DELETE":
  130. // do stuff
  131. case "GUILD_CREATE":
  132. // do stuff
  133. case "GUILD_DELETE":
  134. // do stuff
  135. case "GUILD_MEMBER_ADD":
  136. // do stuff
  137. case "GUILD_MEMBER_REMOVE": // which is it.
  138. // do stuff
  139. case "GUILD_MEMBER_DELETE":
  140. // do stuff
  141. case "GUILD_MEMBER_UPDATE":
  142. // do stuff
  143. case "GUILD_ROLE_CREATE":
  144. // do stuff
  145. case "GUILD_ROLE_DELETE":
  146. // do stuff
  147. case "GUILD_INTEGRATIONS_UPDATE":
  148. // do stuff
  149. default:
  150. fmt.Println("UNKNOWN EVENT: ", event.Type)
  151. // learn the log package
  152. // log.print type and JSON data
  153. }
  154. }
  155. // handles the READY Websocket Event from Discord
  156. // this is the motherload of detail provided at
  157. // initial connection to the Websocket.
  158. func ready(conn *websocket.Conn, event *Event) {
  159. var ready Ready
  160. err := json.Unmarshal(event.RawData, &ready)
  161. if err != nil {
  162. fmt.Println(err)
  163. return
  164. }
  165. fmt.Println(ready)
  166. go heartbeat(conn, ready.HeartbeatInterval)
  167. // Start KeepAlive based on .
  168. }
  169. // This heartbeat is sent to keep the Websocket conenction
  170. // to Discord alive. If not sent, Discord will close the
  171. // connection.
  172. func heartbeat(conn *websocket.Conn, interval time.Duration) {
  173. ticker := time.NewTicker(interval * time.Millisecond)
  174. for range ticker.C {
  175. timestamp := int(time.Now().Unix())
  176. conn.WriteJSON(map[string]int{
  177. "op": 1,
  178. "d": timestamp,
  179. })
  180. }
  181. }