wsapi.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Returns the a websocket Gateway address
  43. // session : An active session connection to Discord
  44. // put this here instead of restapi because it is used soley
  45. // for the websocket stuff - but maybe I should move it back
  46. // because it's part of the restapi...
  47. func Gateway(session *Session) (gateway string, err error) {
  48. response, err := Request(session, "GET", fmt.Sprintf("%s/gateway", discordApi), ``)
  49. var temp map[string]interface{}
  50. err = json.Unmarshal(response, &temp)
  51. gateway = temp["url"].(string)
  52. return
  53. }
  54. // Open a websocket connection to Discord
  55. func Open(session *Session) (conn *websocket.Conn, err error) {
  56. // TODO: See if there's a use for the http response.
  57. //conn, response, err := websocket.DefaultDialer.Dial(session.Gateway, nil)
  58. conn, _, err = websocket.DefaultDialer.Dial(session.Gateway, nil)
  59. if err != nil {
  60. return
  61. }
  62. return
  63. }
  64. // maybe this is SendOrigin? not sure the right name here
  65. // also bson.M vs string interface map? Read about
  66. // how to send JSON the right way.
  67. func Handshake(conn *websocket.Conn, token string) (err error) {
  68. err = conn.WriteJSON(map[string]interface{}{
  69. "op": 2,
  70. "d": map[string]interface{}{
  71. "v": 3,
  72. "token": token,
  73. "properties": map[string]string{
  74. "$os": "linux", // get from os package
  75. "$browser": "Discordgo",
  76. "$device": "Discordgo",
  77. "$referer": "",
  78. "$referring_domain": "",
  79. },
  80. },
  81. })
  82. return
  83. }
  84. func UpdateStatus(conn *websocket.Conn, idleSince, gameId string) (err error) {
  85. err = conn.WriteJSON(map[string]interface{}{
  86. "op": 2,
  87. "d": map[string]interface{}{
  88. "idle_since": idleSince,
  89. "game_id": gameId,
  90. },
  91. })
  92. return
  93. }
  94. // TODO: need a channel or something to communicate
  95. // to this so I can tell it to stop listening
  96. func Listen(conn *websocket.Conn) (err error) {
  97. for {
  98. messageType, message, err := conn.ReadMessage()
  99. if err != nil {
  100. fmt.Println(err)
  101. break
  102. }
  103. go event(conn, messageType, message)
  104. }
  105. return
  106. }
  107. // Not sure how needed this is and where it would be best to call it.
  108. // somewhere.
  109. func Close(conn *websocket.Conn) {
  110. conn.Close()
  111. }
  112. // Front line handler for all Websocket Events. Determines the
  113. // event type and passes the message along to the next handler.
  114. func event(conn *websocket.Conn, messageType int, message []byte) {
  115. //printJSON(message) // TODO: wrap in debug if statement
  116. var event Event
  117. err := json.Unmarshal(message, &event)
  118. if err != nil {
  119. fmt.Println(err)
  120. return
  121. }
  122. switch event.Type {
  123. case "READY":
  124. ready(conn, &event)
  125. case "TYPING_START":
  126. // do stuff
  127. case "MESSAGE_CREATE":
  128. // do stuff
  129. case "MESSAGE_ACK":
  130. // do stuff
  131. case "MESSAGE_UPDATE":
  132. // do stuff
  133. case "MESSAGE_DELETE":
  134. // do stuff
  135. case "PRESENCE_UPDATE":
  136. // do stuff
  137. case "CHANNEL_CREATE":
  138. // do stuff
  139. case "CHANNEL_UPDATE":
  140. // do stuff
  141. case "CHANNEL_DELETE":
  142. // do stuff
  143. case "GUILD_CREATE":
  144. // do stuff
  145. case "GUILD_DELETE":
  146. // do stuff
  147. case "GUILD_MEMBER_ADD":
  148. // do stuff
  149. case "GUILD_MEMBER_REMOVE": // which is it.
  150. // do stuff
  151. case "GUILD_MEMBER_DELETE":
  152. // do stuff
  153. case "GUILD_MEMBER_UPDATE":
  154. // do stuff
  155. case "GUILD_ROLE_CREATE":
  156. // do stuff
  157. case "GUILD_ROLE_DELETE":
  158. // do stuff
  159. case "GUILD_INTEGRATIONS_UPDATE":
  160. // do stuff
  161. default:
  162. fmt.Println("UNKNOWN EVENT: ", event.Type)
  163. // learn the log package
  164. // log.print type and JSON data
  165. }
  166. }
  167. // handles the READY Websocket Event from Discord
  168. // this is the motherload of detail provided at
  169. // initial connection to the Websocket.
  170. func ready(conn *websocket.Conn, event *Event) {
  171. var ready Ready
  172. err := json.Unmarshal(event.RawData, &ready)
  173. if err != nil {
  174. fmt.Println(err)
  175. return
  176. }
  177. fmt.Println(ready)
  178. go heartbeat(conn, ready.HeartbeatInterval)
  179. // Start KeepAlive based on .
  180. }
  181. // This heartbeat is sent to keep the Websocket conenction
  182. // to Discord alive. If not sent, Discord will close the
  183. // connection.
  184. func heartbeat(conn *websocket.Conn, interval time.Duration) {
  185. ticker := time.NewTicker(interval * time.Millisecond)
  186. for range ticker.C {
  187. timestamp := int(time.Now().Unix())
  188. conn.WriteJSON(map[string]int{
  189. "op": 1,
  190. "d": timestamp,
  191. })
  192. }
  193. }