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

This commit is contained in:
Brandon Smith
2015-08-20 14:40:32 -03:00
parent 4f45f8e851
commit ef376dc054
5 changed files with 80 additions and 1 deletions

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);
}
}
}

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

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;
}
}
}

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

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)