Skip to content

Commit

Permalink
Add non-capturing overloads of Modify methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe4evr committed Oct 8, 2019
1 parent 0bda8a4 commit 391823b
Show file tree
Hide file tree
Showing 39 changed files with 286 additions and 68 deletions.
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Entities/Channels/IGuildChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public interface IGuildChannel : IChannel, IDeletable
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this guild channel.
/// </summary>
/// <remarks>
/// This method modifies the current guild channel with the specified properties. To see an example of this
/// method and what properties are available, please refer to <see cref="GuildChannelProperties"/>.
/// </remarks>
/// <param name="func">The delegate containing the properties to modify the channel with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync<TState>(Action<GuildChannelProperties, TState> func, TState state, RequestOptions options = null);

/// <summary>
/// Gets the permission overwrite for a specific role.
Expand Down
13 changes: 12 additions & 1 deletion src/Discord.Net.Core/Entities/Channels/ITextChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ public interface ITextChannel : IMessageChannel, IMentionable, INestedChannel
/// </returns>
/// <seealso cref="TextChannelProperties"/>
Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null);

/// <summary>
/// Modifies this text channel.
/// </summary>
/// <param name="func">The delegate containing the properties to modify the channel with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
/// <seealso cref="TextChannelProperties"/>
Task ModifyAsync<TState>(Action<TextChannelProperties, TState> func, TState state, RequestOptions options = null);

/// <summary>
/// Creates a webhook in this text channel.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Discord.Net.Core/Entities/Channels/IVoiceChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,16 @@ public interface IVoiceChannel : INestedChannel, IAudioChannel
/// </returns>
/// <seealso cref="VoiceChannelProperties"/>
Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this voice channel.
/// </summary>
/// <param name="func">The properties to modify the channel with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
/// <seealso cref="VoiceChannelProperties"/>
Task ModifyAsync<TState>(Action<VoiceChannelProperties, TState> func, TState state, RequestOptions options = null);
}
}
20 changes: 20 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// </returns>
Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this guild.
/// </summary>
/// <param name="func">The delegate containing the properties to modify the guild with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync<TState>(Action<GuildProperties, TState> func, TState state, RequestOptions options = null);
/// <summary>
/// Modifies this guild's embed channel.
/// </summary>
/// <param name="func">The delegate containing the properties to modify the guild widget with.</param>
Expand All @@ -268,6 +278,16 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// </returns>
Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this guild's embed channel.
/// </summary>
/// <param name="func">The delegate containing the properties to modify the guild widget with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyEmbedAsync<TState>(Action<GuildEmbedProperties, TState> func, TState state, RequestOptions options = null);
/// <summary>
/// Bulk-modifies the order of channels in this guild.
/// </summary>
/// <param name="args">The properties used to modify the channel positions with.</param>
Expand Down
20 changes: 20 additions & 0 deletions src/Discord.Net.Core/Entities/Messages/IUserMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public interface IUserMessage : IMessage
/// </returns>
Task ModifyAsync(Action<MessageProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this message.
/// </summary>
/// <remarks>
/// This method modifies this message with the specified properties. To see an example of this
/// method and what properties are available, please refer to <see cref="MessageProperties"/>.
/// </remarks>
/// <example>
/// The following example replaces the content of the message with <c>Hello World!</c>.
/// <code language="cs">
/// await msg.ModifyAsync((x, s) =&gt; x.Content = s, "Hello World!");
/// </code>
/// </example>
/// <param name="func">A delegate containing the properties to modify the message with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync<TState>(Action<MessageProperties, TState> func, TState state, RequestOptions options = null);
/// <summary>
/// Modifies the suppression of this message.
/// </summary>
/// <remarks>
Expand Down
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Entities/Roles/IRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,19 @@ public interface IRole : ISnowflakeEntity, IDeletable, IMentionable, IComparable
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync(Action<RoleProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this role.
/// </summary>
/// <remarks>
/// This method modifies this role with the specified properties. To see an example of this
/// method and what properties are available, please refer to <see cref="RoleProperties"/>.
/// </remarks>
/// <param name="func">A delegate containing the properties to modify the role with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync<TState>(Action<RoleProperties, TState> func, TState state, RequestOptions options = null);
}
}
14 changes: 14 additions & 0 deletions src/Discord.Net.Core/Entities/Users/IGuildUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public interface IGuildUser : IUser, IVoiceState
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this user's properties in this guild.
/// </summary>
/// <remarks>
/// This method modifies the current guild user with the specified properties. To see an example of this
/// method and what properties are available, please refer to <see cref="GuildUserProperties"/>.
/// </remarks>
/// <param name="func">The delegate containing the properties to modify the user with.</param>
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous modification operation.
/// </returns>
Task ModifyAsync<TState>(Action<GuildUserProperties, TState> func, TState state, RequestOptions options = null);

