Skip to content

Commit

Permalink
[Refactor] Remove some unnecessary async/await (#2739)
Browse files Browse the repository at this point in the history
* Remove some unnecessary async/await

* More not-so-async stuff

* More not-so-async stuff

* Fix merge issue
  • Loading branch information
dimson-n authored Nov 18, 2023
1 parent 699554a commit 86655a8
Show file tree
Hide file tree
Showing 73 changed files with 1,013 additions and 1,013 deletions.
8 changes: 4 additions & 4 deletions src/Discord.Net.Commands/Info/CommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ async Task<PreconditionResult> CheckGroups(IEnumerable<PreconditionAttribute> pr
return PreconditionResult.FromSuccess();
}

public async Task<ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult preconditionResult = null, IServiceProvider services = null)
public Task<ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult preconditionResult = null, IServiceProvider services = null)
{
services ??= EmptyServiceProvider.Instance;

if (!searchResult.IsSuccess)
return ParseResult.FromError(searchResult);
return Task.FromResult(ParseResult.FromError(searchResult));
if (preconditionResult != null && !preconditionResult.IsSuccess)
return ParseResult.FromError(preconditionResult);
return Task.FromResult(ParseResult.FromError(preconditionResult));

string input = searchResult.Text.Substring(startIndex);

return await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0, _commandService._quotationMarkAliasMap).ConfigureAwait(false);
return CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0, _commandService._quotationMarkAliasMap);
}

public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)
Expand Down
4 changes: 2 additions & 2 deletions src/Discord.Net.Commands/Info/ParameterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext co
return PreconditionResult.FromSuccess();
}

public async Task<TypeReaderResult> ParseAsync(ICommandContext context, string input, IServiceProvider services = null)
public Task<TypeReaderResult> ParseAsync(ICommandContext context, string input, IServiceProvider services = null)
{
services ??= EmptyServiceProvider.Instance;
return await _reader.ReadAsync(context, input, services).ConfigureAwait(false);
return _reader.ReadAsync(context, input, services);
}

public override string ToString() => Name;
Expand Down
7 changes: 3 additions & 4 deletions src/Discord.Net.Commands/ModuleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ public abstract class ModuleBase<T> : IModuleBase
/// <param name="stickers">A collection of stickers to send with the file.</param>
/// <param name="embeds">A array of <see cref="Embed"/>s to send with this response. Max 10.</param>
/// <param name="flags">Message flags combined as a bitfield.</param>
protected virtual async Task<IUserMessage> ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null,
protected virtual Task<IUserMessage> ReplyAsync(string message = null, bool isTTS = false, Embed embed = null, RequestOptions options = null,
AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null,
Embed[] embeds = null, MessageFlags flags = MessageFlags.None)
{
return await Context.Channel.SendMessageAsync(message, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags).ConfigureAwait(false);
}
=> Context.Channel.SendMessageAsync(message, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags);

/// <summary>
/// The method to execute asynchronously before executing the command.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Discord.Net.Commands/Readers/NullableTypeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public NullableTypeReader(TypeReader baseTypeReader)
}

