Skip to content

Commit 301efd7

Browse files
committed
Add non-capturing overloads of Modify methods
1 parent 0bda8a4 commit 301efd7

39 files changed

+286
-68
lines changed

src/Discord.Net.Core/Entities/Channels/IGuildChannel.cs

+14
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public interface IGuildChannel : IChannel, IDeletable
5757
/// A task that represents the asynchronous modification operation.
5858
/// </returns>
5959
Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null);
60+
/// <summary>
61+
/// Modifies this guild channel.
62+
/// </summary>
63+
/// <remarks>
64+
/// This method modifies the current guild channel with the specified properties. To see an example of this
65+
/// method and what properties are available, please refer to <see cref="GuildChannelProperties"/>.
66+
/// </remarks>
67+
/// <param name="func">The delegate containing the properties to modify the channel with.</param>
68+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
69+
/// <param name="options">The options to be used when sending the request.</param>
70+
/// <returns>
71+
/// A task that represents the asynchronous modification operation.
72+
/// </returns>
73+
Task ModifyAsync<TState>(Action<GuildChannelProperties, TState> func, TState state, RequestOptions options = null);
6074

6175
/// <summary>
6276
/// Gets the permission overwrite for a specific role.

src/Discord.Net.Core/Entities/Channels/ITextChannel.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,18 @@ public interface ITextChannel : IMessageChannel, IMentionable, INestedChannel
8383
/// </returns>
8484
/// <seealso cref="TextChannelProperties"/>
8585
Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null);
86-
86+
/// <summary>
87+
/// Modifies this text channel.
88+
/// </summary>
89+
/// <param name="func">The delegate containing the properties to modify the channel with.</param>
90+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
91+
/// <param name="options">The options to be used when sending the request.</param>
92+
/// <returns>
93+
/// A task that represents the asynchronous modification operation.
94+
/// </returns>
95+
/// <seealso cref="TextChannelProperties"/>
96+
Task ModifyAsync<TState>(Action<TextChannelProperties, TState> func, TState state, RequestOptions options = null);
97+
8798
/// <summary>
8899
/// Creates a webhook in this text channel.
89100
/// </summary>

src/Discord.Net.Core/Entities/Channels/IVoiceChannel.cs

+11
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,16 @@ public interface IVoiceChannel : INestedChannel, IAudioChannel
3535
/// </returns>
3636
/// <seealso cref="VoiceChannelProperties"/>
3737
Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null);
38+
/// <summary>
39+
/// Modifies this voice channel.
40+
/// </summary>
41+
/// <param name="func">The properties to modify the channel with.</param>
42+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
43+
/// <param name="options">The options to be used when sending the request.</param>
44+
/// <returns>
45+
/// A task that represents the asynchronous modification operation.
46+
/// </returns>
47+
/// <seealso cref="VoiceChannelProperties"/>
48+
Task ModifyAsync<TState>(Action<VoiceChannelProperties, TState> func, TState state, RequestOptions options = null);
3849
}
3950
}

src/Discord.Net.Core/Entities/Guilds/IGuild.cs

+20
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ public interface IGuild : IDeletable, ISnowflakeEntity
259259
/// </returns>
260260
Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null);
261261
/// <summary>
262+
/// Modifies this guild.
263+
/// </summary>
264+
/// <param name="func">The delegate containing the properties to modify the guild with.</param>
265+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
266+
/// <param name="options">The options to be used when sending the request.</param>
267+
/// <returns>
268+
/// A task that represents the asynchronous modification operation.
269+
/// </returns>
270+
Task ModifyAsync<TState>(Action<GuildProperties, TState> func, TState state, RequestOptions options = null);
271+
/// <summary>
262272
/// Modifies this guild's embed channel.
263273
/// </summary>
264274
/// <param name="func">The delegate containing the properties to modify the guild widget with.</param>
@@ -268,6 +278,16 @@ public interface IGuild : IDeletable, ISnowflakeEntity
268278
/// </returns>
269279
Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null);
270280
/// <summary>
281+
/// Modifies this guild's embed channel.
282+
/// </summary>
283+
/// <param name="func">The delegate containing the properties to modify the guild widget with.</param>
284+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
285+
/// <param name="options">The options to be used when sending the request.</param>
286+
/// <returns>
287+
/// A task that represents the asynchronous modification operation.
288+
/// </returns>
289+
Task ModifyEmbedAsync<TState>(Action<GuildEmbedProperties, TState> func, TState state, RequestOptions options = null);
290+
/// <summary>
271291
/// Bulk-modifies the order of channels in this guild.
272292
/// </summary>
273293
/// <param name="args">The properties used to modify the channel positions with.</param>

