[Refactor] Remove some unnecessary async/await (#2739)

* Remove some unnecessary async/await

* More not-so-async stuff

* More not-so-async stuff

* Fix merge issue
This commit is contained in:
Dmitry
2023-11-19 00:29:14 +03:00
committed by GitHub
parent 699554ad11
commit 86655a8157
73 changed files with 1020 additions and 1020 deletions

View File

@@ -259,13 +259,14 @@ namespace Discord.WebSocket
#region IMessageChannel
/// <inheritdoc />
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetMessageAsync(id, options).ConfigureAwait(false);
return GetMessageAsync(id, options);
else
return GetCachedMessage(id);
return Task.FromResult((IMessage)GetCachedMessage(id));
}
/// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)
=> mode == CacheMode.CacheOnly ? null : GetMessagesAsync(limit, options);

View File

@@ -330,12 +330,12 @@ namespace Discord.WebSocket
#region IMessageChannel
/// <inheritdoc />
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetMessageAsync(id, options).ConfigureAwait(false);
return GetMessageAsync(id, options);
else
return GetCachedMessage(id);
return Task.FromResult((IMessage)GetCachedMessage(id));
}
/// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)

View File

@@ -131,10 +131,8 @@ namespace Discord.WebSocket
/// <returns>
/// A task representing the asynchronous permission operation for adding the specified permissions to the channel.
/// </returns>
public virtual async Task AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options = null)
{
await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, user, permissions, options).ConfigureAwait(false);
}
public virtual Task AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options = null)
=> ChannelHelper.AddPermissionOverwriteAsync(this, Discord, user, permissions, options);
/// <summary>
/// Adds or updates the permission overwrite for the given role.
@@ -145,10 +143,9 @@ namespace Discord.WebSocket
/// <returns>
/// A task representing the asynchronous permission operation for adding the specified permissions to the channel.
/// </returns>
public virtual async Task AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options = null)
{
await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, role, permissions, options).ConfigureAwait(false);
}
public virtual Task AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options = null)
=> ChannelHelper.AddPermissionOverwriteAsync(this, Discord, role, permissions, options);
/// <summary>
/// Removes the permission overwrite for the given user, if one exists.
/// </summary>
@@ -157,10 +154,9 @@ namespace Discord.WebSocket
/// <returns>
/// A task representing the asynchronous operation for removing the specified permissions from the channel.
/// </returns>
public virtual async Task RemovePermissionOverwriteAsync(IUser user, RequestOptions options = null)
{
await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, user, options).ConfigureAwait(false);
}
public virtual Task RemovePermissionOverwriteAsync(IUser user, RequestOptions options = null)
=> ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, user, options);
/// <summary>
/// Removes the permission overwrite for the given role, if one exists.
/// </summary>
@@ -169,10 +165,8 @@ namespace Discord.WebSocket
/// <returns>
/// A task representing the asynchronous operation for removing the specified permissions from the channel.
/// </returns>
public virtual async Task RemovePermissionOverwriteAsync(IRole role, RequestOptions options = null)
{
await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, role, options).ConfigureAwait(false);
}
public virtual Task RemovePermissionOverwriteAsync(IRole role, RequestOptions options = null)
=> ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, role, options);
public new virtual SocketGuildUser GetUser(ulong id) => null;
@@ -207,17 +201,20 @@ namespace Discord.WebSocket
OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IUser user)
=> GetPermissionOverwrite(user);
/// <inheritdoc />
async Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options)
=> await AddPermissionOverwriteAsync(role, permissions, options).ConfigureAwait(false);
Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options)
=> AddPermissionOverwriteAsync(role, permissions, options);
/// <inheritdoc />
async Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options)
=> await AddPermissionOverwriteAsync(user, permissions, options).ConfigureAwait(false);
Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options)
=> AddPermissionOverwriteAsync(user, permissions, options);
/// <inheritdoc />
async Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role, RequestOptions options)
=> await RemovePermissionOverwriteAsync(role, options).ConfigureAwait(false);
Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role, RequestOptions options)
=> RemovePermissionOverwriteAsync(role, options);
/// <inheritdoc />
async Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user, RequestOptions options)
=> await RemovePermissionOverwriteAsync(user, options).ConfigureAwait(false);
Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user, RequestOptions options)
=> RemovePermissionOverwriteAsync(user, options);
/// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)