/// <inheritdoc />
public override async Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
{
if (string.Equals(input, "null", StringComparison.OrdinalIgnoreCase) || string.Equals(input, "nothing", StringComparison.OrdinalIgnoreCase))
return TypeReaderResult.FromSuccess(new T?());
return await _baseTypeReader.ReadAsync(context, input, services).ConfigureAwait(false);
return Task.FromResult(TypeReaderResult.FromSuccess(new T?()));
return _baseTypeReader.ReadAsync(context, input, services);
}
}
}
6 changes: 2 additions & 4 deletions src/Discord.Net.Core/Extensions/MessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public static async Task RemoveReactionsAsync(this IUserMessage msg, IUser user,
/// A task that represents an asynchronous send operation for delivering the message. The task result
/// contains the sent message.
/// </returns>
public static async Task<IUserMessage> ReplyAsync(this IUserMessage msg, string text = null, bool isTTS = false, Embed embed = null, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None)
{
return await msg.Channel.SendMessageAsync(text, isTTS, embed, options, allowedMentions, new MessageReference(messageId: msg.Id), components, stickers, embeds, flags).ConfigureAwait(false);
}
public static Task<IUserMessage> ReplyAsync(this IUserMessage msg, string text = null, bool isTTS = false, Embed embed = null, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None)
=> msg.Channel.SendMessageAsync(text, isTTS, embed, options, allowedMentions, new MessageReference(messageId: msg.Id), components, stickers, embeds, flags);
}
}
6 changes: 2 additions & 4 deletions src/Discord.Net.Core/Logging/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ public Task DebugAsync(string source, FormattableString message, Exception ex =

public Logger CreateLogger(string name) => new Logger(this, name);

public async Task WriteInitialLog()
{
await ClientLogger.InfoAsync($"Discord.Net v{DiscordConfig.Version} (API v{DiscordConfig.APIVersion})").ConfigureAwait(false);
}
public Task WriteInitialLog()
=> ClientLogger.InfoAsync($"Discord.Net v{DiscordConfig.Version} (API v{DiscordConfig.APIVersion})");
}
}
10 changes: 4 additions & 6 deletions src/Discord.Net.Core/Utils/Cacheable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ internal Cacheable(TEntity value, TId id, bool hasValue, Func<Task<TEntity>> dow
/// A task that represents the asynchronous download operation. The task result contains the downloaded
/// entity.
/// </returns>
public async Task<TEntity> DownloadAsync()
{
return await DownloadFunc().ConfigureAwait(false);
}
public Task<TEntity> DownloadAsync()
=> DownloadFunc();

/// <summary>
/// Returns the cached entity if it exists; otherwise downloads it.
Expand Down Expand Up @@ -103,9 +101,9 @@ internal Cacheable(TCachedEntity value, TId id, bool hasValue, Func<Task<TDownlo
/// A task that represents the asynchronous download operation. The task result contains the downloaded
/// entity.
/// </returns>
public async Task<TDownloadableEntity> DownloadAsync()
public Task<TDownloadableEntity> DownloadAsync()
{
return await DownloadFunc().ConfigureAwait(false);
return DownloadFunc();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ protected virtual string GetLogString(IInteractionContext context)
}

/// <inheritdoc/>
public async Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
IServiceProvider services)
{
switch (InteractionService._runMode)
{
case RunMode.Sync:
{
return await ExecuteInternalAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false);
return ExecuteInternalAsync(context, autocompleteInteraction, parameter, services);
}
case RunMode.Async:
_ = Task.Run(async () =>
Expand All @@ -45,7 +45,7 @@ public async Task<IResult> ExecuteAsync(IInteractionContext context, IAutocomple
throw new InvalidOperationException($"RunMode {InteractionService._runMode} is not supported.");
}

return ExecuteResult.FromSuccess();
return Task.FromResult((IResult)ExecuteResult.FromSuccess());
}

private async Task<IResult> ExecuteInternalAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public static class IDiscordInteractionExtentions
/// <param name="modifyModal">Delegate that can be used to modify the modal.</param>
/// <param name="options">The request options for this <see langword="async"/> request.</param>
/// <returns>A task that represents the asynchronous operation of responding to the interaction.</returns>
public static async Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
public static Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
where T : class, IModal
{
if (!ModalUtils.TryGet<T>(out var modalInfo))
throw new ArgumentException($"{typeof(T).FullName} isn't referenced by any registered Modal Interaction Command and doesn't have a cached {typeof(ModalInfo)}");

await SendModalResponseAsync(interaction, customId, modalInfo, options, modifyModal);
return SendModalResponseAsync(interaction, customId, modalInfo, options, modifyModal);
}

/// <summary>
Expand All @@ -35,13 +35,13 @@ public static async Task RespondWithModalAsync<T>(this IDiscordInteraction inter
/// <param name="options">The request options for this <see langword="async"/> request.</param>
/// <param name="modifyModal">Delegate that can be used to modify the modal.</param>
/// <returns></returns>
public static async Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, InteractionService interactionService,
public static Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, InteractionService interactionService,
RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
where T : class, IModal
{
var modalInfo = ModalUtils.GetOrAdd<T>(interactionService);

await SendModalResponseAsync(interaction, customId, modalInfo, options, modifyModal);
return SendModalResponseAsync(interaction, customId, modalInfo, options, modifyModal);
}

/// <summary>
Expand All @@ -54,7 +54,7 @@ public static async Task RespondWithModalAsync<T>(this IDiscordInteraction inter
/// <param name="options">The request options for this <see langword="async"/> request.</param>
/// <param name="modifyModal">Delegate that can be used to modify the modal.</param>
/// <returns></returns>
public static async Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, T modal, RequestOptions options = null,
public static Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, T modal, RequestOptions options = null,
Action<ModalBuilder> modifyModal = null)
where T : class, IModal
{
Expand All @@ -79,13 +79,13 @@ public static async Task RespondWithModalAsync<T>(this IDiscordInteraction inter
if (modifyModal is not null)
modifyModal(builder);

await interaction.RespondWithModalAsync(builder.Build(), options).ConfigureAwait(false);
return interaction.RespondWithModalAsync(builder.Build(), options);
}

private static async Task SendModalResponseAsync(IDiscordInteraction interaction, string customId, ModalInfo modalInfo, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
private static Task SendModalResponseAsync(IDiscordInteraction interaction, string customId, ModalInfo modalInfo, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
{
var modal = modalInfo.ToModal(customId, modifyModal);
await interaction.RespondWithModalAsync(modal, options).ConfigureAwait(false);
return interaction.RespondWithModalAsync(modal, options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ internal AutocompleteCommandInfo(AutocompleteCommandBuilder builder, ModuleInfo
}

/// <inheritdoc/>
public override async Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
public override Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
{
if (context.Interaction is not IAutocompleteInteraction)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Autocomplete Interaction");
return Task.FromResult((IResult)ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Autocomplete Interaction"));

return await base.ExecuteAsync(context, services).ConfigureAwait(false);
return base.ExecuteAsync(context, services);
}

protected override Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services)
Expand Down
6 changes: 3 additions & 3 deletions src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ internal CommandInfo(Builders.ICommandBuilder builder, ModuleInfo module, Intera
}

/// <inheritdoc/>
public virtual async Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
public virtual Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
{
switch (RunMode)
{
case RunMode.Sync:
return await ExecuteInternalAsync(context, services).ConfigureAwait(false);
return ExecuteInternalAsync(context, services);
case RunMode.Async:
_ = Task.Run(async () =>
{
Expand All @@ -104,7 +104,7 @@ public virtual async Task<IResult> ExecuteAsync(IInteractionContext context, ISe
throw new InvalidOperationException($"RunMode {RunMode} is not supported.");
}

return ExecuteResult.FromSuccess();
return Task.FromResult((IResult)ExecuteResult.FromSuccess());
}

protected abstract Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ internal ComponentCommandInfo(ComponentCommandBuilder builder, ModuleInfo module
}

/// <inheritdoc/>
public override async Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
public override Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
{
if (context.Interaction is not IComponentInteraction)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Message Component Interaction");
return Task.FromResult((IResult)ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Message Component Interaction"));

return await base.ExecuteAsync(context, services).ConfigureAwait(false);
return base.ExecuteAsync(context, services);
}

protected override async Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ internal MessageCommandInfo(Builders.ContextCommandBuilder builder, ModuleInfo m
: base(builder, module, commandService) { }

/// <inheritdoc/>
public override async Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
public override Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
{
if (context.Interaction is not IMessageCommandInteraction)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Message Command Interation");
return Task.FromResult((IResult)ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Message Command Interation"));

return await base.ExecuteAsync(context, services).ConfigureAwait(false);
return base.ExecuteAsync(context, services);
}

protected override Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ internal UserCommandInfo(Builders.ContextCommandBuilder builder, ModuleInfo modu
: base(builder, module, commandService) { }

/// <inheritdoc/>
public override async Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
public override Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
{
if (context.Interaction is not IUserCommandInteraction userCommand)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Message Command Interation");
return Task.FromResult((IResult)ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Message Command Interation"));

return await base.ExecuteAsync(context, services).ConfigureAwait(false);
return base.ExecuteAsync(context, services);
}

protected override Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ internal ModalCommandInfo(Builders.ModalCommandBuilder builder, ModuleInfo modul
}

/// <inheritdoc/>
public override async Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
public override Task<IResult> ExecuteAsync(IInteractionContext context, IServiceProvider services)
{
if (context.Interaction is not IModalInteraction modalInteraction)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Modal Interaction.");
return Task.FromResult((IResult)ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Modal Interaction."));

return await base.ExecuteAsync(context, services).ConfigureAwait(false);
return base.ExecuteAsync(context, services);
}

protected override async Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services)
Expand Down
Loading

0 comments on commit 86655a8

Please sign in to comment.