Merge branch 'issue/379' into dev

This commit is contained in:
RogueException
2016-12-16 11:12:50 -04:00
26 changed files with 583 additions and 57 deletions

View File

@@ -336,7 +336,7 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendAsync<Channel>("DELETE", () => $"channels/{channelId}", ids, options: options).ConfigureAwait(false);
}
public async Task<Channel> ModifyGuildChannelAsync(ulong channelId, ModifyGuildChannelParams args, RequestOptions options = null)
public async Task<Channel> ModifyGuildChannelAsync(ulong channelId, Rest.ModifyGuildChannelParams args, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotNull(args, nameof(args));
@@ -347,7 +347,7 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Channel>("PATCH", () => $"channels/{channelId}", args, ids, options: options).ConfigureAwait(false);
}
public async Task<Channel> ModifyGuildChannelAsync(ulong channelId, ModifyTextChannelParams args, RequestOptions options = null)
public async Task<Channel> ModifyGuildChannelAsync(ulong channelId, Rest.ModifyTextChannelParams args, RequestOptions options = null)
{
Preconditions.NotEqual(channelId, 0, nameof(channelId));
Preconditions.NotNull(args, nameof(args));
@@ -358,11 +358,11 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Channel>("PATCH", () => $"channels/{channelId}", args, ids, options: options).ConfigureAwait(false);
}
public async Task<Channel> ModifyGuildChannelAsync(ulong channelId, ModifyVoiceChannelParams args, RequestOptions options = null)
public async Task<Channel> ModifyGuildChannelAsync(ulong channelId, Rest.ModifyVoiceChannelParams args, RequestOptions options = null)
{
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));
@@ -371,7 +371,7 @@ namespace Discord.API
var ids = new BucketIds(channelId: channelId);
return await SendJsonAsync<Channel>("PATCH", () => $"channels/{channelId}", args, ids, options: options).ConfigureAwait(false);
}
public async Task ModifyGuildChannelsAsync(ulong guildId, IEnumerable<ModifyGuildChannelsParams> args, RequestOptions options = null)
public async Task ModifyGuildChannelsAsync(ulong guildId, IEnumerable<Rest.ModifyGuildChannelsParams> args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotNull(args, nameof(args));
@@ -383,7 +383,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);
@@ -700,7 +700,7 @@ namespace Discord.API
var ids = new BucketIds(guildId: guildId);
return await SendAsync<Guild>("DELETE", () => $"users/@me/guilds/{guildId}", ids, options: options).ConfigureAwait(false);
}
public async Task<Guild> ModifyGuildAsync(ulong guildId, ModifyGuildParams args, RequestOptions options = null)
public async Task<Guild> ModifyGuildAsync(ulong guildId, Rest.ModifyGuildParams args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotNull(args, nameof(args));
@@ -778,7 +778,7 @@ namespace Discord.API
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { return null; }
}
public async Task<GuildEmbed> ModifyGuildEmbedAsync(ulong guildId, ModifyGuildEmbedParams args, RequestOptions options = null)
public async Task<GuildEmbed> ModifyGuildEmbedAsync(ulong guildId, Rest.ModifyGuildEmbedParams args, RequestOptions options = null)
{
Preconditions.NotNull(args, nameof(args));
Preconditions.NotEqual(guildId, 0, nameof(guildId));
@@ -816,7 +816,7 @@ namespace Discord.API
var ids = new BucketIds(guildId: guildId);
return await SendAsync<Integration>("DELETE", () => $"guilds/{guildId}/integrations/{integrationId}", ids, options: options).ConfigureAwait(false);
}
public async Task<Integration> ModifyGuildIntegrationAsync(ulong guildId, ulong integrationId, ModifyGuildIntegrationParams args, RequestOptions options = null)
public async Task<Integration> ModifyGuildIntegrationAsync(ulong guildId, ulong integrationId, Rest.ModifyGuildIntegrationParams args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotEqual(integrationId, 0, nameof(integrationId));
@@ -939,7 +939,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));
@@ -950,7 +950,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<string>(); //Remove
}
@@ -987,7 +987,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<Role> ModifyGuildRoleAsync(ulong guildId, ulong roleId, ModifyGuildRoleParams args, RequestOptions options = null)
public async Task<Role> ModifyGuildRoleAsync(ulong guildId, ulong roleId, Rest.ModifyGuildRoleParams args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotEqual(roleId, 0, nameof(roleId));
@@ -1000,7 +1000,7 @@ namespace Discord.API
var ids = new BucketIds(guildId: guildId);
return await SendJsonAsync<Role>("PATCH", () => $"guilds/{guildId}/roles/{roleId}", args, ids, options: options).ConfigureAwait(false);
}
public async Task<IReadOnlyCollection<Role>> ModifyGuildRolesAsync(ulong guildId, IEnumerable<ModifyGuildRolesParams> args, RequestOptions options = null)
public async Task<IReadOnlyCollection<Role>> ModifyGuildRolesAsync(ulong guildId, IEnumerable<Rest.ModifyGuildRolesParams> args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotNull(args, nameof(args));
@@ -1058,7 +1058,7 @@ namespace Discord.API
options = RequestOptions.CreateOrClone(options);
return await SendAsync<Application>("GET", () => "oauth2/applications/@me", new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task<User> ModifySelfAsync(ModifyCurrentUserParams args, RequestOptions options = null)
public async Task<User> ModifySelfAsync(Rest.ModifyCurrentUserParams args, RequestOptions options = null)
{
Preconditions.NotNull(args, nameof(args));
Preconditions.NotNullOrEmpty(args.Username, nameof(args.Username));
@@ -1066,7 +1066,7 @@ namespace Discord.API
return await SendJsonAsync<User>("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));

View File

@@ -17,5 +17,17 @@ namespace Discord.API
Stream = null;
Hash = hash;
}
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;
}
}
}