View File

@@ -413,12 +413,12 @@ namespace Discord.WebSocket
#region IMessageChannel
/// <inheritdoc />
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetMessageAsync(id, options).ConfigureAwait(false);
return GetMessageAsync(id, options);
else
return GetCachedMessage(id);
return Task.FromResult((IMessage)GetCachedMessage(id));
}
/// <inheritdoc />
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options)

View File

@@ -89,20 +89,17 @@ namespace Discord.WebSocket
=> ChannelHelper.ModifyAsync(this, Discord, func, options);
/// <inheritdoc />
public async Task<IAudioClient> ConnectAsync(bool selfDeaf = false, bool selfMute = false, bool external = false)
{
return await Guild.ConnectAudioAsync(Id, selfDeaf, selfMute, external).ConfigureAwait(false);
}
public Task<IAudioClient> ConnectAsync(bool selfDeaf = false, bool selfMute = false, bool external = false)
=> Guild.ConnectAudioAsync(Id, selfDeaf, selfMute, external);
/// <inheritdoc />
public async Task DisconnectAsync()
=> await Guild.DisconnectAudioAsync();
public Task DisconnectAsync()
=> Guild.DisconnectAudioAsync();
/// <inheritdoc />
public async Task ModifyAsync(Action<AudioChannelProperties> func, RequestOptions options = null)
{
await Guild.ModifyAudioAsync(Id, func, options).ConfigureAwait(false);
}
public Task ModifyAsync(Action<AudioChannelProperties> func, RequestOptions options = null)
=> Guild.ModifyAudioAsync(Id, func, options);
/// <inheritdoc />
public override SocketGuildUser GetUser(ulong id)

View File

@@ -1307,10 +1307,9 @@ namespace Discord.WebSocket
}
/// <inheritdoc />
public async Task DownloadUsersAsync()
{
await Discord.DownloadUsersAsync(new[] { this }).ConfigureAwait(false);
}
public Task DownloadUsersAsync()
=> Discord.DownloadUsersAsync(new[] { this });
internal void CompleteDownloadUsers()
{
_downloaderPromise.TrySetResultAsync(true);
@@ -1545,7 +1544,8 @@ namespace Discord.WebSocket
/// </summary>
/// <param name="user">The user to disconnect.</param>
/// <returns>A task that represents the asynchronous operation for disconnecting a user.</returns>
async Task IGuild.DisconnectAsync(IGuildUser user) => await user.ModifyAsync(x => x.Channel = null);
Task IGuild.DisconnectAsync(IGuildUser user)
=> user.ModifyAsync(x => x.Channel = null);
#endregion
#region Stickers
@@ -1850,7 +1850,7 @@ namespace Discord.WebSocket
}
}
private async Task ModifyAudioInternalAsync(ulong channelId, Action<AudioChannelProperties> func, RequestOptions options)
private Task ModifyAudioInternalAsync(ulong channelId, Action<AudioChannelProperties> func, RequestOptions options)
{
if (_voiceStateUpdateParams == null || _voiceStateUpdateParams.ChannelId != channelId)
throw new InvalidOperationException("Cannot modify properties of not connected audio channel");
@@ -1863,7 +1863,7 @@ namespace Discord.WebSocket
if (props.SelfMute.IsSpecified)
_voiceStateUpdateParams.SelfMute = props.SelfMute.Value;
await Discord.ApiClient.SendVoiceStateUpdateAsync(_voiceStateUpdateParams, options).ConfigureAwait(false);
return Discord.ApiClient.SendVoiceStateUpdateAsync(_voiceStateUpdateParams, options);
}
internal async Task FinishConnectAudio(string url, string token)

View File

