From 3d9e980619187e8eb8cb4d3865af1193f959afd6 Mon Sep 17 00:00:00 2001 From: Bruce Marriner Date: Fri, 13 Nov 2015 09:36:11 -0600 Subject: [PATCH] Several bug fixes causing json unmarshal errors. --- guild.go | 45 ++++++++++++++++++++++++++++----------------- users.go | 21 +++++++++++++++------ wsapi.go | 30 +++++++++++++++++++++++------- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/guild.go b/guild.go index e0ea5d6..f7c56b1 100644 --- a/guild.go +++ b/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 diff --git a/users.go b/users.go index ff4c6fe..d3f0dc4 100644 --- a/users.go +++ b/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"` diff --git a/wsapi.go b/wsapi.go index 8e45b05..4e31d72 100644 --- a/wsapi.go +++ b/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 {