View File

@@ -9,6 +9,6 @@ namespace Discord.API.Rest
[JsonProperty("username")]
public Optional<string> Username { get; set; }
[JsonProperty("avatar")]
public Optional<Image> Avatar { get; set; }
public Optional<Image?> Avatar { get; set; }
}
}

View File

@@ -0,0 +1,30 @@
namespace Discord
{
/// <summary>
/// Modify an IGuildChannel with the specified changes.
/// </summary>
/// <example>
/// <code language="c#">
/// await (Context.Channel as ITextChannel)?.ModifyAsync(x =>
/// {
/// x.Name = "do-not-enter";
/// });
/// </code>
/// </example>
public class ModifyGuildChannelParams
{
/// <summary>
/// Set the channel to this name
/// </summary>
/// <remarks>
/// When modifying an ITextChannel, the Name MUST be alphanumeric with dashes.
/// It must match the following RegEx: [a-z0-9-_]{2,100}
/// </remarks>
/// <exception cref="Net.HttpException">A BadRequest will be thrown if the name does not match the above RegEx.</exception>
public Optional<string> Name { get; set; }
/// <summary>
/// Move the channel to the following position. This is 0-based!
/// </summary>
public Optional<int> Position { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
namespace Discord
{
public class ModifyGuildChannelsParams
{
/// <summary>
/// The id of the channel to apply this position to.
/// </summary>
public ulong Id { get; set; }
/// <summary>
/// The new zero-based position of this channel.
/// </summary>
public int Position { get; set; }
public ModifyGuildChannelsParams(ulong id, int position)
{
Id = id;
Position = position;
}
}
}

View File

@@ -0,0 +1,11 @@
namespace Discord
{
/// <inheritdoc />
public class ModifyTextChannelParams : ModifyGuildChannelParams
{
/// <summary>
/// What the topic of the channel should be set to.
/// </summary>
public Optional<string> Topic { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
namespace Discord
{
/// <inheritdoc />
public class ModifyVoiceChannelParams : ModifyGuildChannelParams
{
/// <summary>
/// The bitrate of the voice connections in this channel. Must be greater than 8000
/// </summary>
public Optional<int> Bitrate { get; set; }
/// <summary>
/// The maximum number of users that can be present in a channel.
/// </summary>
public Optional<int> UserLimit { get; set; }
}
}

View File

@@ -0,0 +1,21 @@
namespace Discord
{
/// <summary>
/// Modify the widget of an IGuild with the specified parameters
/// </summary>
public class ModifyGuildEmbedParams
{
/// <summary>
/// Should the widget be enabled?
/// </summary>
public Optional<bool> Enabled { get; set; }
/// <summary>
/// What channel should the invite place users in, if not null.
/// </summary>
public Optional<IChannel> Channel { get; set; }
/// <summary>
/// What channel should the invite place users in, if not null.
/// </summary>
public Optional<ulong?> ChannelId { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace Discord
{
public class ModifyGuildIntegrationParams
{
public Optional<int> ExpireBehavior { get; set; }
public Optional<int> ExpireGracePeriod { get; set; }
public Optional<bool> EnableEmoticons { get; set; }
}
}

View File

@@ -0,0 +1,71 @@
namespace Discord
{
/// <summary>
/// Modify an IGuild with the specified changes
/// </summary>
/// <example>
/// <code language="c#">
/// await Context.Guild.ModifyAsync(async x =>
/// {
/// x.Name = "aaaaaah";
/// x.RegionId = (await Context.Client.GetOptimalVoiceRegionAsync()).Id;
/// });
/// </code>
/// </example>
/// <see cref="IGuild"/>
public class ModifyGuildParams
{
public Optional<string> Username { get; set; }
/// <summary>
/// The name of the Guild
/// </summary>
public Optional<string> Name { get; set; }
/// <summary>
/// The region for the Guild's voice connections
/// </summary>
public Optional<IVoiceRegion> Region { get; set; }
/// <summary>
/// The ID of the region for the Guild's voice connections
/// </summary>
public Optional<string> RegionId { get; set; }
/// <summary>
/// What verification level new users need to achieve before speaking
/// </summary>
public Optional<VerificationLevel> VerificationLevel { get; set; }
/// <summary>
/// The default message notification state for the guild
/// </summary>
public Optional<DefaultMessageNotifications> DefaultMessageNotifications { get; set; }
/// <summary>
/// How many seconds before a user is sent to AFK. This value MUST be one of: (60, 300, 900, 1800, 3600).
/// </summary>
public Optional<int> AfkTimeout { get; set; }
/// <summary>
/// The icon of the guild
/// </summary>
public Optional<Image?> Icon { get; set; }
/// <summary>
/// The guild's splash image
/// </summary>
/// <remarks>
/// The guild must be partnered for this value to have any effect.
/// </remarks>
public Optional<Image?> Splash { get; set; }
/// <summary>
/// The IVoiceChannel where AFK users should be sent.
/// </summary>
public Optional<IVoiceChannel> AfkChannel { get; set; }
/// <summary>
/// The ID of the IVoiceChannel where AFK users should be sent.
/// </summary>
public Optional<ulong?> AfkChannelId { get; set; }
/// <summary>
/// The owner of this guild.
/// </summary>
public Optional<IUser> Owner { get; set; }
/// <summary>
/// The ID of the owner of this guild.
/// </summary>
public Optional<ulong> OwnerId { get; set; }
}
}

View File

@@ -0,0 +1,34 @@
using System.IO;
namespace Discord
{
/// <summary>
/// An image that will be uploaded to Discord.
/// </summary>
public struct Image
{
public Stream Stream { get; }
/// <summary>
/// Create the image with a Stream.
/// </summary>
/// <param name="stream">This must be some type of stream with the contents of a file in it.</param>
/// <seealso cref="File.OpenRead(string)"/>
public Image(Stream stream)
{
Stream = stream;
}
#if NETSTANDARD1_3
/// <summary>
/// Create the image from a file path.
/// </summary>
/// <remarks>
/// This file path is NOT validated, and is passed directly into a <see cref="File.OpenRead(string)"/>
/// </remarks>
/// <param name="path">The path to the file.</param>
public Image(string path)
{
Stream = File.OpenRead(path);
}
#endif
}
}

View File

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

View File

@@ -1,12 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Discord
namespace Discord
{
/// <summary>
/// Modify a message with the specified parameters.
/// </summary>
/// <remarks>
/// The content of a message can be cleared with String.Empty; if and only if an Embed is present.
/// </remarks>
/// <example>
/// <code language="c#">
/// 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.");
/// });
/// </code>
/// </example>
public class ModifyMessageParams
{
/// <summary>
/// The content of the message
/// </summary>
/// <remarks>
/// This must be less than 2000 characters.
/// </remarks>
public Optional<string> Content { get; set; }
/// <summary>
/// The embed the message should display
/// </summary>
public Optional<EmbedBuilder> Embed { get; set; }
}
}

View File

@@ -0,0 +1,51 @@
namespace Discord
{
/// <summary>
/// Modify an IRole with the specified parameters
/// </summary>
/// <example>
/// <code language="c#">
/// await role.ModifyAsync(x =>
/// {
/// x.Color = new Color(180, 15, 40);
/// x.Hoist = true;
/// });
/// </code>
/// </example>
/// <seealso cref="IRole"/>
public class ModifyGuildRoleParams
{
/// <summary>
/// The name of the role
/// </summary>
/// <remarks>
/// If this role is the EveryoneRole, this value may not be set.
/// </remarks>
public Optional<string> Name { get; set; }
/// <summary>
/// The role's GuildPermissions
/// </summary>
public Optional<GuildPermissions> Permissions { get; set; }
/// <summary>
/// The position of the role. This is 0-based!
/// </summary>
/// <remarks>
/// If this role is the EveryoneRole, this value may not be set.
/// </remarks>
public Optional<int> Position { get; set; }
/// <summary>
/// The color of the Role.
/// </summary>
/// <remarks>
/// If this role is the EveryoneRole, this value may not be set.
/// </remarks>
public Optional<Color> Color { get; set; }
/// <summary>
/// Whether or not this role should be displayed independently in the userlist.
/// </summary>
/// <remarks>
/// If this role is the EveryoneRole, this value may not be set.
/// </remarks>
public Optional<bool> Hoist { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
namespace Discord
{
public class ModifyGuildRolesParams : ModifyGuildRoleParams
{
/// <summary>
/// The id of the role to be edited
/// </summary>
public ulong Id { get; }
public ModifyGuildRolesParams(ulong id)
{
Id = id;
}
}
}

View File

@@ -0,0 +1,12 @@
namespace Discord
{
public class ModifyCurrentUserNickParams
{
public string Nickname { get; }
public ModifyCurrentUserNickParams(string nickname)
{
Nickname = nickname;
}
}
}

View File

@@ -0,0 +1,26 @@
namespace Discord
{
/// <summary>
/// Modify the current user with the specified arguments
/// </summary>
/// <example>
/// <code language="c#">
/// await Context.Client.CurrentUser.ModifyAsync(x =>
/// {
/// x.Avatar = new Image(File.OpenRead("avatar.jpg"));
/// });
/// </code>
/// </example>
/// <seealso cref="ISelfUser"/>
public class ModifyCurrentUserParams
{
/// <summary>
/// Your username
/// </summary>
public Optional<string> Username { get; set; }
/// <summary>
/// Your avatar
/// </summary>
public Optional<Image> Avatar { get; set; }
}
}

View File

@@ -0,0 +1,64 @@
using System.Collections.Generic;
namespace Discord
{
/// <summary>
/// Modify an IGuildUser with the following parameters.
/// </summary>
/// <example>
/// <code language="c#">
/// await (Context.User as IGuildUser)?.ModifyAsync(x =>
/// {
/// x.Nickname = $"festive {Context.User.Username}";
/// });
/// </code>
/// </example>
/// <seealso cref="IGuildUser"/>
public class ModifyGuildMemberParams
{
/// <summary>
/// Should the user be guild-muted in a voice channel?
/// </summary>
/// <remarks>
/// If this value is set to true, no user will be able to hear this user speak in the guild.
/// </remarks>
public Optional<bool> Mute { get; set; }
/// <summary>
/// Should the user be guild-deafened in a voice channel?
/// </summary>
/// <remarks>
/// If this value is set to true, this user will not be able to hear anyone speak in the guild.
/// </remarks>
public Optional<bool> Deaf { get; set; }
/// <summary>
/// Should the user have a nickname set?
/// </summary>
/// <remarks>
/// To clear the user's nickname, this value can be set to null.
/// </remarks>
public Optional<string> Nickname { get; set; }
/// <summary>
/// What roles should the user have?
/// </summary>
/// <remarks>
/// To add a role to a user: <see cref="GuildUserExtensions.AddRolesAsync(IGuildUser, IRole[])"/>
/// To remove a role from a user: <see cref="GuildUserExtensions.RemoveRolesAsync(IGuildUser, IRole[])"/>
/// </remarks>
public Optional<IEnumerable<IRole>> Roles { get; set; }
/// <summary>
/// What roles should the user have?
/// </summary>
/// <remarks>
/// To add a role to a user: <see cref="GuildUserExtensions.AddRolesAsync(IGuildUser, IRole[])"/>
/// To remove a role from a user: <see cref="GuildUserExtensions.RemoveRolesAsync(IGuildUser, IRole[])"/>
/// </remarks>
public Optional<IEnumerable<ulong>> RoleIds { get; set; }
/// <summary>
/// Move a user to a voice channel.
/// </summary>
/// <remarks>
/// This user MUST already be in a Voice Channel for this to work.
/// </remarks>
public Optional<IVoiceChannel> Channel { get; set; }
}
}

View File

@@ -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)
{

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
namespace Discord
{