src/Discord.Net.Core/Entities/Messages/IUserMessage.cs

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ public interface IUserMessage : IMessage
2929
/// </returns>
3030
Task ModifyAsync(Action<MessageProperties> func, RequestOptions options = null);
3131
/// <summary>
32+
/// Modifies this message.
33+
/// </summary>
34+
/// <remarks>
35+
/// This method modifies this message with the specified properties. To see an example of this
36+
/// method and what properties are available, please refer to <see cref="MessageProperties"/>.
37+
/// </remarks>
38+
/// <example>
39+
/// The following example replaces the content of the message with <c>Hello World!</c>.
40+
/// <code language="cs">
41+
/// await msg.ModifyAsync((x, s) =&gt; x.Content = s, "Hello World!");
42+
/// </code>
43+
/// </example>
44+
/// <param name="func">A delegate containing the properties to modify the message with.</param>
45+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
46+
/// <param name="options">The options to be used when sending the request.</param>
47+
/// <returns>
48+
/// A task that represents the asynchronous modification operation.
49+
/// </returns>
50+
Task ModifyAsync<TState>(Action<MessageProperties, TState> func, TState state, RequestOptions options = null);
51+
/// <summary>
3252
/// Modifies the suppression of this message.
3353
/// </summary>
3454
/// <remarks>

src/Discord.Net.Core/Entities/Roles/IRole.cs

+14
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,19 @@ public interface IRole : ISnowflakeEntity, IDeletable, IMentionable, IComparable
7979
/// A task that represents the asynchronous modification operation.
8080
/// </returns>
8181
Task ModifyAsync(Action<RoleProperties> func, RequestOptions options = null);
82+
/// <summary>
83+
/// Modifies this role.
84+
/// </summary>
85+
/// <remarks>
86+
/// This method modifies this role with the specified properties. To see an example of this
87+
/// method and what properties are available, please refer to <see cref="RoleProperties"/>.
88+
/// </remarks>
89+
/// <param name="func">A delegate containing the properties to modify the role with.</param>
90+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
91+
/// <param name="options">The options to be used when sending the request.</param>
92+
/// <returns>
93+
/// A task that represents the asynchronous modification operation.
94+
/// </returns>
95+
Task ModifyAsync<TState>(Action<RoleProperties, TState> func, TState state, RequestOptions options = null);
8296
}
8397
}

src/Discord.Net.Core/Entities/Users/IGuildUser.cs

+14
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ public interface IGuildUser : IUser, IVoiceState
108108
/// A task that represents the asynchronous modification operation.
109109
/// </returns>
110110
Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null);
111+
/// <summary>
112+
/// Modifies this user's properties in this guild.
113+
/// </summary>
114+
/// <remarks>
115+
/// This method modifies the current guild user with the specified properties. To see an example of this
116+
/// method and what properties are available, please refer to <see cref="GuildUserProperties"/>.
117+
/// </remarks>
118+
/// <param name="func">The delegate containing the properties to modify the user with.</param>
119+
/// <param name="state">An object to carry state into the delegate to prevent closures.</param>
120+
/// <param name="options">The options to be used when sending the request.</param>
121+
/// <returns>
122+
/// A task that represents the asynchronous modification operation.
123+
/// </returns>
124+
Task ModifyAsync<TState>(Action<GuildUserProperties, TState> func, TState state, RequestOptions options = null);
111125

112126
/// <summary>
113127
/// Adds the specified role to this user in the guild.

