Added Guild Role Update handler
This commit is contained in:
parent
b122bd3013
commit
3794c1a31e
3 changed files with 72 additions and 59 deletions
50
restapi.go
50
restapi.go
|
@ -79,9 +79,9 @@ const (
|
||||||
|
|
||||||
// Request makes a (GET/POST/?) Requests to Discord REST API.
|
// Request makes a (GET/POST/?) Requests to Discord REST API.
|
||||||
// All the other functions in this file use this function.
|
// All the other functions in this file use this function.
|
||||||
func Request(session *Session, method, urlStr, body string) (response []byte, err error) {
|
func (s *Session) Request(method, urlStr, body string) (response []byte, err error) {
|
||||||
|
|
||||||
if session.Debug {
|
if s.Debug {
|
||||||
fmt.Println("REQUEST :: " + method + " " + urlStr + "\n" + body)
|
fmt.Println("REQUEST :: " + method + " " + urlStr + "\n" + body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,8 +91,8 @@ func Request(session *Session, method, urlStr, body string) (response []byte, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not used on initial login..
|
// Not used on initial login..
|
||||||
if session.Token != "" {
|
if s.Token != "" {
|
||||||
req.Header.Set("authorization", session.Token)
|
req.Header.Set("authorization", s.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
@ -114,7 +114,7 @@ func Request(session *Session, method, urlStr, body string) (response []byte, er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if session.Debug {
|
if s.Debug {
|
||||||
var prettyJSON bytes.Buffer
|
var prettyJSON bytes.Buffer
|
||||||
error := json.Indent(&prettyJSON, response, "", "\t")
|
error := json.Indent(&prettyJSON, response, "", "\t")
|
||||||
if error != nil {
|
if error != nil {
|
||||||
|
@ -127,9 +127,9 @@ func Request(session *Session, method, urlStr, body string) (response []byte, er
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login asks the Discord server for an authentication token
|
// Login asks the Discord server for an authentication token
|
||||||
func Login(session *Session, email string, password string) (token string, err error) {
|
func (s *Session) Login(email string, password string) (token string, err error) {
|
||||||
|
|
||||||
response, err := Request(session, "POST", LOGIN, fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))
|
response, err := s.Request("POST", LOGIN, fmt.Sprintf(`{"email":"%s", "password":"%s"}`, email, password))
|
||||||
|
|
||||||
var temp map[string]interface{}
|
var temp map[string]interface{}
|
||||||
err = json.Unmarshal(response, &temp)
|
err = json.Unmarshal(response, &temp)
|
||||||
|
@ -141,9 +141,9 @@ func Login(session *Session, email string, password string) (token string, err e
|
||||||
// Returns the user details of the given userId
|
// Returns the user details of the given userId
|
||||||
// session : An active session connection to Discord
|
// session : An active session connection to Discord
|
||||||
// user : A user Id or name
|
// user : A user Id or name
|
||||||
func Users(session *Session, userId string) (user User, err error) {
|
func (s *Session) Users(userId string) (user User, err error) {
|
||||||
|
|
||||||
body, err := Request(session, "GET", fmt.Sprintf("%s/%s", USERS, userId), ``)
|
body, err := s.Request("GET", fmt.Sprintf("%s/%s", USERS, userId), ``)
|
||||||
err = json.Unmarshal(body, &user)
|
err = json.Unmarshal(body, &user)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -154,18 +154,18 @@ func Users(session *Session, userId string) (user User, err error) {
|
||||||
|
|
||||||
// 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 (s *Session) PrivateChannels(userId string) (channels []Channel, err error) {
|
||||||
|
|
||||||
body, err := Request(session, "GET", fmt.Sprintf("%s/%s/channels", USERS, userId), ``)
|
body, err := s.Request("GET", fmt.Sprintf("%s/%s/channels", USERS, userId), ``)
|
||||||
err = json.Unmarshal(body, &channels)
|
err = json.Unmarshal(body, &channels)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guilds returns an array of Guild structures for all servers for a user
|
// Guilds returns an array of Guild structures for all servers for a user
|
||||||
func Guilds(session *Session, userId string) (servers []Guild, err error) {
|
func (s *Session) Guilds(userId string) (servers []Guild, err error) {
|
||||||
|
|
||||||
body, err := Request(session, "GET", fmt.Sprintf("%s/%s/guilds", USERS, userId), ``)
|
body, err := s.Request("GET", fmt.Sprintf("%s/%s/guilds", USERS, userId), ``)
|
||||||
err = json.Unmarshal(body, &servers)
|
err = json.Unmarshal(body, &servers)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -176,9 +176,9 @@ func Guilds(session *Session, userId string) (servers []Guild, err error) {
|
||||||
|
|
||||||
// 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 (s *Session) Members(serverId int) (members []Member, err error) {
|
||||||
|
|
||||||
body, err := Request(session, "GET", fmt.Sprintf("%s/%d/members", SERVERS, serverId), ``)
|
body, err := s.Request("GET", fmt.Sprintf("%s/%d/members", SERVERS, serverId), ``)
|
||||||
err = json.Unmarshal(body, &members)
|
err = json.Unmarshal(body, &members)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -186,9 +186,9 @@ func Members(session *Session, serverId int) (members []Member, err error) {
|
||||||
|
|
||||||
// Channels returns an array of Channel structures for all channels of a given
|
// Channels returns an array of Channel structures for all channels of a given
|
||||||
// server.
|
// server.
|
||||||
func Channels(session *Session, serverId int) (channels []Channel, err error) {
|
func (s *Session) Channels(serverId int) (channels []Channel, err error) {
|
||||||
|
|
||||||
body, err := Request(session, "GET", fmt.Sprintf("%s/%d/channels", SERVERS, serverId), ``)
|
body, err := s.Request("GET", fmt.Sprintf("%s/%d/channels", SERVERS, serverId), ``)
|
||||||
err = json.Unmarshal(body, &channels)
|
err = json.Unmarshal(body, &channels)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -201,7 +201,7 @@ func Channels(session *Session, serverId int) (channels []Channel, err error) {
|
||||||
// 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.
|
||||||
func Messages(session *Session, channelId int, limit int, beforeId int, afterId int) (messages []Message, err error) {
|
func (s *Session) Messages(channelId int, limit int, beforeId int, afterId int) (messages []Message, err error) {
|
||||||
|
|
||||||
var urlStr string
|
var urlStr string
|
||||||
|
|
||||||
|
@ -229,17 +229,17 @@ func Messages(session *Session, channelId int, limit int, beforeId int, afterId
|
||||||
urlStr = fmt.Sprintf("%s/%d/messages", CHANNELS, channelId)
|
urlStr = fmt.Sprintf("%s/%d/messages", CHANNELS, channelId)
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := Request(session, "GET", urlStr, ``)
|
body, err := s.Request("GET", urlStr, ``)
|
||||||
err = json.Unmarshal(body, &messages)
|
err = json.Unmarshal(body, &messages)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendMessage sends a message to the given channel.
|
// SendMessage sends a message to the given channel.
|
||||||
func SendMessage(session *Session, channelId int, content string) (message Message, err error) {
|
func (s *Session) SendMessage(channelId int, content string) (message Message, err error) {
|
||||||
|
|
||||||
var urlStr string = fmt.Sprintf("%s/%d/messages", CHANNELS, channelId)
|
var urlStr string = fmt.Sprintf("%s/%d/messages", CHANNELS, channelId)
|
||||||
response, err := Request(session, "POST", urlStr, fmt.Sprintf(`{"content":"%s"}`, content))
|
response, err := s.Request("POST", urlStr, fmt.Sprintf(`{"content":"%s"}`, content))
|
||||||
err = json.Unmarshal(response, &message)
|
err = json.Unmarshal(response, &message)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -247,9 +247,9 @@ func SendMessage(session *Session, channelId int, content string) (message Messa
|
||||||
|
|
||||||
// Returns the a websocket Gateway address
|
// Returns the a websocket Gateway address
|
||||||
// session : An active session connection to Discord
|
// session : An active session connection to Discord
|
||||||
func Gateway(session *Session) (gateway string, err error) {
|
func (s *Session) Gateway() (gateway string, err error) {
|
||||||
|
|
||||||
response, err := Request(session, "GET", GATEWAY, ``)
|
response, err := s.Request("GET", GATEWAY, ``)
|
||||||
|
|
||||||
var temp map[string]interface{}
|
var temp map[string]interface{}
|
||||||
err = json.Unmarshal(response, &temp)
|
err = json.Unmarshal(response, &temp)
|
||||||
|
@ -261,9 +261,9 @@ func Gateway(session *Session) (gateway string, err error) {
|
||||||
// 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
|
||||||
// even use.
|
// even use.
|
||||||
func Logout(session *Session) (err error) {
|
func (s *Session) Logout() (err error) {
|
||||||
|
|
||||||
_, err = Request(session, "POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, session.Token))
|
_, err = s.Request("POST", LOGOUT, fmt.Sprintf(`{"token": "%s"}`, s.Token))
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
39
session.go
39
session.go
|
@ -1,5 +1,13 @@
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* A Discord API for Golang.
|
* A Discord API for Golang.
|
||||||
|
*
|
||||||
|
* This file has structs and functions specific to a session.
|
||||||
|
*
|
||||||
|
* A session is a single connection to Discord for a given
|
||||||
|
* user and all REST and Websock API functions exist within
|
||||||
|
* a session.
|
||||||
|
*
|
||||||
|
* See the restapi.go and wsapi.go for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package discordgo
|
package discordgo
|
||||||
|
@ -10,10 +18,9 @@ import "github.com/gorilla/websocket"
|
||||||
// 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 // Authentication token for this session
|
Token string // Authentication token for this session
|
||||||
Gateway string // Websocket Gateway for this session
|
Debug bool // Debug for printing JSON request/responses
|
||||||
Debug bool // Debug for printing JSON request/responses
|
Cache int // number in X to cache some responses
|
||||||
Cache int // number in X to cache some responses
|
|
||||||
|
|
||||||
// Settable Callback functions for Websocket Events
|
// Settable Callback functions for Websocket Events
|
||||||
OnEvent func(*Session, Event) // should Event be *Event?
|
OnEvent func(*Session, Event) // should Event be *Event?
|
||||||
|
@ -36,6 +43,7 @@ type Session struct {
|
||||||
OnGuildMemberDelete func(*Session, Member) // which is it?
|
OnGuildMemberDelete func(*Session, Member) // which is it?
|
||||||
OnGuildMemberUpdate func(*Session, Member)
|
OnGuildMemberUpdate func(*Session, Member)
|
||||||
OnGuildRoleCreate func(*Session, Role)
|
OnGuildRoleCreate func(*Session, Role)
|
||||||
|
OnGuildRoleUpdate func(*Session, GuildRoleUpdate)
|
||||||
OnGuildRoleDelete func(*Session, Role)
|
OnGuildRoleDelete func(*Session, Role)
|
||||||
OnGuildIntegrationsUpdate func(*Session, GuildIntegrationsUpdate)
|
OnGuildIntegrationsUpdate func(*Session, GuildIntegrationsUpdate)
|
||||||
|
|
||||||
|
@ -56,28 +64,17 @@ type Session struct {
|
||||||
* The below functions are "shortcut" methods for functions in restapi.go
|
* The below functions are "shortcut" methods for functions in restapi.go
|
||||||
* Reference the client.go file for more documentation.
|
* Reference the client.go file for more documentation.
|
||||||
*/
|
*/
|
||||||
func (session *Session) Login(email string, password string) (token string, err error) {
|
func (s *Session) Self() (user User, err error) {
|
||||||
token, err = Login(session, email, password)
|
user, err = s.Users("@me")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) Self() (user User, err error) {
|
func (s *Session) MyPrivateChannels() (channels []Channel, err error) {
|
||||||
user, err = Users(session, "@me")
|
channels, err = s.PrivateChannels("@me")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (session *Session) PrivateChannels() (channels []Channel, err error) {
|
func (s *Session) MyGuilds() (servers []Guild, err error) {
|
||||||
channels, err = PrivateChannels(session, "@me")
|
servers, err = s.Guilds("@me")
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (session *Session) Guilds() (servers []Guild, err error) {
|
|
||||||
servers, err = Guilds(session, "@me")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Logout ends a session and logs out from the Discord REST API.
|
|
||||||
func (session *Session) Logout() (err error) {
|
|
||||||
err = Logout(session)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
42
wsapi.go
42
wsapi.go
|
@ -26,7 +26,7 @@ type Event struct {
|
||||||
//Direction of command, 0-received, 1-sent -- thanks Xackery/discord
|
//Direction of command, 0-received, 1-sent -- thanks Xackery/discord
|
||||||
|
|
||||||
RawData json.RawMessage `json:"d"`
|
RawData json.RawMessage `json:"d"`
|
||||||
Session Session
|
Session *Session
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Ready Event given after initial connection
|
// The Ready Event given after initial connection
|
||||||
|
@ -88,19 +88,27 @@ type GuildIntegrationsUpdate struct {
|
||||||
GuildId int `json:"guild_id,string"`
|
GuildId int `json:"guild_id,string"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GuildRoleUpdate struct {
|
||||||
|
Role Role `json:"role"`
|
||||||
|
GuildId int `json:"guild_id,int"`
|
||||||
|
}
|
||||||
|
|
||||||
// Open a websocket connection to Discord
|
// Open a websocket connection to Discord
|
||||||
func Open(s *Session) (err error) {
|
func (s *Session) Open() (err error) {
|
||||||
|
|
||||||
|
// Get the gateway to use for the Websocket connection
|
||||||
|
g, err := s.Gateway()
|
||||||
|
|
||||||
// TODO: See if there's a use for the http response.
|
// TODO: See if there's a use for the http response.
|
||||||
// conn, response, err := websocket.DefaultDialer.Dial(session.Gateway, nil)
|
// conn, response, err := websocket.DefaultDialer.Dial(session.Gateway, nil)
|
||||||
s.wsConn, _, err = websocket.DefaultDialer.Dial(s.Gateway, nil)
|
s.wsConn, _, err = websocket.DefaultDialer.Dial(g, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// maybe this is SendOrigin? not sure the right name here
|
// maybe this is SendOrigin? not sure the right name here
|
||||||
// also bson.M vs string interface map? Read about
|
// also bson.M vs string interface map? Read about
|
||||||
// how to send JSON the right way.
|
// how to send JSON the right way.
|
||||||
func Handshake(s *Session) (err error) {
|
func (s *Session) Handshake() (err error) {
|
||||||
|
|
||||||
err = s.wsConn.WriteJSON(map[string]interface{}{
|
err = s.wsConn.WriteJSON(map[string]interface{}{
|
||||||
"op": 2,
|
"op": 2,
|
||||||
|
@ -120,7 +128,7 @@ func Handshake(s *Session) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateStatus(s *Session, idleSince, gameId string) (err error) {
|
func (s *Session) UpdateStatus(idleSince, gameId string) (err error) {
|
||||||
|
|
||||||
err = s.wsConn.WriteJSON(map[string]interface{}{
|
err = s.wsConn.WriteJSON(map[string]interface{}{
|
||||||
"op": 2,
|
"op": 2,
|
||||||
|
@ -135,7 +143,7 @@ func UpdateStatus(s *Session, idleSince, gameId string) (err error) {
|
||||||
|
|
||||||
// TODO: need a channel or something to communicate
|
// TODO: need a channel or something to communicate
|
||||||
// to this so I can tell it to stop listening
|
// to this so I can tell it to stop listening
|
||||||
func Listen(s *Session) (err error) {
|
func (s *Session) Listen() (err error) {
|
||||||
|
|
||||||
if s.wsConn == nil {
|
if s.wsConn == nil {
|
||||||
fmt.Println("No websocket connection exists.")
|
fmt.Println("No websocket connection exists.")
|
||||||
|
@ -148,7 +156,7 @@ func Listen(s *Session) (err error) {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
go event(s, messageType, message)
|
go s.event(messageType, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -156,13 +164,13 @@ func Listen(s *Session) (err error) {
|
||||||
|
|
||||||
// Not sure how needed this is and where it would be best to call it.
|
// Not sure how needed this is and where it would be best to call it.
|
||||||
// somewhere.
|
// somewhere.
|
||||||
func Close(s *Session) {
|
func (s *Session) Close() {
|
||||||
s.wsConn.Close()
|
s.wsConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Front line handler for all Websocket Events. Determines the
|
// Front line handler for all Websocket Events. Determines the
|
||||||
// event type and passes the message along to the next handler.
|
// event type and passes the message along to the next handler.
|
||||||
func event(s *Session, messageType int, message []byte) (err error) {
|
func (s *Session) event(messageType int, message []byte) (err error) {
|
||||||
|
|
||||||
if s.Debug {
|
if s.Debug {
|
||||||
printJSON(message)
|
printJSON(message)
|
||||||
|
@ -362,10 +370,18 @@ func event(s *Session, messageType int, message []byte) (err error) {
|
||||||
s.OnGuildMemberUpdate(s, st)
|
s.OnGuildMemberUpdate(s, st)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case "GUILD_ROLE_CREATE":
|
||||||
|
if s.OnGuildRoleUpdate != nil {
|
||||||
|
var st GuildRoleUpdate
|
||||||
|
if err := json.Unmarshal(e.RawData, &st); err != nil {
|
||||||
|
fmt.Println(e.Type, err)
|
||||||
|
printJSON(e.RawData) // TODO: Better error logginEventg
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.OnGuildRoleUpdate(s, st)
|
||||||
|
return
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
case "GUILD_ROLE_CREATE":
|
|
||||||
if s.OnGuildRoleCreate != nil {
|
|
||||||
}
|
|
||||||
case "GUILD_ROLE_DELETE":
|
case "GUILD_ROLE_DELETE":
|
||||||
if s.OnGuildRoleDelete != nil {
|
if s.OnGuildRoleDelete != nil {
|
||||||
}
|
}
|
||||||
|
@ -398,7 +414,7 @@ func event(s *Session, messageType int, message []byte) (err error) {
|
||||||
// This heartbeat is sent to keep the Websocket conenction
|
// This heartbeat is sent to keep the Websocket conenction
|
||||||
// to Discord alive. If not sent, Discord will close the
|
// to Discord alive. If not sent, Discord will close the
|
||||||
// connection.
|
// connection.
|
||||||
func Heartbeat(s *Session, i time.Duration) {
|
func (s *Session) Heartbeat(i time.Duration) {
|
||||||
|
|
||||||
if s.wsConn == nil {
|
if s.wsConn == nil {
|
||||||
fmt.Println("No websocket connection exists.")
|
fmt.Println("No websocket connection exists.")
|
||||||
|
|
Loading…
Reference in a new issue