Browse Source

Inital add of Websockets

Bruce Marriner 9 years ago
parent
commit
e6789fde74
7 changed files with 104 additions and 58 deletions
  1. 3 0
      channel.go
  2. 2 0
      discord.go
  3. 24 2
      restapi.go
  4. 69 12
      server.go
  5. 6 2
      session.go
  6. 0 27
      structs.go
  7. 0 15
      wsapi.go

+ 3 - 0
channel.go

@@ -16,4 +16,7 @@ type Channel struct {
 /*
 func (c *Channel) Messages() (messages []Message) {
 }
+
+func (c *Channel) SendMessage() (content string) {
+}
 */

+ 2 - 0
discord.go

@@ -5,6 +5,8 @@
  * Currently only the REST API is functional.  I will add on the websocket
  * layer once I get the API section where I want it.
  *
+ * The idea is that this file is where we pull together the wsapi, and
+ * restapi to create a single do-it-all struct
  */
 
 package discordgo

+ 24 - 2
restapi.go

@@ -3,7 +3,7 @@
  * A Discord API for Golang.
  * See discord.go for more information.
  *
- * This file contains functions for interacting with the Discord HTTPHTTP  REST API
+ * This file contains functions for interacting with the Discord HTTP REST API
  * at the lowest level.
  */
 
@@ -27,7 +27,6 @@ func Request(session *Session, method, urlStr, body string) (response []byte, er
 		fmt.Println("REQUEST  :: " + method + " " + urlStr + "\n" + body)
 	}
 
-	// TODO: not sure if the NewBuffer is really needed always?
 	req, err := http.NewRequest(method, urlStr, bytes.NewBuffer([]byte(body)))
 	if err != nil {
 		return
@@ -93,6 +92,10 @@ func Users(session *Session, userId string) (user User, err error) {
 	return
 }
 
+// USERS could pull users channels, servers, settings and so forth too?
+// you know, pull all the data for the user.  update the user strut
+// to house that data.  Seems reasonable.
+
 // PrivateChannels returns an array of Channel structures for all private
 // channels for a user
 func PrivateChannels(session *Session, userId string) (channels []Channel, err error) {
@@ -112,6 +115,9 @@ func Servers(session *Session, userId string) (servers []Server, err error) {
 	return
 }
 
+// add one to get specific server by ID, or enhance the above with an ID field.
+// GET http://discordapp.com/api/guilds/ID#
+
 // Members returns an array of Member structures for all members of a given
 // server.
 func Members(session *Session, serverId int) (members []Member, err error) {
@@ -132,6 +138,10 @@ func Channels(session *Session, serverId int) (channels []Channel, err error) {
 	return
 }
 
+// update above or add a way to get channel by ID.  ChannelByName could be handy
+// too you know.
+// http://discordapp.com/api/channels/ID#
+
 // Messages returns an array of Message structures for messaages within a given
 // channel.  limit, beforeId, and afterId can be used to control what messages
 // are returned.
@@ -179,6 +189,18 @@ func SendMessage(session *Session, channelId int, content string) (message Messa
 	return
 }
 
+// Returns the a websocket Gateway address
+// session : An active session connection to Discord
+func Gateway(session *Session) (gateway string, err error) {
+
+	response, err := Request(session, "GET", fmt.Sprintf("%s/gateway", discordApi), ``)
+
+	var temp map[string]interface{}
+	err = json.Unmarshal(response, &temp)
+	gateway = temp["url"].(string)
+	return
+}
+
 // Close ends a session and logs out from the Discord REST API.
 // This does not seem to actually invalidate the token.  So you can still
 // make API calls even after a Logout.  So, it seems almost pointless to

+ 69 - 12
server.go

@@ -1,23 +1,80 @@
 package discordgo
 
 type Server struct {
-	Id               int      `json:"id,string"`
-	Name             string   `json:"name"`
-	Icon             string   `json:"icon"`
-	Region           string   `json:"region"`
-	Joined_at        string   `json:"joined_at"`
-	Afk_timeout      int      `json:"afk_timeout"`
-	Afk_channel_id   int      `json:"afk_channel_id"`
-	Embed_channel_id int      `json:"embed_channel_id"`
-	Embed_enabled    bool     `json:"embed_enabled"`
-	Owner_id         int      `json:"owner_id,string"`
-	Roles            []Role   `json:"roles"`
-	Session          *Session // I got to be doing it wrong here.
+	Id               int        `json:"id,string"`
+	Name             string     `json:"name"`
+	Icon             string     `json:"icon"`
+	Region           string     `json:"region"`
+	Joined_at        string     `json:"joined_at"`
+	Afk_timeout      int        `json:"afk_timeout"`
+	Afk_channel_id   int        `json:"afk_channel_id"`
+	Embed_channel_id int        `json:"embed_channel_id"`
+	Embed_enabled    bool       `json:"embed_enabled"`
+	Owner_id         int        `json:"owner_id,string"`
+	Large            bool       `json:"large"`     // ??
+	JoinedAt         string     `json:"joined_at"` // make this a timestamp
+	Roles            []Role     `json:"roles"`
+	Members          []Member   `json:"members"`
+	Presences        []Presence `json:"presences"`
+	Channels         []Channel  `json:"channels"`
+	//	VoiceStates      []VoiceState `json:"voice_states"`
+	Session *Session // I got to be doing it wrong here.
+}
+
+type VoiceStates struct {
+	UserId    int    `json:"user_id,string"`
+	Suppress  bool   `json:"suppress"`
+	SessionId string `json:"session_id"`
+	SelfMute  bool   `json:"self_mute"`
+	SelfDeaf  bool   `json:"self_deaf"`
+	Mute      bool   `json:"mute"`
+	Deaf      bool   `json:"deaf"`
+	ChannelId int    `json:"channel_id,string"`
+}
+
+type Presence struct {
+	User   User   `json:"user"`
+	Status string `json:"status"`
+	GameId int    `json:"game_id"`
+}
+
+// TODO: Member vs User?
+type Member struct {
+	JoinedAt string `json:"joined_at"`
+	Deaf     bool   `json:"deaf"`
+	mute     bool   `json:"mute"`
+	User     User   `json:"user"`
+	Roles    []Role `json:"roles"`
+}
+
+type Role struct {
+	Id          int    `json:"id,string"`
+	Name        string `json:"name"`
+	Managed     bool   `json:"managed"`
+	Color       int    `json:"color"`
+	Hoist       bool   `json:"hoist"`
+	Position    int    `json:"position"`
+	Permissions int    `json:"permissions"`
 }
 
 // Channels returns an array of Channel structures for channels within
 // this Server
+/*
+TODO: How to name these? If we make a variable to store
+channels from READY packet, etc.  We can't have a Channel
+func?  And which is better.  Channels func gets live up
+to date data on each call.. so, there is some benefit there.
+
+Maybe it should have both, but make the Channels check and
+pull new data based on a cache time?
+
 func (s *Server) Channels() (c []Channel, err error) {
 	c, err = Channels(s.Session, s.Id)
 	return
 }
+*/
+/*
+
+func (s *Server) Members() (m []Users, err error) {
+}
+*/

+ 6 - 2
session.go

@@ -5,12 +5,16 @@
 
 package discordgo
 
+import "github.com/gorilla/websocket"
+
 // A Session represents a connection to the Discord REST API.
 // Token : The authentication token returned from Discord
 // Debug : If set to ture debug logging will be displayed.
 type Session struct {
-	Token string
-	Debug bool
+	Token     string
+	Gateway   string
+	Debug     bool
+	Websocket *websocket.Conn
 }
 
 /******************************************************************************

+ 0 - 27
structs.go

@@ -1,32 +1,5 @@
 package discordgo
 
-type User struct {
-	Id            int    `json:"id,string"`
-	Email         string `json:"email"`
-	Username      string `json:"username"`
-	Avatar        string `json:"Avatar"`
-	Verified      bool   `json:"verified"`
-	Discriminator string `json:"discriminator"`
-}
-
-type Member struct {
-	JoinedAt string `json:"joined_at"`
-	Deaf     bool   `json:"deaf"`
-	mute     bool   `json:"mute"`
-	User     User   `json:"user"`
-	Roles    []Role `json:"roles"`
-}
-
-type Role struct {
-	Id          int    `json:"id,string"`
-	Name        string `json:"name"`
-	Managed     bool   `json:"managed"`
-	Color       int    `json:"color"`
-	Hoist       bool   `json:"hoist"`
-	Position    int    `json:"position"`
-	Permissions int    `json:"permissions"`
-}
-
 type Message struct {
 	Id              int          `json:"id,string"`
 	Author          User         `json:"author"`

+ 0 - 15
wsapi.go

@@ -47,21 +47,6 @@ type ReadState struct {
 	ID            int `json:"id,string"`
 }
 
-// Returns the a websocket Gateway address
-// session : An active session connection to Discord
-// put this here instead of restapi because it is used soley
-// for the websocket stuff - but maybe I should move it back
-// because it's part of the restapi...
-func Gateway(session *Session) (gateway string, err error) {
-
-	response, err := Request(session, "GET", fmt.Sprintf("%s/gateway", discordApi), ``)
-
-	var temp map[string]interface{}
-	err = json.Unmarshal(response, &temp)
-	gateway = temp["url"].(string)
-	return
-}
-
 // Open a websocket connection to Discord
 func Open(session *Session) (conn *websocket.Conn, err error) {