Browse Source

Add Support of changing user status (#258)

Add Support of changing user status
robbix1206 8 years ago
parent
commit
af3fe4842a
3 changed files with 61 additions and 15 deletions
  1. 21 0
      restapi.go
  2. 11 0
      restapi_test.go
  3. 29 15
      structs.go

+ 21 - 0
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) {

+ 11 - 0
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) {
 

+ 29 - 15
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,20 +304,34 @@ 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 {