src/Discord.Net.Core/Entities/Users/ISelfUser.cs

+4
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,9 @@ public interface ISelfUser : IUser
5959
/// Modifies the user's properties.
6060
/// </summary>
6161
Task ModifyAsync(Action<SelfUserProperties> func, RequestOptions options = null);
62+
/// <summary>
63+
/// Modifies the user's properties.
64+
/// </summary>
65+
Task ModifyAsync<TState>(Action<SelfUserProperties, TState> func, TState state, RequestOptions options = null);
6266
}
6367
}

src/Discord.Net.Core/Entities/Webhooks/IWebhook.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33

44
namespace Discord
@@ -53,5 +53,9 @@ public interface IWebhook : IDeletable, ISnowflakeEntity
5353
/// Modifies this webhook.
5454
/// </summary>
5555
Task ModifyAsync(Action<WebhookProperties> func, RequestOptions options = null);
56+
/// <summary>
57+
/// Modifies this webhook.
58+
/// </summary>
59+
Task ModifyAsync<TState>(Action<WebhookProperties, TState> func, TState state, RequestOptions options = null);
5660
}
5761
}

src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ public static async Task DeleteAsync(IChannel channel, BaseDiscordClient client,
1818
{
1919
await client.ApiClient.DeleteChannelAsync(channel.Id, options).ConfigureAwait(false);
2020
}
21-
public static async Task<Model> ModifyAsync(IGuildChannel channel, BaseDiscordClient client,
22-
Action<GuildChannelProperties> func,
21+
public static async Task<Model> ModifyAsync<TState>(IGuildChannel channel, BaseDiscordClient client,
22+
Action<GuildChannelProperties, TState> func,
23+
TState state,
2324
RequestOptions options)
2425
{
2526
var args = new GuildChannelProperties();
26-
func(args);
27+
func(args, state);
2728
var apiArgs = new API.Rest.ModifyGuildChannelParams
2829
{
2930
Name = args.Name,
@@ -32,12 +33,13 @@ public static async Task<Model> ModifyAsync(IGuildChannel channel, BaseDiscordCl
3233
};
3334
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
3435
}
35-
public static async Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordClient client,
36-
Action<TextChannelProperties> func,
36+
public static async Task<Model> ModifyAsync<TState>(ITextChannel channel, BaseDiscordClient client,
37+
Action<TextChannelProperties, TState> func,
38+
TState state,
3739
RequestOptions options)
3840
{
3941
var args = new TextChannelProperties();
40-
func(args);
42+
func(args, state);
4143
var apiArgs = new API.Rest.ModifyTextChannelParams
4244
{
4345
Name = args.Name,
@@ -49,12 +51,13 @@ public static async Task<Model> ModifyAsync(ITextChannel channel, BaseDiscordCli
4951
};
5052
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
5153
}
52-
public static async Task<Model> ModifyAsync(IVoiceChannel channel, BaseDiscordClient client,
53-
Action<VoiceChannelProperties> func,
54+
public static async Task<Model> ModifyAsync<TState>(IVoiceChannel channel, BaseDiscordClient client,
55+
Action<VoiceChannelProperties, TState> func,
56+
TState state,
5457
RequestOptions options)
5558
{
5659
var args = new VoiceChannelProperties();
57-
func(args);
60+
func(args, state);
5861
var apiArgs = new API.Rest.ModifyVoiceChannelParams
5962
{
6063
Bitrate = args.Bitrate,

src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ public override async Task UpdateAsync(RequestOptions options = null)
6565
Update(model);
6666
}
6767
/// <inheritdoc />
68-
public async Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null)
68+
public Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null)
69+
=> ModifyAsync((props, f) => f(props), func, options);
70+
/// <inheritdoc />
71+
public async Task ModifyAsync<TState>(Action<GuildChannelProperties, TState> func, TState state, RequestOptions options = null)
6972
{
70-
var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
73+
var model = await ChannelHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
7174
Update(model);
7275
}
7376
/// <inheritdoc />

src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ internal override void Update(Model model)
4747
}
4848

4949
/// <inheritdoc />
50-
public async Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
50+
public Task ModifyAsync(Action<TextChannelProperties> func, RequestOptions options = null)
51+
=> ModifyAsync((props, f) => f(props), func, options);
52+
/// <inheritdoc />
53+
public async Task ModifyAsync<TState>(Action<TextChannelProperties, TState> func, TState state, RequestOptions options = null)
5154
{
52-
var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
55+
var model = await ChannelHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
5356
Update(model);
5457
}
5558

src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ internal override void Update(Model model)
4141
}
4242

4343
/// <inheritdoc />
44-
public async Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
44+
public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
45+
=> ModifyAsync((props, f) => f(props), func, options);
46+
/// <inheritdoc />
47+
public async Task ModifyAsync<TState>(Action<VoiceChannelProperties, TState> func, TState state, RequestOptions options = null)
4548
{
46-
var model = await ChannelHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
49+
var model = await ChannelHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
4750
Update(model);
4851
}
4952

src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ internal static class GuildHelper
1515
{
1616
//General
1717
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
18-
public static async Task<Model> ModifyAsync(IGuild guild, BaseDiscordClient client,
19-
Action<GuildProperties> func, RequestOptions options)
18+
public static async Task<Model> ModifyAsync<TState>(IGuild guild, BaseDiscordClient client,
19+
Action<GuildProperties, TState> func, TState state, RequestOptions options)
2020
{
2121
if (func == null) throw new ArgumentNullException(nameof(func));
2222

2323
var args = new GuildProperties();
24-
func(args);
24+
func(args, state);
2525

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

7979
var args = new GuildEmbedProperties();
80-
func(args);
80+
func(args, state);
8181
var apiArgs = new API.Rest.ModifyGuildEmbedParams
8282
{
8383
Enabled = args.Enabled

src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,25 @@ public Task DeleteAsync(RequestOptions options = null)
166166

167167
/// <inheritdoc />
168168
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
169-
public async Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
169+
public Task ModifyAsync(Action<GuildProperties> func, RequestOptions options = null)
170+
=> ModifyAsync((props, f) => f(props), func, options);
171+
/// <inheritdoc />
172+
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
173+
public async Task ModifyAsync<TState>(Action<GuildProperties, TState> func, TState state, RequestOptions options = null)
170174
{
171-
var model = await GuildHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
175+
var model = await GuildHelper.ModifyAsync(this, Discord, func, state, options).ConfigureAwait(false);
172176
Update(model);
173177
}
174178

175179
/// <inheritdoc />
176180
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
177-
public async Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
181+
public Task ModifyEmbedAsync(Action<GuildEmbedProperties> func, RequestOptions options = null)
182+
=> ModifyEmbedAsync((props, f) => f(props), func, options);
183+
/// <inheritdoc />
184+
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
185+
public async Task ModifyEmbedAsync<TState>(Action<GuildEmbedProperties, TState> func, TState state, RequestOptions options = null)
178186
{
179-
var model = await GuildHelper.ModifyEmbedAsync(this, Discord, func, options).ConfigureAwait(false);
187+
var model = await GuildHelper.ModifyEmbedAsync(this, Discord, func, state, options).ConfigureAwait(false);
180188
Update(model);
181189
}
182190

src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ internal static class MessageHelper
1313
{
1414
/// <exception cref="InvalidOperationException">Only the author of a message may modify the message.</exception>
1515
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
16-
public static async Task<Model> ModifyAsync(IMessage msg, BaseDiscordClient client, Action<MessageProperties> func,
16+
public static async Task<Model> ModifyAsync<TState>(IMessage msg, BaseDiscordClient client,
17+
Action<MessageProperties, TState> func,
18+
TState state,
1719
RequestOptions options)
1820
{
1921
if (msg.Author.Id != client.CurrentUser.Id)
2022
throw new InvalidOperationException("Only the author of a message may modify the message.");
2123

2224
var args = new MessageProperties();
23-
func(args);
25+
func(args, state);
2426

2527
var apiArgs = new API.Rest.ModifyMessageParams
2628
{

0 commit comments

Comments
 (0)