From 31b2e22f57edf03f0e4c385ab0b34d1da67358d5 Mon Sep 17 00:00:00 2001 From: RogueException Date: Mon, 19 Oct 2015 20:05:24 -0300 Subject: [PATCH] Added EditServer(icon), Renamed AvatarImageType to ImageType --- src/Discord.Net/API/Channels.cs | 2 +- src/Discord.Net/API/Enums/AvatarImageType.cs | 2 +- src/Discord.Net/API/Members.cs | 6 ++-- src/Discord.Net/API/Servers.cs | 2 ++ src/Discord.Net/API/Users.cs | 10 +++--- src/Discord.Net/DiscordAPIClient.cs | 32 +++++++++++--------- src/Discord.Net/DiscordClient.API.cs | 18 +++++------ 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/Discord.Net/API/Channels.cs b/src/Discord.Net/API/Channels.cs index 030bda73d..ec5c2c5d5 100644 --- a/src/Discord.Net/API/Channels.cs +++ b/src/Discord.Net/API/Channels.cs @@ -40,7 +40,7 @@ namespace Discord.API public bool IsPrivate; [JsonProperty("position")] public int? Position; - [JsonProperty(PropertyName = "topic")] + [JsonProperty("topic")] public string Topic; [JsonProperty("permission_overwrites")] public PermissionOverwrite[] PermissionOverwrites; diff --git a/src/Discord.Net/API/Enums/AvatarImageType.cs b/src/Discord.Net/API/Enums/AvatarImageType.cs index 7b2895a04..738c67a3d 100644 --- a/src/Discord.Net/API/Enums/AvatarImageType.cs +++ b/src/Discord.Net/API/Enums/AvatarImageType.cs @@ -1,6 +1,6 @@ namespace Discord { - public enum AvatarImageType + public enum ImageType { None, Jpeg, diff --git a/src/Discord.Net/API/Members.cs b/src/Discord.Net/API/Members.cs index 9858e51ab..2d2805a8d 100644 --- a/src/Discord.Net/API/Members.cs +++ b/src/Discord.Net/API/Members.cs @@ -72,11 +72,11 @@ namespace Discord.API public class EditMemberRequest { - [JsonProperty(PropertyName = "mute", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("mute", NullValueHandling = NullValueHandling.Ignore)] public bool? Mute; - [JsonProperty(PropertyName = "deaf", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("deaf", NullValueHandling = NullValueHandling.Ignore)] public bool? Deaf; - [JsonProperty(PropertyName = "roles", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("roles", NullValueHandling = NullValueHandling.Ignore)] public IEnumerable Roles; } diff --git a/src/Discord.Net/API/Servers.cs b/src/Discord.Net/API/Servers.cs index b86a94ffa..925d5c923 100644 --- a/src/Discord.Net/API/Servers.cs +++ b/src/Discord.Net/API/Servers.cs @@ -67,6 +67,8 @@ namespace Discord.API public string Name; [JsonProperty("region", NullValueHandling = NullValueHandling.Ignore)] public string Region; + [JsonProperty("icon", NullValueHandling = NullValueHandling.Ignore)] + public string Icon; } public sealed class EditServerResponse : GuildInfo { } diff --git a/src/Discord.Net/API/Users.cs b/src/Discord.Net/API/Users.cs index 52bb8d6a2..a75363d2c 100644 --- a/src/Discord.Net/API/Users.cs +++ b/src/Discord.Net/API/Users.cs @@ -29,15 +29,15 @@ namespace Discord.API //Edit internal sealed class EditUserRequest { - [JsonProperty(PropertyName = "password")] + [JsonProperty("password")] public string CurrentPassword; - [JsonProperty(PropertyName = "email", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)] public string Email; - [JsonProperty(PropertyName = "new_password")] + [JsonProperty("new_password")] public string Password; - [JsonProperty(PropertyName = "username", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)] public string Username; - [JsonProperty(PropertyName = "avatar", NullValueHandling = NullValueHandling.Ignore)] + [JsonProperty("avatar", NullValueHandling = NullValueHandling.Ignore)] public string Avatar; } public sealed class EditUserResponse : UserInfo { } diff --git a/src/Discord.Net/DiscordAPIClient.cs b/src/Discord.Net/DiscordAPIClient.cs index bea5e5788..344013019 100644 --- a/src/Discord.Net/DiscordAPIClient.cs +++ b/src/Discord.Net/DiscordAPIClient.cs @@ -279,31 +279,22 @@ namespace Discord return _rest.Delete(Endpoints.Server(serverId)); } - public Task EditServer(string serverId, string name = null, string region = null) + public Task EditServer(string serverId, string name = null, string region = null, ImageType iconType = ImageType.Png, byte[] icon = null) { if (serverId == null) throw new ArgumentNullException(nameof(serverId)); - var request = new EditServerRequest { Name = name, Region = region }; + var request = new EditServerRequest { Name = name, Region = region, Icon = Base64Picture(iconType, icon) }; return _rest.Patch(Endpoints.Server(serverId), request); } //User public Task EditUser(string currentPassword = "", string username = null, string email = null, string password = null, - AvatarImageType avatarType = AvatarImageType.Png, byte[] avatar = null) + ImageType avatarType = ImageType.Png, byte[] avatar = null) { if (currentPassword == null) throw new ArgumentNullException(nameof(currentPassword)); - - string avatarBase64 = null; - if (avatarType == AvatarImageType.None) - avatarBase64 = ""; - else if (avatar != null) - { - string base64 = Convert.ToBase64String(avatar); - string type = avatarType == AvatarImageType.Jpeg ? "image/jpeg;base64" : "image/png;base64"; - avatarBase64 = $"data:{type},{base64}"; - } - var request = new EditUserRequest { CurrentPassword = currentPassword, Username = username, Email = email, Password = password, Avatar = avatarBase64 }; + + var request = new EditUserRequest { CurrentPassword = currentPassword, Username = username, Email = email, Password = password, Avatar = Base64Picture(avatarType, avatar) }; return _rest.Patch(Endpoints.UserMe, request); } @@ -312,5 +303,18 @@ namespace Discord => _rest.Get(Endpoints.VoiceRegions); /*public Task GetVoiceIce() => _rest.Get(Endpoints.VoiceIce);*/ + + private string Base64Picture(ImageType type, byte[] data) + { + if (type == ImageType.None) + return ""; + else if (data != null) + { + string base64 = Convert.ToBase64String(data); + string imageType = type == ImageType.Jpeg ? "image/jpeg;base64" : "image/png;base64"; + return $"data:{imageType},{base64}"; + } + return null; + } } } diff --git a/src/Discord.Net/DiscordClient.API.cs b/src/Discord.Net/DiscordClient.API.cs index a778f4a74..d26c4405d 100644 --- a/src/Discord.Net/DiscordClient.API.cs +++ b/src/Discord.Net/DiscordClient.API.cs @@ -596,7 +596,7 @@ namespace Discord //Profile public Task EditProfile(string currentPassword = "", string username = null, string email = null, string password = null, - AvatarImageType avatarType = AvatarImageType.Png, byte[] avatar = null) + ImageType avatarType = ImageType.Png, byte[] avatar = null) { if (currentPassword == null) throw new ArgumentNullException(nameof(currentPassword)); @@ -727,18 +727,16 @@ namespace Discord } /// Edits the provided server, changing only non-null attributes. - public Task EditServer(Server server) - => EditServer(server?.Id); + public Task EditServer(string serverId, string name = null, string region = null, ImageType iconType = ImageType.Png, byte[] icon = null) + => EditServer(_servers[serverId], name: name, region: region, iconType: iconType, icon: icon); /// Edits the provided server, changing only non-null attributes. - public async Task EditServer(string serverId, string name = null, string region = null) + public async Task EditServer(Server server, string name = null, string region = null, ImageType iconType = ImageType.Png, byte[] icon = null) { - CheckReady(); - if (serverId == null) throw new ArgumentNullException(nameof(serverId)); + CheckReady(); + if (server == null) throw new ArgumentNullException(nameof(server)); - var response = await _api.EditServer(serverId, name: name, region: region); - var server = _servers[response.Id]; - if (server != null) - server.Update(response); + var response = await _api.EditServer(server.Id, name: name ?? server.Name, region: region, iconType: iconType, icon: icon); + server.Update(response); } /// Leaves the provided server, destroying it if you are the owner.