/// <summary>
/// Adds the specified role to this user in the guild.
Expand Down
4 changes: 4 additions & 0 deletions src/Discord.Net.Core/Entities/Users/ISelfUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ public interface ISelfUser : IUser
/// Modifies the user's properties.
/// </summary>
Task ModifyAsync(Action<SelfUserProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies the user's properties.
/// </summary>
Task ModifyAsync<TState>(Action<SelfUserProperties, TState> func, TState state, RequestOptions options = null);
}
}
6 changes: 5 additions & 1 deletion src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;

namespace Discord
Expand Down Expand Up @@ -53,5 +53,9 @@ public interface IWebhook : IDeletable, ISnowflakeEntity
/// Modifies this webhook.
/// </summary>
Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null);
/// <summary>
/// Modifies this webhook.
/// </summary>
Task ModifyAsync<TState>(Action<WebhookProperties, TState> func, TState state, RequestOptions options = null);
}
}
21 changes: 12 additions & 9 deletions src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ public static async Task DeleteAsync(IChannel channel, BaseDiscordClient client,
{
await client.ApiClient.DeleteChannelAsync(channel.Id, options).ConfigureAwait(false);
}
public static async Task<Model> ModifyAsync(IGuildChannel channel, BaseDiscordClient client,
Action<GuildChannelProperties> func,
public static async Task<Model> ModifyAsync<TState>(IGuildChannel channel, BaseDiscordClient client,
Action<GuildChannelProperties, TState> func,
TState state,
RequestOptions options)
{
var args = new GuildChannelProperties();
func(args);
func(args, state);
var apiArgs = new API.Rest.ModifyGuildChannelParams
{
Name = args.Name,
Expand All @@ -32,12 +33,13 @@ public static async Task<Model> ModifyAsync(IGuildChannel channel, BaseDiscordCl
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
}
public static async Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordClient client,
Action<TextChannelProperties> func,
public static async Task<Model> ModifyAsync<TState>(ITextChannel channel, BaseDiscordClient client,
Action<TextChannelProperties, TState> func,
TState state,
RequestOptions options)
{
var args = new TextChannelProperties();
func(args);
func(args, state);
var apiArgs = new API.Rest.ModifyTextChannelParams
{
Name = args.Name,
Expand All @@ -49,12 +51,13 @@ public static async Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordCli
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
}
public static async Task<Model> ModifyAsync(IVoiceChannel channel, BaseDiscordClient client,
Action<VoiceChannelProperties> func,
public static async Task<Model> ModifyAsync<TState>(IVoiceChannel channel, BaseDiscordClient client,
Action<VoiceChannelProperties, TState> func,
TState state,
RequestOptions options)
{
var args = new VoiceChannelProperties();
func(args);
func(args, state);
var apiArgs = new API.Rest.ModifyVoiceChannelParams
{
Bitrate = args.Bitrate,
Expand Down
7 changes: 5 additions & 2 deletions src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ public override async Task UpdateAsync(RequestOptions options = null)
Update(model);
}
/// <inheritdoc />
public async Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null)
public Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null)
=> ModifyAsync((props, f) => f(props), func, options);
/// <inheritdoc />
public async Task ModifyAsync<TState>(Action<GuildChannelProperties, TState> func, TState state, RequestOptions options = null)
{
var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
var model = await ChannelHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
Update(model);
}
/// <inheritdoc />
Expand Down
7 changes: 5 additions & 2 deletions src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ internal override void Update(Model model)
}

/// <inheritdoc />
public async Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
public Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
=> ModifyAsync((props, f) => f(props), func, options);
/// <inheritdoc />
public async Task ModifyAsync<TState>(Action<TextChannelProperties, TState> func, TState state, RequestOptions options = null)
{
var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
var model = await ChannelHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
Update(model);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ internal override void Update(Model model)
}

/// <inheritdoc />
public async Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
=> ModifyAsync((props, f) => f(props), func, options);
/// <inheritdoc />
public async Task ModifyAsync<TState>(Action<VoiceChannelProperties, TState> func, TState state, RequestOptions options = null)
{
var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
var model = await ChannelHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
Update(model);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ internal static class GuildHelper
{
//General
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public static async Task<Model> ModifyAsync(IGuild guild, BaseDiscordClient client,
Action<GuildProperties> func, RequestOptions options)
public static async Task<Model> ModifyAsync<TState>(IGuild guild, BaseDiscordClient client,
Action<GuildProperties, TState> func, TState state, RequestOptions options)
{
if (func == null) throw new ArgumentNullException(nameof(func));

var args = new GuildProperties();
func(args);
func(args, state);

var apiArgs = new API.Rest.ModifyGuildParams
{
Expand Down Expand Up @@ -71,13 +71,13 @@ public static async Task<Model> ModifyAsync(IGuild guild, BaseDiscordClient clie
return await client.ApiClient.ModifyGuildAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
}
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public static async Task<EmbedModel> ModifyEmbedAsync(IGuild guild, BaseDiscordClient client,
Action<GuildEmbedProperties> func, RequestOptions options)
public static async Task<EmbedModel> ModifyEmbedAsync<TState>(IGuild guild, BaseDiscordClient client,
Action<GuildEmbedProperties, TState> func, TState state, RequestOptions options)
{
if (func == null) throw new ArgumentNullException(nameof(func));

var args = new GuildEmbedProperties();
func(args);
func(args, state);
var apiArgs = new API.Rest.ModifyGuildEmbedParams
{
Enabled = args.Enabled
Expand Down
16 changes: 12 additions & 4 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,25 @@ public Task DeleteAsync(RequestOptions options = null)

/// <inheritdoc />
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public async Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
public Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
=> ModifyAsync((props, f) => f(props), func, options);
/// <inheritdoc />
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public async Task ModifyAsync<TState>(Action<GuildProperties, TState> func, TState state, RequestOptions options = null)
{
var model = await GuildHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
var model = await GuildHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
Update(model);
}

/// <inheritdoc />
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public async Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
public Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
=> ModifyEmbedAsync((props, f) => f(props), func, options);
/// <inheritdoc />
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public async Task ModifyEmbedAsync<TState>(Action<GuildEmbedProperties, TState> func, TState state, RequestOptions options = null)
{
var model = await GuildHelper.ModifyEmbedAsync(this, Discord, func, options).ConfigureAwait(false);
var model = await GuildHelper.ModifyEmbedAsync(this, Discord, func, state, options).ConfigureAwait(false);
Update(model);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ internal static class MessageHelper
{
/// <exception cref="InvalidOperationException">Only the author of a message may modify the message.</exception>
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
public static async Task<Model> ModifyAsync(IMessage msg, BaseDiscordClient client, Action<MessageProperties> func,
public static async Task<Model> ModifyAsync<TState>(IMessage msg, BaseDiscordClient client,
Action<MessageProperties, TState> func,
TState state,
RequestOptions options)
{
if (msg.Author.Id != client.CurrentUser.Id)
throw new InvalidOperationException("Only the author of a message may modify the message.");

var args = new MessageProperties();
func(args);
func(args, state);

var apiArgs = new API.Rest.ModifyMessageParams
{
Expand Down
Loading

0 comments on commit 391823b

Please sign in to comment.