Browse Source

Several bug fixes causing json unmarshal errors.

Bruce Marriner 9 years ago
parent
commit
3d9e980619
3 changed files with 66 additions and 30 deletions
  1. 28 17
      guild.go
  2. 15 6
      users.go
  3. 23 7
      wsapi.go

+ 28 - 17
guild.go

@@ -7,7 +7,7 @@ type Guild struct {
 	Region           string       `json:"region"`
 	Joined_at        string       `json:"joined_at"` // make time stamp
 	Afk_timeout      int          `json:"afk_timeout"`
-	Afk_channel_id   int          `json:"afk_channel_id"`
+	Afk_channel_id   int          `json:"afk_channel_id,string"`
 	Embed_channel_id int          `json:"embed_channel_id"`
 	Embed_enabled    bool         `json:"embed_enabled"`
 	Owner_id         int          `json:"owner_id,string"`
@@ -18,7 +18,16 @@ type Guild struct {
 	Presences        []Presence   `json:"presences"`
 	Channels         []Channel    `json:"channels"`
 	VoiceStates      []VoiceState `json:"voice_states"`
-	Session          *Session     // I got to be doing it wrong here.
+}
+
+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 VoiceState struct {
@@ -40,23 +49,25 @@ type Presence struct {
 
 // TODO: Member vs User?
 type Member struct {
-	GuildId  int    `json:"guild_id"`
-	JoinedAt string `json:"joined_at"`
-	Deaf     bool   `json:"deaf"`
-	mute     bool   `json:"mute"`
-	User     User   `json:"user"`
-	Roles    []Role `json:"roles"`
+	GuildId  int      `json:"guild_id"`
+	JoinedAt string   `json:"joined_at"`
+	Deaf     bool     `json:"deaf"`
+	mute     bool     `json:"mute"`
+	User     User     `json:"user"`
+	Roles    []string `json:"roles"` // TODO: See below
 }
 
-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"`
-}
+//Roles   []string `json:"roles"` // TODO: Should be ints, see below
+// Above "Roles" should be an array of ints
+// TODO: Figure out how to make it be one.
+/*
+	{
+		"roles": [
+			"89544728336416768",
+			"110429733396676608"
+		],
+	}
+*/
 
 // Channels returns an array of Channel structures for channels within
 // this Server

+ 15 - 6
users.go

@@ -1,14 +1,23 @@
 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"`
+	Id       int    `json:"id,string"`
+	Email    string `json:"email"`
+	Username string `json:"username"`
+	Avatar   string `json:"Avatar"`
+	Verified bool   `json:"verified"`
+	//Discriminator int    `json:"discriminator,string"` // TODO: See below
 }
 
+// Discriminator sometimes comes as a string
+// and sometimes it comes as a int.  Weird.
+// to avoid errors I've just commented it out
+// but it doesn't seem to just kill the whole
+// program.  Heartbeat is taken on READY even
+// with error and the system continues to read
+// it just doesn't seem able to handle this one
+// field correctly.  Need to research this more.
+
 type PrivateChannel struct {
 	Id            int  `json:"id,string"`
 	IsPrivate     bool `json:"is_private"`

+ 23 - 7
wsapi.go

@@ -55,13 +55,25 @@ type TypingStart struct {
 }
 
 type PresenceUpdate struct {
-	User    User   `json:"user"`
-	Status  string `json:"status"`
-	Roles   []Role `json:"roles"`
-	GuildId int    `json:"guild_id,string"`
-	GameId  int    `json:"game_id"`
+	User    User     `json:"user"`
+	Status  string   `json:"status"`
+	Roles   []string `json:"roles"` // TODO: Should be ints, see below
+	GuildId int      `json:"guild_id,string"`
+	GameId  int      `json:"game_id"`
 }
 
+//Roles   []string `json:"roles"` // TODO: Should be ints, see below
+// Above "Roles" should be an array of ints
+// TODO: Figure out how to make it be one.
+/*
+	{
+		"roles": [
+			"89544728336416768",
+			"110429733396676608"
+		],
+	}
+*/
+
 type MessageAck struct {
 	MessageId int `json:"message_id,string"`
 	ChannelId int `json:"channel_id,string"`
@@ -167,8 +179,9 @@ func event(s *Session, messageType int, message []byte) (err error) {
 		if s.OnReady != nil {
 			var st Ready
 			if err := json.Unmarshal(e.RawData, &st); err != nil {
+				fmt.Println(err)
 				printJSON(e.RawData) // TODO: Better error logging
-				return err
+				return
 			}
 			s.OnReady(s, st)
 			return
@@ -177,8 +190,9 @@ func event(s *Session, messageType int, message []byte) (err error) {
 		if s.OnVoiceStateUpdate != nil {
 			var st VoiceState
 			if err := json.Unmarshal(e.RawData, &st); err != nil {
+				fmt.Println(err)
 				printJSON(e.RawData) // TODO: Better error logging
-				return err
+				return
 			}
 			s.OnVoiceStateUpdate(s, st)
 			return
@@ -187,6 +201,7 @@ func event(s *Session, messageType int, message []byte) (err error) {
 		if s.OnPresenceUpdate != nil {
 			var st PresenceUpdate
 			if err := json.Unmarshal(e.RawData, &st); err != nil {
+				fmt.Println(err)
 				printJSON(e.RawData) // TODO: Better error logging
 				return err
 			}
@@ -231,6 +246,7 @@ func event(s *Session, messageType int, message []byte) (err error) {
 				return err
 			}
 			s.OnMessageDelete(s, st)
+			return
 		}
 	case "MESSAGE_ACK":
 		if s.OnMessageAck != nil {