Inital add of Websockets

This commit is contained in:
Bruce Marriner 2015-11-08 21:42:16 -06:00
parent adac11495a
commit e6789fde74
7 changed files with 104 additions and 58 deletions

View file

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

View file

@ -5,6 +5,8 @@
* Currently only the REST API is functional. I will add on the websocket * Currently only the REST API is functional. I will add on the websocket
* layer once I get the API section where I want it. * 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 package discordgo

View file

@ -3,7 +3,7 @@
* A Discord API for Golang. * A Discord API for Golang.
* See discord.go for more information. * 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. * 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) 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))) req, err := http.NewRequest(method, urlStr, bytes.NewBuffer([]byte(body)))
if err != nil { if err != nil {
return return
@ -93,6 +92,10 @@ func Users(session *Session, userId string) (user User, err error) {
return 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 // PrivateChannels returns an array of Channel structures for all private
// channels for a user // channels for a user
func PrivateChannels(session *Session, userId string) (channels []Channel, err error) { 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 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 // Members returns an array of Member structures for all members of a given
// server. // server.
func Members(session *Session, serverId int) (members []Member, err error) { 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 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 // Messages returns an array of Message structures for messaages within a given
// channel. limit, beforeId, and afterId can be used to control what messages // channel. limit, beforeId, and afterId can be used to control what messages
// are returned. // are returned.
@ -179,6 +189,18 @@ func SendMessage(session *Session, channelId int, content string) (message Messa
return 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. // 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 // 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 // make API calls even after a Logout. So, it seems almost pointless to

View file

@ -11,13 +11,70 @@ type Server struct {
Embed_channel_id int `json:"embed_channel_id"` Embed_channel_id int `json:"embed_channel_id"`
Embed_enabled bool `json:"embed_enabled"` Embed_enabled bool `json:"embed_enabled"`
Owner_id int `json:"owner_id,string"` Owner_id int `json:"owner_id,string"`
Large bool `json:"large"` // ??
JoinedAt string `json:"joined_at"` // make this a timestamp
Roles []Role `json:"roles"` 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. 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 // Channels returns an array of Channel structures for channels within
// this Server // 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) { func (s *Server) Channels() (c []Channel, err error) {
c, err = Channels(s.Session, s.Id) c, err = Channels(s.Session, s.Id)
return return
} }
*/
/*
func (s *Server) Members() (m []Users, err error) {
}
*/

View file

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

View file

@ -1,32 +1,5 @@
package discordgo 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 { type Message struct {
Id int `json:"id,string"` Id int `json:"id,string"`
Author User `json:"author"` Author User `json:"author"`

View file

@ -47,21 +47,6 @@ type ReadState struct {
ID int `json:"id,string"` 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 // Open a websocket connection to Discord
func Open(session *Session) (conn *websocket.Conn, err error) { func Open(session *Session) (conn *websocket.Conn, err error) {