From af3fe4842a69fef773ac284fc6d1497a58fff76e Mon Sep 17 00:00:00 2001 From: robbix1206 Date: Wed, 28 Sep 2016 19:04:37 +0200 Subject: [PATCH] Add Support of changing user status (#258) Add Support of changing user status --- restapi.go | 21 +++++++++++++++++++++ restapi_test.go | 11 +++++++++++ structs.go | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/restapi.go b/restapi.go index 5ecb48f..3214556 100644 --- a/restapi.go +++ b/restapi.go @@ -332,6 +332,27 @@ func (s *Session) UserSettings() (st *Settings, err error) { return } +// UserUpdateStatus update the user status +// status : The new status (Actual valid status are 'online','idle','dnd','invisible') +func (s *Session) UserUpdateStatus(status Status) (st *Settings, err error) { + if status == StatusOffline { + err = errors.New("You can't set your Status to offline") + return + } + + data := struct { + Status Status `json:"status"` + }{status} + + body, err := s.Request("PATCH", EndpointUserSettings("@me"), data) + if err != nil { + return + } + + err = unmarshal(body, &st) + return +} + // UserChannels returns an array of Channel structures for all private // channels. func (s *Session) UserChannels() (st []*Channel, err error) { diff --git a/restapi_test.go b/restapi_test.go index bfe60fc..0d68d6e 100644 --- a/restapi_test.go +++ b/restapi_test.go @@ -131,6 +131,17 @@ func TestUserSettings(t *testing.T) { } } +func TestUserUpdateStatus(t *testing.T) { + if dg == nil { + t.Skip("Cannot TestUserSettings, dg not set.") + } + + _, err := dg.UserUpdateStatus(StatusDoNotDisturb) + if err != nil { + t.Errorf(err.Error()) + } +} + // TestLogout tests the Logout() function. This should not return an error. func TestLogout(t *testing.T) { diff --git a/structs.go b/structs.go index dcfb447..88352d9 100644 --- a/structs.go +++ b/structs.go @@ -267,7 +267,7 @@ type VoiceState struct { // A Presence stores the online, offline, or idle and game status of Guild members. type Presence struct { User *User `json:"user"` - Status string `json:"status"` + Status Status `json:"status"` Game *Game `json:"game"` } @@ -304,21 +304,35 @@ type User struct { // A Settings stores data for a specific users Discord client settings. type Settings struct { - RenderEmbeds bool `json:"render_embeds"` - InlineEmbedMedia bool `json:"inline_embed_media"` - InlineAttachmentMedia bool `json:"inline_attachment_media"` - EnableTtsCommand bool `json:"enable_tts_command"` - MessageDisplayCompact bool `json:"message_display_compact"` - ShowCurrentGame bool `json:"show_current_game"` - AllowEmailFriendRequest bool `json:"allow_email_friend_request"` - ConvertEmoticons bool `json:"convert_emoticons"` - Locale string `json:"locale"` - Theme string `json:"theme"` - GuildPositions []string `json:"guild_positions"` - RestrictedGuilds []string `json:"restricted_guilds"` - FriendSourceFlags *FriendSourceFlags `json:"friend_source_flags"` + RenderEmbeds bool `json:"render_embeds"` + InlineEmbedMedia bool `json:"inline_embed_media"` + InlineAttachmentMedia bool `json:"inline_attachment_media"` + EnableTtsCommand bool `json:"enable_tts_command"` + MessageDisplayCompact bool `json:"message_display_compact"` + ShowCurrentGame bool `json:"show_current_game"` + ConvertEmoticons bool `json:"convert_emoticons"` + Locale string `json:"locale"` + Theme string `json:"theme"` + GuildPositions []string `json:"guild_positions"` + RestrictedGuilds []string `json:"restricted_guilds"` + FriendSourceFlags *FriendSourceFlags `json:"friend_source_flags"` + Status Status `json:"status"` + DetectPlatformAccounts bool `json:"detect_platform_accounts"` + DeveloperMode bool `json:"developer_mode"` } +// Status type defination +type Status string + +// Constants for Status with the different current available status +const ( + StatusOnline Status = "online" + StatusIdle Status = "idle" + StatusDoNotDisturb Status = "dnd" + StatusInvisible Status = "invisible" + StatusOffline Status = "offline" +) + // FriendSourceFlags stores ... TODO :) type FriendSourceFlags struct { All bool `json:"all"`