@@ -305,7 +305,7 @@ namespace Discord.WebSocket
}
/// <inheritdoc/>
public override async Task<RestFollowupMessage> FollowupAsync(
public override Task<RestFollowupMessage> FollowupAsync(
string text = null,
Embed[] embeds = null,
bool isTTS = false,
@@ -338,11 +338,11 @@ namespace Discord.WebSocket
if (ephemeral)
args.Flags = MessageFlags.Ephemeral;
return await InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options);
return InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options);
}
/// <inheritdoc/>
public override async Task<RestFollowupMessage> FollowupWithFilesAsync(
public override Task<RestFollowupMessage> FollowupWithFilesAsync(
IEnumerable<FileAttachment> attachments,
string text = null,
Embed[] embeds = null,
@@ -391,7 +391,7 @@ namespace Discord.WebSocket
flags |= MessageFlags.Ephemeral;
var args = new API.Rest.UploadWebhookFileParams(attachments.ToArray()) { Flags = flags, Content = text, IsTTS = isTTS, Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified };
return await InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options).ConfigureAwait(false);
return InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options);
}
/// <inheritdoc/>

View File

@@ -302,7 +302,7 @@ namespace Discord.WebSocket
}
/// <inheritdoc/>
public override async Task<RestFollowupMessage> FollowupAsync(
public override Task<RestFollowupMessage> FollowupAsync(
string text = null,
Embed[] embeds = null,
bool isTTS = false,
@@ -335,11 +335,11 @@ namespace Discord.WebSocket
if (ephemeral)
args.Flags = MessageFlags.Ephemeral;
return await InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options);
return InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options);
}
/// <inheritdoc/>
public override async Task<RestFollowupMessage> FollowupWithFilesAsync(
public override Task<RestFollowupMessage> FollowupWithFilesAsync(
IEnumerable<FileAttachment> attachments,
string text = null,
Embed[] embeds = null,
@@ -388,7 +388,7 @@ namespace Discord.WebSocket
flags |= MessageFlags.Ephemeral;
var args = new API.Rest.UploadWebhookFileParams(attachments.ToArray()) { Flags = flags, Content = text, IsTTS = isTTS, Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified };
return await InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options).ConfigureAwait(false);
return InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options);
}
/// <inheritdoc/>

View File

@@ -234,7 +234,7 @@ namespace Discord.WebSocket
}
/// <inheritdoc/>
public override async Task<RestFollowupMessage> FollowupAsync(
public override Task<RestFollowupMessage> FollowupAsync(
string text = null,
Embed[] embeds = null,
bool isTTS = false,
@@ -267,11 +267,11 @@ namespace Discord.WebSocket
if (ephemeral)
args.Flags = MessageFlags.Ephemeral;
return await InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options);
return InteractionHelper.SendFollowupAsync(Discord.Rest, args, Token, Channel, options);
}
/// <inheritdoc/>
public override async Task<RestFollowupMessage> FollowupWithFilesAsync(
public override Task<RestFollowupMessage> FollowupWithFilesAsync(
IEnumerable<FileAttachment> attachments,
string text = null,
Embed[] embeds = null,
@@ -320,7 +320,7 @@ namespace Discord.WebSocket
flags |= MessageFlags.Ephemeral;
var args = new API.Rest.UploadWebhookFileParams(attachments.ToArray()) { Flags = flags, Content = text, IsTTS = isTTS, Embeds = embeds.Any() ? embeds.Select(x => x.ToModel()).ToArray() : Optional<API.Embed[]>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, MessageComponents = components?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional<API.ActionRowComponent[]>.Unspecified };
return await InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options).ConfigureAwait(false);
return InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options);
}
/// <summary>

View File

@@ -228,14 +228,14 @@ namespace Discord.WebSocket
/// <inheritdoc />
/// <exception cref="InvalidOperationException">This operation may only be called on a <see cref="INewsChannel"/> channel.</exception>
public async Task CrosspostAsync(RequestOptions options = null)
public Task CrosspostAsync(RequestOptions options = null)
{
if (!(Channel is INewsChannel))
{
throw new InvalidOperationException("Publishing (crossposting) is only valid in news channels.");
}
await MessageHelper.CrosspostAsync(this, Discord, options);
return MessageHelper.CrosspostAsync(this, Discord, options);
}
private string DebuggerDisplay => $"{Author}: {Content} ({Id}{(Attachments.Count > 0 ? $", {Attachments.Count} Attachments" : "")})";