From 64681856b1fd742cc7b53e22a5cfe9492b7a9617 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sun, 4 Dec 2016 17:21:25 -0500 Subject: [PATCH 1/5] Create wrapper modify objects this was a big one --- .../API/DiscordRestApiClient.cs | 28 +++++++------- src/Discord.Net.Core/API/Image.cs | 5 +++ .../Channels/ModifyGuildChannelParams.cs | 8 ++++ .../Channels/ModifyGuildChannelsParams.cs | 14 +++++++ .../Channels/ModifyTextChannelParams.cs | 7 ++++ .../Channels/ModifyVoiceChannelParams.cs | 8 ++++ .../Entities/Guilds/ModifyGuildEmbedParams.cs | 8 ++++ .../Guilds/ModifyGuildIntegrationParams.cs | 9 +++++ .../Entities/Guilds/ModifyGuildParams.cs | 16 ++++++++ src/Discord.Net.Core/Entities/Image.cs | 17 +++++++++ .../Entities/Messages/EmbedBuilder.cs | 2 +- .../Entities/Roles/ModifyGuildRoleParams.cs | 11 ++++++ .../Entities/Roles/ModifyGuildRolesParams.cs | 12 ++++++ .../Users/ModifyCurrentUserNickParams.cs | 12 ++++++ .../Entities/Users/ModifyCurrentUserParams.cs | 8 ++++ .../Entities/Users/ModifyGuildMemberParams.cs | 11 ++++++ .../Net/Converters/ImageConverter.cs | 3 +- .../Entities/Channels/ChannelHelper.cs | 24 ++++++++++-- .../Entities/Guilds/GuildHelper.cs | 38 ++++++++++++++----- .../Entities/Guilds/RestGuildIntegration.cs | 8 +++- .../Entities/Roles/RoleHelper.cs | 10 ++++- .../Entities/Users/UserHelper.cs | 18 ++++++++- 22 files changed, 245 insertions(+), 32 deletions(-) create mode 100644 src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs create mode 100644 src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs create mode 100644 src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs create mode 100644 src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs create mode 100644 src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs create mode 100644 src/Discord.Net.Core/Entities/Guilds/ModifyGuildIntegrationParams.cs create mode 100644 src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs create mode 100644 src/Discord.Net.Core/Entities/Image.cs create mode 100644 src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs create mode 100644 src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs create mode 100644 src/Discord.Net.Core/Entities/Users/ModifyCurrentUserNickParams.cs create mode 100644 src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs create mode 100644 src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs diff --git a/src/Discord.Net.Core/API/DiscordRestApiClient.cs b/src/Discord.Net.Core/API/DiscordRestApiClient.cs index bc7436cd4..167be4397 100644 --- a/src/Discord.Net.Core/API/DiscordRestApiClient.cs +++ b/src/Discord.Net.Core/API/DiscordRestApiClient.cs @@ -331,7 +331,7 @@ namespace Discord.API var ids = new BucketIds(channelId: channelId); return await SendAsync("DELETE", () => $"channels/{channelId}", ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildChannelAsync(ulong channelId, ModifyGuildChannelParams args, RequestOptions options = null) + public async Task ModifyGuildChannelAsync(ulong channelId, Rest.ModifyGuildChannelParams args, RequestOptions options = null) { Preconditions.NotEqual(channelId, 0, nameof(channelId)); Preconditions.NotNull(args, nameof(args)); @@ -342,7 +342,7 @@ namespace Discord.API var ids = new BucketIds(channelId: channelId); return await SendJsonAsync("PATCH", () => $"channels/{channelId}", args, ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildChannelAsync(ulong channelId, ModifyTextChannelParams args, RequestOptions options = null) + public async Task ModifyGuildChannelAsync(ulong channelId, Rest.ModifyTextChannelParams args, RequestOptions options = null) { Preconditions.NotEqual(channelId, 0, nameof(channelId)); Preconditions.NotNull(args, nameof(args)); @@ -353,7 +353,7 @@ namespace Discord.API var ids = new BucketIds(channelId: channelId); return await SendJsonAsync("PATCH", () => $"channels/{channelId}", args, ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildChannelAsync(ulong channelId, ModifyVoiceChannelParams args, RequestOptions options = null) + public async Task ModifyGuildChannelAsync(ulong channelId, Rest.ModifyVoiceChannelParams args, RequestOptions options = null) { Preconditions.NotEqual(channelId, 0, nameof(channelId)); Preconditions.NotNull(args, nameof(args)); @@ -366,7 +366,7 @@ namespace Discord.API var ids = new BucketIds(channelId: channelId); return await SendJsonAsync("PATCH", () => $"channels/{channelId}", args, ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildChannelsAsync(ulong guildId, IEnumerable args, RequestOptions options = null) + public async Task ModifyGuildChannelsAsync(ulong guildId, IEnumerable args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotNull(args, nameof(args)); @@ -378,7 +378,7 @@ namespace Discord.API case 0: return; case 1: - await ModifyGuildChannelAsync(channels[0].Id, new ModifyGuildChannelParams { Position = channels[0].Position }).ConfigureAwait(false); + await ModifyGuildChannelAsync(channels[0].Id, new Rest.ModifyGuildChannelParams { Position = channels[0].Position }).ConfigureAwait(false); break; default: var ids = new BucketIds(guildId: guildId); @@ -695,7 +695,7 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); return await SendAsync("DELETE", () => $"users/@me/guilds/{guildId}", ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildAsync(ulong guildId, ModifyGuildParams args, RequestOptions options = null) + public async Task ModifyGuildAsync(ulong guildId, Rest.ModifyGuildParams args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotNull(args, nameof(args)); @@ -773,7 +773,7 @@ namespace Discord.API } catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { return null; } } - public async Task ModifyGuildEmbedAsync(ulong guildId, ModifyGuildEmbedParams args, RequestOptions options = null) + public async Task ModifyGuildEmbedAsync(ulong guildId, Rest.ModifyGuildEmbedParams args, RequestOptions options = null) { Preconditions.NotNull(args, nameof(args)); Preconditions.NotEqual(guildId, 0, nameof(guildId)); @@ -811,7 +811,7 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); return await SendAsync("DELETE", () => $"guilds/{guildId}/integrations/{integrationId}", ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildIntegrationAsync(ulong guildId, ulong integrationId, ModifyGuildIntegrationParams args, RequestOptions options = null) + public async Task ModifyGuildIntegrationAsync(ulong guildId, ulong integrationId, Rest.ModifyGuildIntegrationParams args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); @@ -934,7 +934,7 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); await SendAsync("DELETE", () => $"guilds/{guildId}/members/{userId}", ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildMemberAsync(ulong guildId, ulong userId, ModifyGuildMemberParams args, RequestOptions options = null) + public async Task ModifyGuildMemberAsync(ulong guildId, ulong userId, Rest.ModifyGuildMemberParams args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotEqual(userId, 0, nameof(userId)); @@ -945,7 +945,7 @@ namespace Discord.API if (isCurrentUser && args.Nickname.IsSpecified) { - var nickArgs = new ModifyCurrentUserNickParams(args.Nickname.Value ?? ""); + var nickArgs = new Rest.ModifyCurrentUserNickParams(args.Nickname.Value ?? ""); await ModifyMyNickAsync(guildId, nickArgs).ConfigureAwait(false); args.Nickname = Optional.Create(); //Remove } @@ -982,7 +982,7 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); await SendAsync("DELETE", () => $"guilds/{guildId}/roles/{roleId}", ids, options: options).ConfigureAwait(false); } - public async Task ModifyGuildRoleAsync(ulong guildId, ulong roleId, ModifyGuildRoleParams args, RequestOptions options = null) + public async Task ModifyGuildRoleAsync(ulong guildId, ulong roleId, Rest.ModifyGuildRoleParams args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotEqual(roleId, 0, nameof(roleId)); @@ -995,7 +995,7 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); return await SendJsonAsync("PATCH", () => $"guilds/{guildId}/roles/{roleId}", args, ids, options: options).ConfigureAwait(false); } - public async Task> ModifyGuildRolesAsync(ulong guildId, IEnumerable args, RequestOptions options = null) + public async Task> ModifyGuildRolesAsync(ulong guildId, IEnumerable args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotNull(args, nameof(args)); @@ -1053,7 +1053,7 @@ namespace Discord.API options = RequestOptions.CreateOrClone(options); return await SendAsync("GET", () => "oauth2/applications/@me", new BucketIds(), options: options).ConfigureAwait(false); } - public async Task ModifySelfAsync(ModifyCurrentUserParams args, RequestOptions options = null) + public async Task ModifySelfAsync(Rest.ModifyCurrentUserParams args, RequestOptions options = null) { Preconditions.NotNull(args, nameof(args)); Preconditions.NotNullOrEmpty(args.Username, nameof(args.Username)); @@ -1061,7 +1061,7 @@ namespace Discord.API return await SendJsonAsync("PATCH", () => "users/@me", args, new BucketIds(), options: options).ConfigureAwait(false); } - public async Task ModifyMyNickAsync(ulong guildId, ModifyCurrentUserNickParams args, RequestOptions options = null) + public async Task ModifyMyNickAsync(ulong guildId, Rest.ModifyCurrentUserNickParams args, RequestOptions options = null) { Preconditions.NotNull(args, nameof(args)); Preconditions.NotNull(args.Nickname, nameof(args.Nickname)); diff --git a/src/Discord.Net.Core/API/Image.cs b/src/Discord.Net.Core/API/Image.cs index 5442bd30f..44c58f344 100644 --- a/src/Discord.Net.Core/API/Image.cs +++ b/src/Discord.Net.Core/API/Image.cs @@ -17,5 +17,10 @@ namespace Discord.API Stream = null; Hash = hash; } + + public static Image Create(Discord.Image image) + { + return new Image(image.Stream); + } } } diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs new file mode 100644 index 000000000..2f1cce4b1 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs @@ -0,0 +1,8 @@ +namespace Discord +{ + public class ModifyGuildChannelParams + { + public Optional Name { get; set; } + public Optional Position { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs new file mode 100644 index 000000000..e46e172b9 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs @@ -0,0 +1,14 @@ +namespace Discord +{ + public class ModifyGuildChannelsParams + { + public ulong Id { get; set; } + public int Position { get; set; } + + public ModifyGuildChannelsParams(ulong id, int position) + { + Id = id; + Position = position; + } + } +} diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs new file mode 100644 index 000000000..fc5c2aad4 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs @@ -0,0 +1,7 @@ +namespace Discord +{ + public class ModifyTextChannelParams : ModifyGuildChannelParams + { + public Optional Topic { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs new file mode 100644 index 000000000..36941e03b --- /dev/null +++ b/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs @@ -0,0 +1,8 @@ +namespace Discord +{ + public class ModifyVoiceChannelParams : ModifyGuildChannelParams + { + public Optional Bitrate { get; set; } + public Optional UserLimit { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs new file mode 100644 index 000000000..83a32a79c --- /dev/null +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs @@ -0,0 +1,8 @@ +namespace Discord +{ + public class ModifyGuildEmbedParams + { + public Optional Enabled { get; set; } + public Optional ChannelId { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildIntegrationParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildIntegrationParams.cs new file mode 100644 index 000000000..b9375d907 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildIntegrationParams.cs @@ -0,0 +1,9 @@ +namespace Discord +{ + public class ModifyGuildIntegrationParams + { + public Optional ExpireBehavior { get; set; } + public Optional ExpireGracePeriod { get; set; } + public Optional EnableEmoticons { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs new file mode 100644 index 000000000..19f2476f2 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs @@ -0,0 +1,16 @@ +namespace Discord +{ + public class ModifyGuildParams + { + public Optional Username { get; set; } + public Optional Name { get; set; } + public Optional RegionId { get; set; } + public Optional VerificationLevel { get; set; } + public Optional DefaultMessageNotifications { get; set; } + public Optional AfkTimeout { get; set; } + public Optional Icon { get; set; } + public Optional Splash { get; set; } + public Optional AfkChannelId { get; set; } + public Optional OwnerId { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Image.cs b/src/Discord.Net.Core/Entities/Image.cs new file mode 100644 index 000000000..bb305f113 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Image.cs @@ -0,0 +1,17 @@ +using System.IO; + +namespace Discord +{ + public struct Image + { + public Stream Stream { get; } + public Image(Stream stream) + { + Stream = stream; + } + public Image(string path) + { + Stream = File.OpenRead(path); + } + } +} diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs index 40ef93e10..a6c9cfb8c 100644 --- a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -5,7 +5,7 @@ using Field = Discord.API.EmbedField; using Author = Discord.API.EmbedAuthor; using Footer = Discord.API.EmbedFooter; using Thumbnail = Discord.API.EmbedThumbnail; -using Image = Discord.API.EmbedImage; +using ImageEmbed = Discord.API.EmbedImage; namespace Discord { diff --git a/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs new file mode 100644 index 000000000..486d56513 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs @@ -0,0 +1,11 @@ +namespace Discord +{ + public class ModifyGuildRoleParams + { + public Optional Name { get; set; } + public Optional Permissions { get; set; } + public Optional Position { get; set; } + public Optional Color { get; set; } + public Optional Hoist { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs new file mode 100644 index 000000000..4d6a0f63b --- /dev/null +++ b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs @@ -0,0 +1,12 @@ +namespace Discord +{ + public class ModifyGuildRolesParams : ModifyGuildRoleParams + { + public ulong Id { get; } + + public ModifyGuildRolesParams(ulong id) + { + Id = id; + } + } +} diff --git a/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserNickParams.cs b/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserNickParams.cs new file mode 100644 index 000000000..c8d2b5199 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserNickParams.cs @@ -0,0 +1,12 @@ +namespace Discord +{ + public class ModifyCurrentUserNickParams + { + public string Nickname { get; } + + public ModifyCurrentUserNickParams(string nickname) + { + Nickname = nickname; + } + } +} diff --git a/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs b/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs new file mode 100644 index 000000000..cdd031c34 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs @@ -0,0 +1,8 @@ +namespace Discord +{ + public class ModifyCurrentUserParams + { + public Optional Username { get; set; } + public Optional Avatar { get; set; } + } +} diff --git a/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs b/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs new file mode 100644 index 000000000..8e74d80e0 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs @@ -0,0 +1,11 @@ +namespace Discord +{ + public class ModifyGuildMemberParams + { + public Optional Mute { get; set; } + public Optional Deaf { get; set; } + public Optional Nickname { get; set; } + public Optional RoleIds { get; set; } + public Optional ChannelId { get; set; } + } +} diff --git a/src/Discord.Net.Core/Net/Converters/ImageConverter.cs b/src/Discord.Net.Core/Net/Converters/ImageConverter.cs index 79e8c984d..e82b7952b 100644 --- a/src/Discord.Net.Core/Net/Converters/ImageConverter.cs +++ b/src/Discord.Net.Core/Net/Converters/ImageConverter.cs @@ -1,6 +1,7 @@ using Discord.API; using Newtonsoft.Json; using System; +using Model = Discord.API.Image; namespace Discord.Net.Converters { @@ -19,7 +20,7 @@ namespace Discord.Net.Converters public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - var image = (Image)value; + var image = (Model)value; if (image.Stream != null) { diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index 99edc6f48..9db89bb34 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -24,7 +24,12 @@ namespace Discord.Rest { var args = new ModifyGuildChannelParams(); func(args); - return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyGuildChannelParams + { + Name = args.Name, + Position = args.Position + }; + return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false); } public static async Task ModifyAsync(ITextChannel channel, BaseDiscordClient client, Action func, @@ -32,7 +37,13 @@ namespace Discord.Rest { var args = new ModifyTextChannelParams(); func(args); - return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyTextChannelParams + { + Name = args.Name, + Position = args.Position, + Topic = args.Topic + }; + return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false); } public static async Task ModifyAsync(IVoiceChannel channel, BaseDiscordClient client, Action func, @@ -40,7 +51,14 @@ namespace Discord.Rest { var args = new ModifyVoiceChannelParams(); func(args); - return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyVoiceChannelParams + { + Bitrate = args.Bitrate, + Name = args.Name, + Position = args.Position, + UserLimit = args.UserLimit + }; + return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false); } //Invites diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 7aaa19304..fb3e8e2cf 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using EmbedModel = Discord.API.GuildEmbed; using Model = Discord.API.Guild; using RoleModel = Discord.API.Role; +using ImageModel = Discord.API.Image; namespace Discord.Rest { @@ -21,12 +22,24 @@ namespace Discord.Rest var args = new ModifyGuildParams(); func(args); - if (args.Splash.IsSpecified && guild.SplashId != null) - args.Splash = new API.Image(guild.SplashId); - if (args.Icon.IsSpecified && guild.IconId != null) - args.Icon = new API.Image(guild.IconId); - - return await client.ApiClient.ModifyGuildAsync(guild.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyGuildParams + { + AfkChannelId = args.AfkChannelId, + AfkTimeout = args.AfkTimeout, + DefaultMessageNotifications = args.DefaultMessageNotifications, + Name = args.Name, + OwnerId = args.OwnerId, + RegionId = args.RegionId, + Username = args.Username, + VerificationLevel = args.VerificationLevel + }; + + if (apiArgs.Splash.IsSpecified && guild.SplashId != null) + apiArgs.Splash = new ImageModel(guild.SplashId); + if (apiArgs.Icon.IsSpecified && guild.IconId != null) + apiArgs.Icon = new ImageModel(guild.IconId); + + return await client.ApiClient.ModifyGuildAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } public static async Task ModifyEmbedAsync(IGuild guild, BaseDiscordClient client, Action func, RequestOptions options) @@ -35,17 +48,24 @@ namespace Discord.Rest var args = new ModifyGuildEmbedParams(); func(args); - return await client.ApiClient.ModifyGuildEmbedAsync(guild.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyGuildEmbedParams + { + ChannelId = args.ChannelId, + Enabled = args.Enabled + }; + return await client.ApiClient.ModifyGuildEmbedAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } public static async Task ModifyChannelsAsync(IGuild guild, BaseDiscordClient client, IEnumerable args, RequestOptions options) { - await client.ApiClient.ModifyGuildChannelsAsync(guild.Id, args, options).ConfigureAwait(false); + var apiArgs = args.Select(x => new API.Rest.ModifyGuildChannelsParams(x.Id, x.Position)); + await client.ApiClient.ModifyGuildChannelsAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } public static async Task> ModifyRolesAsync(IGuild guild, BaseDiscordClient client, IEnumerable args, RequestOptions options) { - return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, args, options).ConfigureAwait(false); + var apiArgs = args.Select(x => new API.Rest.ModifyGuildRolesParams(x.Id) { Color = x.Color, Hoist = x.Hoist, Name = x.Name, Permissions = x.Permissions, Position = x.Position }); + return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs index fc2bfd8b2..5405becc4 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs @@ -61,7 +61,13 @@ namespace Discord.Rest var args = new ModifyGuildIntegrationParams(); func(args); - var model = await Discord.ApiClient.ModifyGuildIntegrationAsync(GuildId, Id, args).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyGuildIntegrationParams + { + EnableEmoticons = args.EnableEmoticons, + ExpireBehavior = args.ExpireBehavior, + ExpireGracePeriod = args.ExpireGracePeriod + }; + var model = await Discord.ApiClient.ModifyGuildIntegrationAsync(GuildId, Id, apiArgs).ConfigureAwait(false); Update(model); } diff --git a/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs b/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs index 0b102098c..bea2547e9 100644 --- a/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs +++ b/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs @@ -18,7 +18,15 @@ namespace Discord.Rest { var args = new ModifyGuildRoleParams(); func(args); - return await client.ApiClient.ModifyGuildRoleAsync(role.Guild.Id, role.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyGuildRoleParams + { + Color = args.Color, + Hoist = args.Hoist, + Name = args.Name, + Permissions = args.Permissions, + Position = args.Position + }; + return await client.ApiClient.ModifyGuildRoleAsync(role.Guild.Id, role.Id, apiArgs, options).ConfigureAwait(false); } } } diff --git a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs index 545703f0d..4499e1ec1 100644 --- a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs +++ b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs @@ -2,6 +2,7 @@ using System; using System.Threading.Tasks; using Model = Discord.API.User; +using ImageModel = Discord.API.Image; namespace Discord.Rest { @@ -12,14 +13,27 @@ namespace Discord.Rest { var args = new ModifyCurrentUserParams(); func(args); - return await client.ApiClient.ModifySelfAsync(args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyCurrentUserParams + { + Avatar = args.Avatar.IsSpecified ? ImageModel.Create(args.Avatar.Value) : Optional.Create(), + Username = args.Username + }; + return await client.ApiClient.ModifySelfAsync(apiArgs, options).ConfigureAwait(false); } public static async Task ModifyAsync(IGuildUser user, BaseDiscordClient client, Action func, RequestOptions options) { var args = new ModifyGuildMemberParams(); func(args); - await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, args, options).ConfigureAwait(false); + var apiArgs = new API.Rest.ModifyGuildMemberParams + { + ChannelId = args.ChannelId, + Deaf = args.Deaf, + Mute = args.Mute, + Nickname = args.Nickname, + RoleIds = args.RoleIds + }; + await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, apiArgs, options).ConfigureAwait(false); return args; } From e2aa0e92a4a9d1ea5ac3354ad94db2d8d5c9f5ec Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sun, 4 Dec 2016 17:35:36 -0500 Subject: [PATCH 2/5] Slight modification to fix images in thumbnails --- src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs index a6c9cfb8c..767641f0e 100644 --- a/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs +++ b/src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs @@ -110,7 +110,7 @@ namespace Discord _model.Footer = Footer?.ToModel(); _model.Timestamp = Timestamp?.ToUniversalTime(); _model.Thumbnail = ThumbnailUrl != null ? new Thumbnail { Url = ThumbnailUrl } : null; - _model.Image = ImageUrl != null ? new Image { Url = ImageUrl } : null; + _model.Image = ImageUrl != null ? new ImageEmbed { Url = ImageUrl } : null; _model.Fields = _fields.ToArray(); return _model; } From a4e95923b66e7fdee008ebe44832b1394208f6f3 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sun, 4 Dec 2016 17:51:57 -0500 Subject: [PATCH 3/5] Bitrate must be at least 8000 per the following API error: { "bitrate": [ "Int value should be greater than 8000." ] } --- src/Discord.Net.Core/API/DiscordRestApiClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Core/API/DiscordRestApiClient.cs b/src/Discord.Net.Core/API/DiscordRestApiClient.cs index 167be4397..00253a3eb 100644 --- a/src/Discord.Net.Core/API/DiscordRestApiClient.cs +++ b/src/Discord.Net.Core/API/DiscordRestApiClient.cs @@ -357,7 +357,7 @@ namespace Discord.API { Preconditions.NotEqual(channelId, 0, nameof(channelId)); Preconditions.NotNull(args, nameof(args)); - Preconditions.GreaterThan(args.Bitrate, 0, nameof(args.Bitrate)); + Preconditions.GreaterThan(args.Bitrate, 8000, nameof(args.Bitrate)); Preconditions.AtLeast(args.UserLimit, 0, nameof(args.Bitrate)); Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name)); From 3fc043132b62328638324f279061add28a32c9f9 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sun, 4 Dec 2016 19:07:07 -0500 Subject: [PATCH 4/5] docstrings for modify params, minor bugfixes --- .../Channels/ModifyGuildChannelParams.cs | 22 +++++++++ .../Channels/ModifyTextChannelParams.cs | 4 ++ .../Channels/ModifyVoiceChannelParams.cs | 7 +++ .../Entities/Guilds/ModifyGuildEmbedParams.cs | 9 ++++ .../Entities/Guilds/ModifyGuildParams.cs | 43 +++++++++++++++++ src/Discord.Net.Core/Entities/Image.cs | 15 ++++++ .../Entities/Messages/ModifyMessageParams.cs | 35 ++++++++++++-- .../Entities/Roles/ModifyGuildRoleParams.cs | 44 ++++++++++++++++- .../Entities/Users/ModifyCurrentUserParams.cs | 18 +++++++ .../Entities/Users/ModifyGuildMemberParams.cs | 47 ++++++++++++++++++- src/Discord.Net.Core/Utils/Preconditions.cs | 1 + .../Entities/Guilds/GuildHelper.cs | 13 +++-- .../Entities/Roles/RoleHelper.cs | 4 +- .../Entities/Users/UserHelper.cs | 5 +- 14 files changed, 251 insertions(+), 16 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs index 2f1cce4b1..005460915 100644 --- a/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs +++ b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelParams.cs @@ -1,8 +1,30 @@ namespace Discord { + /// + /// Modify an IGuildChannel with the specified changes. + /// + /// + /// + /// await (Context.Channel as ITextChannel)?.ModifyAsync(x => + /// { + /// x.Name = "do-not-enter"; + /// }); + /// + /// public class ModifyGuildChannelParams { + /// + /// Set the channel to this name + /// + /// + /// When modifying an ITextChannel, the Name MUST be alphanumeric with dashes. + /// It must match the following RegEx: [a-z0-9-_]{2,100} + /// + /// A BadRequest will be thrown if the name does not match the above RegEx. public Optional Name { get; set; } + /// + /// Move the channel to the following position. This is 0-based! + /// public Optional Position { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs index fc5c2aad4..d144706d9 100644 --- a/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs +++ b/src/Discord.Net.Core/Entities/Channels/ModifyTextChannelParams.cs @@ -1,7 +1,11 @@ namespace Discord { + /// public class ModifyTextChannelParams : ModifyGuildChannelParams { + /// + /// What the topic of the channel should be set to. + /// public Optional Topic { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs index 36941e03b..bb3d2fee5 100644 --- a/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs +++ b/src/Discord.Net.Core/Entities/Channels/ModifyVoiceChannelParams.cs @@ -1,8 +1,15 @@ namespace Discord { + /// public class ModifyVoiceChannelParams : ModifyGuildChannelParams { + /// + /// The bitrate of the voice connections in this channel. Must be greater than 8000 + /// public Optional Bitrate { get; set; } + /// + /// The maximum number of users that can be present in a channel. + /// public Optional UserLimit { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs index 83a32a79c..11b72774c 100644 --- a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs @@ -1,8 +1,17 @@ namespace Discord { + /// + /// Modify the widget of an IGuild with the specified parameters + /// public class ModifyGuildEmbedParams { + /// + /// Should the widget be enabled? + /// public Optional Enabled { get; set; } + /// + /// What channel should the invite place users in, if not null. + /// public Optional ChannelId { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs index 19f2476f2..08deff727 100644 --- a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs @@ -1,16 +1,59 @@ namespace Discord { + /// + /// Modify an IGuild with the specified changes + /// + /// + /// + /// await Context.Guild.ModifyAsync(async x => + /// { + /// x.Name = "aaaaaah"; + /// x.RegionId = (await Context.Client.GetOptimalVoiceRegionAsync()).Id; + /// }); + /// + /// + /// public class ModifyGuildParams { public Optional Username { get; set; } + /// + /// The name of the Guild + /// public Optional Name { get; set; } + /// + /// The ID of the region for the Guild's voice connections + /// public Optional RegionId { get; set; } + /// + /// What verification level new users need to achieve before speaking + /// public Optional VerificationLevel { get; set; } + /// + /// The default message notification state for the guild + /// public Optional DefaultMessageNotifications { get; set; } + /// + /// How many seconds before a user is sent to AFK. This value MUST be one of: (60, 300, 900, 1800, 3600). + /// public Optional AfkTimeout { get; set; } + /// + /// The icon of the guild + /// public Optional Icon { get; set; } + /// + /// The guild's splash image + /// + /// + /// The guild must be partnered for this value to have any effect. + /// public Optional Splash { get; set; } + /// + /// The ID of the IVoiceChannel where AFK users should be sent. + /// public Optional AfkChannelId { get; set; } + /// + /// The ID of the owner of this guild. + /// public Optional OwnerId { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Image.cs b/src/Discord.Net.Core/Entities/Image.cs index bb305f113..538d82730 100644 --- a/src/Discord.Net.Core/Entities/Image.cs +++ b/src/Discord.Net.Core/Entities/Image.cs @@ -2,13 +2,28 @@ namespace Discord { + /// + /// An image that will be uploaded to Discord. + /// public struct Image { public Stream Stream { get; } + /// + /// Create the image with a Stream. + /// + /// This must be some type of stream with the contents of a file in it. + /// public Image(Stream stream) { Stream = stream; } + /// + /// Create the image from a file path. + /// + /// + /// This file path is NOT validated, and is passed directly into a + /// + /// The path to the file. public Image(string path) { Stream = File.OpenRead(path); diff --git a/src/Discord.Net.Core/Entities/Messages/ModifyMessageParams.cs b/src/Discord.Net.Core/Entities/Messages/ModifyMessageParams.cs index cc05e620a..6b7e3b478 100644 --- a/src/Discord.Net.Core/Entities/Messages/ModifyMessageParams.cs +++ b/src/Discord.Net.Core/Entities/Messages/ModifyMessageParams.cs @@ -1,12 +1,37 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Discord +namespace Discord { + /// + /// Modify a message with the specified parameters. + /// + /// + /// The content of a message can be cleared with String.Empty; if and only if an Embed is present. + /// + /// + /// + /// var message = await ReplyAsync("abc"); + /// await message.ModifyAsync(x => + /// { + /// x.Content = ""; + /// x.Embed = new EmbedBuilder() + /// .WithColor(new Color(40, 40, 120)) + /// .WithAuthor(a => a.Name = "foxbot") + /// .WithTitle("Embed!") + /// .WithDescription("This is an embed."); + /// }); + /// + /// public class ModifyMessageParams { + /// + /// The content of the message + /// + /// + /// This must be less than 2000 characters. + /// public Optional Content { get; set; } + /// + /// The embed the message should display + /// public Optional Embed { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs index 486d56513..fa99c5bcc 100644 --- a/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs +++ b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRoleParams.cs @@ -1,11 +1,51 @@ namespace Discord { + /// + /// Modify an IRole with the specified parameters + /// + /// + /// + /// await role.ModifyAsync(x => + /// { + /// x.Color = new Color(180, 15, 40); + /// x.Hoist = true; + /// }); + /// + /// + /// public class ModifyGuildRoleParams { + /// + /// The name of the role + /// + /// + /// If this role is the EveryoneRole, this value may not be set. + /// public Optional Name { get; set; } - public Optional Permissions { get; set; } + /// + /// The role's GuildPermissions + /// + public Optional Permissions { get; set; } + /// + /// The position of the role. This is 0-based! + /// + /// + /// If this role is the EveryoneRole, this value may not be set. + /// public Optional Position { get; set; } - public Optional Color { get; set; } + /// + /// The color of the Role. + /// + /// + /// If this role is the EveryoneRole, this value may not be set. + /// + public Optional Color { get; set; } + /// + /// Whether or not this role should be displayed independently in the userlist. + /// + /// + /// If this role is the EveryoneRole, this value may not be set. + /// public Optional Hoist { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs b/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs index cdd031c34..c517b88df 100644 --- a/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs +++ b/src/Discord.Net.Core/Entities/Users/ModifyCurrentUserParams.cs @@ -1,8 +1,26 @@ namespace Discord { + /// + /// Modify the current user with the specified arguments + /// + /// + /// + /// await Context.Client.CurrentUser.ModifyAsync(x => + /// { + /// x.Avatar = new Image(File.OpenRead("avatar.jpg")); + /// }); + /// + /// + /// public class ModifyCurrentUserParams { + /// + /// Your username + /// public Optional Username { get; set; } + /// + /// Your avatar + /// public Optional Avatar { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs b/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs index 8e74d80e0..57909cd03 100644 --- a/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs +++ b/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs @@ -1,11 +1,54 @@ namespace Discord { + /// + /// Modify an IGuildUser with the following parameters. + /// + /// + /// + /// await (Context.User as IGuildUser)?.ModifyAsync(x => + /// { + /// x.Nickname = $"festive {Context.User.Username}"; + /// }); + /// + /// + /// public class ModifyGuildMemberParams { + /// + /// Should the user be guild-muted in a voice channel? + /// + /// + /// If this value is set to true, no user will be able to hear this user speak in the guild. + /// public Optional Mute { get; set; } + /// + /// Should the user be guild-deafened in a voice channel? + /// + /// + /// If this value is set to true, this user will not be able to hear anyone speak in the guild. + /// public Optional Deaf { get; set; } + /// + /// Should the user have a nickname set? + /// + /// + /// To clear the user's nickname, this value can be set to null. + /// public Optional Nickname { get; set; } - public Optional RoleIds { get; set; } - public Optional ChannelId { get; set; } + /// + /// What roles should the user have? + /// + /// + /// To add a role to a user: + /// To remove a role from a user: + /// + public Optional Roles { get; set; } + /// + /// Move a user to a voice channel. + /// + /// + /// This user MUST already be in a Voice Channel for this to work. + /// + public Optional Channel { get; set; } } } diff --git a/src/Discord.Net.Core/Utils/Preconditions.cs b/src/Discord.Net.Core/Utils/Preconditions.cs index 14a730b7e..02cc5d288 100644 --- a/src/Discord.Net.Core/Utils/Preconditions.cs +++ b/src/Discord.Net.Core/Utils/Preconditions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace Discord { diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index fb3e8e2cf..428541509 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -64,7 +64,14 @@ namespace Discord.Rest public static async Task> ModifyRolesAsync(IGuild guild, BaseDiscordClient client, IEnumerable args, RequestOptions options) { - var apiArgs = args.Select(x => new API.Rest.ModifyGuildRolesParams(x.Id) { Color = x.Color, Hoist = x.Hoist, Name = x.Name, Permissions = x.Permissions, Position = x.Position }); + var apiArgs = args.Select(x => new API.Rest.ModifyGuildRolesParams(x.Id) + { + Color = x.Color.IsSpecified ? x.Color.Value.RawValue : Optional.Create(), + Hoist = x.Hoist, + Name = x.Name, + Permissions = x.Permissions.IsSpecified ? x.Permissions.Value.RawValue : Optional.Create(), + Position = x.Position + }); return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client, @@ -167,8 +174,8 @@ namespace Discord.Rest await role.ModifyAsync(x => { x.Name = name; - x.Permissions = (permissions ?? role.Permissions).RawValue; - x.Color = (color ?? Color.Default).RawValue; + x.Permissions = (permissions ?? role.Permissions); + x.Color = (color ?? Color.Default); x.Hoist = isHoisted; }, options).ConfigureAwait(false); diff --git a/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs b/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs index bea2547e9..4b786b6e6 100644 --- a/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs +++ b/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs @@ -20,10 +20,10 @@ namespace Discord.Rest func(args); var apiArgs = new API.Rest.ModifyGuildRoleParams { - Color = args.Color, + Color = args.Color.IsSpecified ? args.Color.Value.RawValue : Optional.Create(), Hoist = args.Hoist, Name = args.Name, - Permissions = args.Permissions, + Permissions = args.Permissions.IsSpecified ? args.Permissions.Value.RawValue : Optional.Create(), Position = args.Position }; return await client.ApiClient.ModifyGuildRoleAsync(role.Guild.Id, role.Id, apiArgs, options).ConfigureAwait(false); diff --git a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs index 4499e1ec1..dbfa2f2e5 100644 --- a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs +++ b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs @@ -3,6 +3,7 @@ using System; using System.Threading.Tasks; using Model = Discord.API.User; using ImageModel = Discord.API.Image; +using System.Linq; namespace Discord.Rest { @@ -27,11 +28,11 @@ namespace Discord.Rest func(args); var apiArgs = new API.Rest.ModifyGuildMemberParams { - ChannelId = args.ChannelId, + ChannelId = args.Channel.IsSpecified ? args.Channel.Value.Id : Optional.Create(), Deaf = args.Deaf, Mute = args.Mute, Nickname = args.Nickname, - RoleIds = args.RoleIds + RoleIds = args.Roles.IsSpecified ? args.Roles.Value.Select(r => r.Id).ToArray() : Optional.Create(), }; await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, apiArgs, options).ConfigureAwait(false); return args; From 86d9f524384d2445adcb91b9456b0b49346c98c5 Mon Sep 17 00:00:00 2001 From: RogueException Date: Fri, 16 Dec 2016 11:11:07 -0400 Subject: [PATCH 5/5] Cleaned up new params --- src/Discord.Net.Core/API/Image.cs | 9 +++- .../API/Rest/ModifyCurrentUserParams.cs | 2 +- .../Channels/ModifyGuildChannelsParams.cs | 6 +++ .../Entities/Guilds/ModifyGuildEmbedParams.cs | 4 ++ .../Entities/Guilds/ModifyGuildParams.cs | 12 +++++ src/Discord.Net.Core/Entities/Image.cs | 2 + .../Entities/Roles/ModifyGuildRolesParams.cs | 3 ++ .../Entities/Users/ModifyGuildMemberParams.cs | 14 ++++- .../Entities/Guilds/GuildHelper.cs | 52 +++++++++++++------ .../Entities/Users/RestGuildUser.cs | 15 ++++-- .../Entities/Users/UserHelper.cs | 15 ++++-- 11 files changed, 106 insertions(+), 28 deletions(-) diff --git a/src/Discord.Net.Core/API/Image.cs b/src/Discord.Net.Core/API/Image.cs index 44c58f344..a500ebfd5 100644 --- a/src/Discord.Net.Core/API/Image.cs +++ b/src/Discord.Net.Core/API/Image.cs @@ -18,9 +18,16 @@ namespace Discord.API Hash = hash; } - public static Image Create(Discord.Image image) + internal static Image Create(Discord.Image image) { return new Image(image.Stream); } + internal static Image? Create(Discord.Image? image) + { + if (image.HasValue) + return new Image(image.Value.Stream); + else + return null; + } } } diff --git a/src/Discord.Net.Core/API/Rest/ModifyCurrentUserParams.cs b/src/Discord.Net.Core/API/Rest/ModifyCurrentUserParams.cs index d11ef2b77..3f7afe187 100644 --- a/src/Discord.Net.Core/API/Rest/ModifyCurrentUserParams.cs +++ b/src/Discord.Net.Core/API/Rest/ModifyCurrentUserParams.cs @@ -9,6 +9,6 @@ namespace Discord.API.Rest [JsonProperty("username")] public Optional Username { get; set; } [JsonProperty("avatar")] - public Optional Avatar { get; set; } + public Optional Avatar { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs index e46e172b9..14b77457f 100644 --- a/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs +++ b/src/Discord.Net.Core/Entities/Channels/ModifyGuildChannelsParams.cs @@ -2,7 +2,13 @@ { public class ModifyGuildChannelsParams { + /// + /// The id of the channel to apply this position to. + /// public ulong Id { get; set; } + /// + /// The new zero-based position of this channel. + /// public int Position { get; set; } public ModifyGuildChannelsParams(ulong id, int position) diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs index 11b72774c..f5d212e17 100644 --- a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildEmbedParams.cs @@ -12,6 +12,10 @@ /// /// What channel should the invite place users in, if not null. /// + public Optional Channel { get; set; } + /// + /// What channel should the invite place users in, if not null. + /// public Optional ChannelId { get; set; } } } diff --git a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs index 08deff727..ec7866b8a 100644 --- a/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs +++ b/src/Discord.Net.Core/Entities/Guilds/ModifyGuildParams.cs @@ -21,6 +21,10 @@ /// public Optional Name { get; set; } /// + /// The region for the Guild's voice connections + /// + public Optional Region { get; set; } + /// /// The ID of the region for the Guild's voice connections /// public Optional RegionId { get; set; } @@ -48,10 +52,18 @@ /// public Optional Splash { get; set; } /// + /// The IVoiceChannel where AFK users should be sent. + /// + public Optional AfkChannel { get; set; } + /// /// The ID of the IVoiceChannel where AFK users should be sent. /// public Optional AfkChannelId { get; set; } /// + /// The owner of this guild. + /// + public Optional Owner { get; set; } + /// /// The ID of the owner of this guild. /// public Optional OwnerId { get; set; } diff --git a/src/Discord.Net.Core/Entities/Image.cs b/src/Discord.Net.Core/Entities/Image.cs index 538d82730..e92342e7a 100644 --- a/src/Discord.Net.Core/Entities/Image.cs +++ b/src/Discord.Net.Core/Entities/Image.cs @@ -17,6 +17,7 @@ namespace Discord { Stream = stream; } +#if NETSTANDARD1_3 /// /// Create the image from a file path. /// @@ -28,5 +29,6 @@ namespace Discord { Stream = File.OpenRead(path); } +#endif } } diff --git a/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs index 4d6a0f63b..5140f4dae 100644 --- a/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs +++ b/src/Discord.Net.Core/Entities/Roles/ModifyGuildRolesParams.cs @@ -2,6 +2,9 @@ { public class ModifyGuildRolesParams : ModifyGuildRoleParams { + /// + /// The id of the role to be edited + /// public ulong Id { get; } public ModifyGuildRolesParams(ulong id) diff --git a/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs b/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs index 57909cd03..50f535a4c 100644 --- a/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs +++ b/src/Discord.Net.Core/Entities/Users/ModifyGuildMemberParams.cs @@ -1,4 +1,6 @@ -namespace Discord +using System.Collections.Generic; + +namespace Discord { /// /// Modify an IGuildUser with the following parameters. @@ -42,7 +44,15 @@ /// To add a role to a user: /// To remove a role from a user: /// - public Optional Roles { get; set; } + public Optional> Roles { get; set; } + /// + /// What roles should the user have? + /// + /// + /// To add a role to a user: + /// To remove a role from a user: + /// + public Optional> RoleIds { get; set; } /// /// Move a user to a voice channel. /// diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 428541509..5c6f1cb0e 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -27,16 +27,31 @@ namespace Discord.Rest AfkChannelId = args.AfkChannelId, AfkTimeout = args.AfkTimeout, DefaultMessageNotifications = args.DefaultMessageNotifications, + Icon = args.Icon.IsSpecified ? ImageModel.Create(args.Icon.Value) : Optional.Create(), Name = args.Name, - OwnerId = args.OwnerId, - RegionId = args.RegionId, + Splash = args.Splash.IsSpecified ? ImageModel.Create(args.Splash.Value) : Optional.Create(), Username = args.Username, VerificationLevel = args.VerificationLevel }; - if (apiArgs.Splash.IsSpecified && guild.SplashId != null) + if (args.AfkChannel.IsSpecified) + apiArgs.AfkChannelId = args.AfkChannel.Value.Id; + else if (args.AfkChannelId.IsSpecified) + apiArgs.AfkChannelId = args.AfkChannelId.Value; + + if (args.Owner.IsSpecified) + apiArgs.OwnerId = args.Owner.Value.Id; + else if (args.OwnerId.IsSpecified) + apiArgs.OwnerId = args.OwnerId.Value; + + if (args.Region.IsSpecified) + apiArgs.RegionId = args.Region.Value.Id; + else if (args.RegionId.IsSpecified) + apiArgs.RegionId = args.RegionId.Value; + + if (!apiArgs.Splash.IsSpecified && guild.SplashId != null) apiArgs.Splash = new ImageModel(guild.SplashId); - if (apiArgs.Icon.IsSpecified && guild.IconId != null) + if (!apiArgs.Icon.IsSpecified && guild.IconId != null) apiArgs.Icon = new ImageModel(guild.IconId); return await client.ApiClient.ModifyGuildAsync(guild.Id, apiArgs, options).ConfigureAwait(false); @@ -50,9 +65,14 @@ namespace Discord.Rest func(args); var apiArgs = new API.Rest.ModifyGuildEmbedParams { - ChannelId = args.ChannelId, Enabled = args.Enabled }; + + if (args.Channel.IsSpecified) + apiArgs.ChannelId = args.Channel.Value?.Id; + else if (args.ChannelId.IsSpecified) + apiArgs.ChannelId = args.ChannelId.Value; + return await client.ApiClient.ModifyGuildEmbedAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } public static async Task ModifyChannelsAsync(IGuild guild, BaseDiscordClient client, @@ -74,32 +94,32 @@ namespace Discord.Rest }); return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, apiArgs, options).ConfigureAwait(false); } - public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client, + public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { await client.ApiClient.LeaveGuildAsync(guild.Id, options).ConfigureAwait(false); } - public static async Task DeleteAsync(IGuild guild, BaseDiscordClient client, + public static async Task DeleteAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { await client.ApiClient.DeleteGuildAsync(guild.Id, options).ConfigureAwait(false); } //Bans - public static async Task> GetBansAsync(IGuild guild, BaseDiscordClient client, + public static async Task> GetBansAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { var models = await client.ApiClient.GetGuildBansAsync(guild.Id, options).ConfigureAwait(false); return models.Select(x => RestBan.Create(client, x)).ToImmutableArray(); } - - public static async Task AddBanAsync(IGuild guild, BaseDiscordClient client, + + public static async Task AddBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, int pruneDays, RequestOptions options) { var args = new CreateGuildBanParams { DeleteMessageDays = pruneDays }; await client.ApiClient.CreateGuildBanAsync(guild.Id, userId, args, options).ConfigureAwait(false); - } - public static async Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, + } + public static async Task RemoveBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, RequestOptions options) { await client.ApiClient.RemoveGuildBanAsync(guild.Id, userId, options).ConfigureAwait(false); @@ -114,7 +134,7 @@ namespace Discord.Rest return RestGuildChannel.Create(client, guild, model); return null; } - public static async Task> GetChannelsAsync(IGuild guild, BaseDiscordClient client, + public static async Task> GetChannelsAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id, options).ConfigureAwait(false); @@ -140,7 +160,7 @@ namespace Discord.Rest } //Integrations - public static async Task> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client, + public static async Task> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false); @@ -155,7 +175,7 @@ namespace Discord.Rest } //Invites - public static async Task> GetInvitesAsync(IGuild guild, BaseDiscordClient client, + public static async Task> GetInvitesAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id, options).ConfigureAwait(false); @@ -191,7 +211,7 @@ namespace Discord.Rest return RestGuildUser.Create(client, guild, model); return null; } - public static async Task GetCurrentUserAsync(IGuild guild, BaseDiscordClient client, + public static async Task GetCurrentUserAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { return await GetUserAsync(guild, client, client.CurrentUser.Id, options).ConfigureAwait(false); diff --git a/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs b/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs index 180ad38bc..40d57c511 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestGuildUser.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; +using System.Linq; using System.Threading.Tasks; using Model = Discord.API.GuildMember; @@ -30,7 +31,7 @@ namespace Discord.Rest } } public IReadOnlyCollection RoleIds => _roleIds; - + public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks); internal RestGuildUser(BaseDiscordClient discord, IGuild guild, ulong id) @@ -61,21 +62,25 @@ namespace Discord.Rest roles.Add(roleIds[i]); _roleIds = roles.ToImmutable(); } - + public override async Task UpdateAsync(RequestOptions options = null) { var model = await Discord.ApiClient.GetGuildMemberAsync(GuildId, Id, options).ConfigureAwait(false); Update(model); } public async Task ModifyAsync(Action func, RequestOptions options = null) - { + { var args = await UserHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false); if (args.Deaf.IsSpecified) IsDeafened = args.Deaf.Value; if (args.Mute.IsSpecified) IsMuted = args.Mute.Value; - if (args.RoleIds.IsSpecified) - UpdateRoles(args.RoleIds.Value); + if (args.Nickname.IsSpecified) + Nickname = args.Nickname.Value; + if (args.Roles.IsSpecified) + UpdateRoles(args.Roles.Value.Select(x => x.Id).ToArray()); + else if (args.RoleIds.IsSpecified) + UpdateRoles(args.RoleIds.Value.ToArray()); } public Task KickAsync(RequestOptions options = null) => UserHelper.KickAsync(this, Discord, options); diff --git a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs index dbfa2f2e5..e23c1cbb0 100644 --- a/src/Discord.Net.Rest/Entities/Users/UserHelper.cs +++ b/src/Discord.Net.Rest/Entities/Users/UserHelper.cs @@ -16,9 +16,13 @@ namespace Discord.Rest func(args); var apiArgs = new API.Rest.ModifyCurrentUserParams { - Avatar = args.Avatar.IsSpecified ? ImageModel.Create(args.Avatar.Value) : Optional.Create(), + Avatar = args.Avatar.IsSpecified ? ImageModel.Create(args.Avatar.Value) : Optional.Create(), Username = args.Username }; + + if (!apiArgs.Avatar.IsSpecified && user.AvatarId != null) + apiArgs.Avatar = new ImageModel(user.AvatarId); + return await client.ApiClient.ModifySelfAsync(apiArgs, options).ConfigureAwait(false); } public static async Task ModifyAsync(IGuildUser user, BaseDiscordClient client, Action func, @@ -31,9 +35,14 @@ namespace Discord.Rest ChannelId = args.Channel.IsSpecified ? args.Channel.Value.Id : Optional.Create(), Deaf = args.Deaf, Mute = args.Mute, - Nickname = args.Nickname, - RoleIds = args.Roles.IsSpecified ? args.Roles.Value.Select(r => r.Id).ToArray() : Optional.Create(), + Nickname = args.Nickname }; + + if (args.Roles.IsSpecified) + apiArgs.RoleIds = args.Roles.Value.Select(x => x.Id).ToArray(); + else if (args.RoleIds.IsSpecified) + apiArgs.RoleIds = args.RoleIds.Value.ToArray(); + await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, apiArgs, options).ConfigureAwait(false); return args; }