Browse Source

Added support for changing username, password and email. Added UserChanged event.

tags/docs-0.9
Brandon Smith 9 years ago
parent
commit
ef376dc054
5 changed files with 80 additions and 1 deletions
  1. +18
    -0
      src/Discord.Net/API/DiscordAPI.cs
  2. +2
    -1
      src/Discord.Net/API/Endpoints.cs
  3. +26
    -0
      src/Discord.Net/API/Models/APIRequests.cs
  4. +6
    -0
      src/Discord.Net/DiscordClient.Events.cs
  5. +28
    -0
      src/Discord.Net/DiscordClient.cs

+ 18
- 0
src/Discord.Net/API/DiscordAPI.cs View File

@@ -112,5 +112,23 @@ namespace Discord.API
var request = new APIRequests.SetMemberDeaf { Deaf = false };
return Http.Patch(Endpoints.ServerMember(serverId, memberId));
}

//Profile
public static Task<SelfUserInfo> ChangeUsername(string newUsername, string currentEmail, string currentPassword)
{
var request = new APIRequests.ChangeUsername { Username = newUsername, CurrentEmail = currentEmail, CurrentPassword = currentPassword };
return Http.Patch<SelfUserInfo>(Endpoints.UserMe, request);
}
public static Task<SelfUserInfo> ChangeEmail(string newEmail, string currentPassword)
{
var request = new APIRequests.ChangeEmail { Email = newEmail, CurrentPassword = currentPassword };
return Http.Patch<SelfUserInfo>(Endpoints.UserMe, request);
}
public static Task<SelfUserInfo> ChangePassword(string newPassword, string currentEmail, string currentPassword)
{
var request = new APIRequests.ChangePassword { NewPassword = newPassword, CurrentEmail = currentEmail, CurrentPassword = currentPassword };
return Http.Patch<SelfUserInfo>(Endpoints.UserMe, request);
}

}
}

+ 2
- 1
src/Discord.Net/API/Endpoints.cs View File

@@ -40,7 +40,8 @@

// /api/users
public static readonly string Users = $"{BaseApi}/users";
public static string UserChannels(string userId) => $"{Users}/{userId}/channels";
public static string UserMe => $"{Users}/@me";
public static string UserChannels(string userId) => $"{Users}/{userId}/channels";
public static string UserAvatar(string userId, string avatarId) => $"{Users}/{userId}/avatars/{avatarId}.jpg";

// /api/voice


+ 26
- 0
src/Discord.Net/API/Models/APIRequests.cs View File

@@ -75,5 +75,31 @@ namespace Discord.API.Models
[JsonProperty(PropertyName = "deaf")]
public bool Deaf;
}

public class ChangeUsername
{
[JsonProperty(PropertyName = "username")]
public string Username;
[JsonProperty(PropertyName = "email")]
public string CurrentEmail;
[JsonProperty(PropertyName = "password")]
public string CurrentPassword;
}
public class ChangeEmail
{
[JsonProperty(PropertyName = "email")]
public string Email;
[JsonProperty(PropertyName = "password")]
public string CurrentPassword;
}
public class ChangePassword
{
[JsonProperty(PropertyName = "new_password")]
public string NewPassword;
[JsonProperty(PropertyName = "email")]
public string CurrentEmail;
[JsonProperty(PropertyName = "password")]
public string CurrentPassword;
}
}
}

+ 6
- 0
src/Discord.Net/DiscordClient.Events.cs View File

@@ -89,6 +89,12 @@ namespace Discord
public readonly User User;
internal UserEventArgs(User user) { User = user; }
}
public event EventHandler<UserEventArgs> UserUpdated;
private void RaiseUserUpdated(User user)
{
if (UserUpdated != null)
UserUpdated(this, new UserEventArgs(user));
}

//Message
public sealed class MessageEventArgs : EventArgs


+ 28
- 0
src/Discord.Net/DiscordClient.cs View File

@@ -381,6 +381,13 @@ namespace Discord
break;

//Settings
case "USER_UPDATE":
{
var data = e.Event.ToObject<WebSocketEvents.UserUpdate>();
var user = _users.Update(data.Id, data);
RaiseUserUpdate(user);
}
break;
case "USER_SETTINGS_UPDATE":
{
//TODO: Process this
@@ -844,6 +851,27 @@ namespace Discord
return DiscordAPI.Undeafen(serverId, userId);
}

//Profile
public async Task ChangeUsername(string newName, string currentEmail, string currentPassword)
{
CheckReady();
var response = await DiscordAPI.ChangeUsername(newName, currentEmail, currentPassword);
_users.Update(response.Id, response);
}
public async Task ChangeEmail(string newEmail, string currentPassword)
{
CheckReady();
var response = await DiscordAPI.ChangeEmail(newEmail, currentPassword);
_users.Update(response.Id, response);
}
public async Task ChangePassword(string newPassword, string currentEmail, string currentPassword)
{
CheckReady();
var response = await DiscordAPI.ChangePassword(newPassword, currentEmail, currentPassword);
_users.Update(response.Id, response);
}

//Helpers
private void CheckReady()
{
if (!_isReady)


Loading…
Cancel
Save