[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

@@ -26,14 +26,14 @@ namespace Discord.Interactions
}
/// <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 () =>
@@ -45,7 +45,7 @@ namespace Discord.Interactions
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,

View File

@@ -13,13 +13,13 @@ namespace Discord.Interactions
/// <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>
@@ -35,13 +35,13 @@ namespace Discord.Interactions
/// <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>
@@ -54,7 +54,7 @@ namespace Discord.Interactions
/// <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
{
@@ -79,13 +79,13 @@ namespace Discord.Interactions
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);
}
}
}

View File

@@ -36,12 +36,12 @@ namespace Discord.Interactions
}
/// <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)

View File

@@ -88,12 +88,12 @@ namespace Discord.Interactions
}
/// <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 () =>
{
@@ -104,7 +104,7 @@ namespace Discord.Interactions
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);

View File

@@ -24,12 +24,12 @@ namespace Discord.Interactions
}
/// <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)

View File

@@ -12,12 +12,12 @@ namespace Discord.Interactions
: 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)

View File

@@ -12,12 +12,12 @@ namespace Discord.Interactions
: 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)

View File

@@ -28,12 +28,12 @@ namespace Discord.Interactions
}
/// <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)

View File

@@ -64,12 +64,12 @@ namespace Discord.Interactions
}
/// <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 ISlashCommandInteraction)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Slash Command Interaction");
return Task.FromResult((IResult)ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Slash Command Interaction"));
return await base.ExecuteAsync(context, services);
return base.ExecuteAsync(context, services);
}
protected override async Task<IResult> ParseArgumentsAsync(IInteractionContext context, IServiceProvider services)

View File

@@ -41,13 +41,13 @@ namespace Discord.Interactions
}
/// <inheritdoc cref="IDiscordInteraction.DeferAsync(bool, RequestOptions)"/>
protected virtual async Task DeferAsync(bool ephemeral = false, RequestOptions options = null) =>
await Context.Interaction.DeferAsync(ephemeral, options).ConfigureAwait(false);
protected virtual Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
=> Context.Interaction.DeferAsync(ephemeral, options);
/// <inheritdoc cref="IDiscordInteraction.RespondAsync(string, Embed[], bool, bool, AllowedMentions, MessageComponent, Embed, RequestOptions)"/>
protected virtual async Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, Embed embed = null) =>
await Context.Interaction.RespondAsync(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false);
protected virtual Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, Embed embed = null)
=> Context.Interaction.RespondAsync(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
/// <inheritdoc cref="IDiscordInteraction.RespondWithFileAsync(Stream, string, string, Embed[], bool, bool, AllowedMentions, MessageComponent, Embed, RequestOptions)"/>
protected virtual Task RespondWithFileAsync(Stream fileStream, string fileName, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
@@ -70,9 +70,9 @@ namespace Discord.Interactions
=> Context.Interaction.RespondWithFilesAsync(attachments, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
/// <inheritdoc cref="IDiscordInteraction.FollowupAsync(string, Embed[], bool, bool, AllowedMentions, MessageComponent, Embed, RequestOptions)"/>
protected virtual async Task<IUserMessage> FollowupAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, Embed embed = null) =>
await Context.Interaction.FollowupAsync(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false);
protected virtual Task<IUserMessage> FollowupAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, Embed embed = null)
=> Context.Interaction.FollowupAsync(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
/// <inheritdoc cref="IDiscordInteraction.FollowupWithFileAsync(Stream, string, string, Embed[], bool, bool, AllowedMentions, MessageComponent, Embed, RequestOptions)"/>
protected virtual Task<IUserMessage> FollowupWithFileAsync(Stream fileStream, string fileName, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
@@ -95,10 +95,10 @@ namespace Discord.Interactions
=> Context.Interaction.FollowupWithFilesAsync(attachments, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);
/// <inheritdoc cref="IMessageChannel.SendMessageAsync(string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent, ISticker[], Embed[], MessageFlags)"/>
protected virtual async Task<IUserMessage> ReplyAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null,
protected virtual Task<IUserMessage> ReplyAsync(string text = 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) =>
await Context.Channel.SendMessageAsync(text, false, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags).ConfigureAwait(false);
Embed[] embeds = null, MessageFlags flags = MessageFlags.None)
=> Context.Channel.SendMessageAsync(text, false, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags);
/// <inheritdoc cref="IDiscordInteraction.GetOriginalResponseAsync(RequestOptions)"/>
protected virtual Task<IUserMessage> GetOriginalResponseAsync(RequestOptions options = null)
@@ -116,16 +116,16 @@ namespace Discord.Interactions
}
/// <inheritdoc cref="IDiscordInteraction.RespondWithModalAsync(Modal, RequestOptions)"/>
protected virtual async Task RespondWithModalAsync(Modal modal, RequestOptions options = null)
=> await Context.Interaction.RespondWithModalAsync(modal, options);
protected virtual Task RespondWithModalAsync(Modal modal, RequestOptions options = null)
=> Context.Interaction.RespondWithModalAsync(modal, options);
/// <inheritdoc cref="IDiscordInteractionExtentions.RespondWithModalAsync{T}(IDiscordInteraction, string, T, RequestOptions, Action{ModalBuilder})"/>
protected virtual async Task RespondWithModalAsync<TModal>(string customId, TModal modal, RequestOptions options = null, Action<ModalBuilder> modifyModal = null) where TModal : class, IModal
=> await Context.Interaction.RespondWithModalAsync(customId, modal, options, modifyModal);
protected virtual Task RespondWithModalAsync<TModal>(string customId, TModal modal, RequestOptions options = null, Action<ModalBuilder> modifyModal = null) where TModal : class, IModal
=> Context.Interaction.RespondWithModalAsync(customId, modal, options, modifyModal);
/// <inheritdoc cref="IDiscordInteractionExtentions.RespondWithModalAsync{T}(IDiscordInteraction, string, RequestOptions, Action{ModalBuilder})"/>
protected virtual async Task RespondWithModalAsync<TModal>(string customId, RequestOptions options = null, Action<ModalBuilder> modifyModal = null) where TModal : class, IModal
=> await Context.Interaction.RespondWithModalAsync<TModal>(customId, options, modifyModal);
protected virtual Task RespondWithModalAsync<TModal>(string customId, RequestOptions options = null, Action<ModalBuilder> modifyModal = null) where TModal : class, IModal
=> Context.Interaction.RespondWithModalAsync<TModal>(customId, options, modifyModal);
/// <inheritdoc cref="IDiscordInteraction.RespondWithPremiumRequiredAsync(RequestOptions)"/>
protected virtual Task RespondWithPremiumRequiredAsync(RequestOptions options = null)

View File

@@ -437,12 +437,12 @@ namespace Discord.Interactions
/// <returns>
/// A task representing the command registration process. The task result contains the active application commands of the target guild.
/// </returns>
public async Task<IReadOnlyCollection<RestGuildCommand>> AddCommandsToGuildAsync(IGuild guild, bool deleteMissing = false, params ICommandInfo[] commands)
public Task<IReadOnlyCollection<RestGuildCommand>> AddCommandsToGuildAsync(IGuild guild, bool deleteMissing = false, params ICommandInfo[] commands)
{
if (guild is null)
throw new ArgumentNullException(nameof(guild));
return await AddCommandsToGuildAsync(guild.Id, deleteMissing, commands).ConfigureAwait(false);
return AddCommandsToGuildAsync(guild.Id, deleteMissing, commands);
}
/// <summary>
@@ -498,12 +498,12 @@ namespace Discord.Interactions
/// <returns>
/// A task representing the command registration process. The task result contains the active application commands of the target guild.
/// </returns>
public async Task<IReadOnlyCollection<RestGuildCommand>> AddModulesToGuildAsync(IGuild guild, bool deleteMissing = false, params ModuleInfo[] modules)
public Task<IReadOnlyCollection<RestGuildCommand>> AddModulesToGuildAsync(IGuild guild, bool deleteMissing = false, params ModuleInfo[] modules)
{
if (guild is null)
throw new ArgumentNullException(nameof(guild));
return await AddModulesToGuildAsync(guild.Id, deleteMissing, modules).ConfigureAwait(false);
return AddModulesToGuildAsync(guild.Id, deleteMissing, modules);
}
/// <summary>
@@ -692,12 +692,12 @@ namespace Discord.Interactions
/// <returns>
/// A task representing the command de-registration process. The task result contains the active application commands of the target guild.
/// </returns>
public async Task<IReadOnlyCollection<RestGuildCommand>> RemoveModulesFromGuildAsync(IGuild guild, params ModuleInfo[] modules)
public Task<IReadOnlyCollection<RestGuildCommand>> RemoveModulesFromGuildAsync(IGuild guild, params ModuleInfo[] modules)
{
if (guild is null)
throw new ArgumentNullException(nameof(guild));
return await RemoveModulesFromGuildAsync(guild.Id, modules).ConfigureAwait(false);
return RemoveModulesFromGuildAsync(guild.Id, modules);
}
/// <summary>
@@ -1164,7 +1164,7 @@ namespace Discord.Interactions
/// <returns>
/// The active command permissions after the modification.
/// </returns>
public async Task<GuildApplicationCommandPermission> ModifySlashCommandPermissionsAsync(ModuleInfo module, IGuild guild,
public Task<GuildApplicationCommandPermission> ModifySlashCommandPermissionsAsync(ModuleInfo module, IGuild guild,
params ApplicationCommandPermission[] permissions)
{
if (module is null)
@@ -1173,7 +1173,7 @@ namespace Discord.Interactions
if (guild is null)
throw new ArgumentNullException(nameof(guild));
return await ModifySlashCommandPermissionsAsync(module, guild.Id, permissions).ConfigureAwait(false);
return ModifySlashCommandPermissionsAsync(module, guild.Id, permissions);
}
/// <summary>
@@ -1212,7 +1212,7 @@ namespace Discord.Interactions
/// <returns>
/// The active command permissions after the modification.
/// </returns>
public async Task<GuildApplicationCommandPermission> ModifySlashCommandPermissionsAsync(SlashCommandInfo command, IGuild guild,
public Task<GuildApplicationCommandPermission> ModifySlashCommandPermissionsAsync(SlashCommandInfo command, IGuild guild,
params ApplicationCommandPermission[] permissions)
{
if (command is null)
@@ -1221,7 +1221,7 @@ namespace Discord.Interactions
if (guild is null)
throw new ArgumentNullException(nameof(guild));
return await ModifyApplicationCommandPermissionsAsync(command, guild.Id, permissions).ConfigureAwait(false);
return ModifyApplicationCommandPermissionsAsync(command, guild.Id, permissions);
}
/// <summary>
@@ -1233,8 +1233,9 @@ namespace Discord.Interactions
/// <returns>
/// The active command permissions after the modification.
/// </returns>
public async Task<GuildApplicationCommandPermission> ModifySlashCommandPermissionsAsync(SlashCommandInfo command, ulong guildId,
params ApplicationCommandPermission[] permissions) => await ModifyApplicationCommandPermissionsAsync(command, guildId, permissions).ConfigureAwait(false);
public Task<GuildApplicationCommandPermission> ModifySlashCommandPermissionsAsync(SlashCommandInfo command, ulong guildId,
params ApplicationCommandPermission[] permissions)
=> ModifyApplicationCommandPermissionsAsync(command, guildId, permissions);
/// <summary>
/// Modify the command permissions of the matching Discord Slash Command.
@@ -1245,7 +1246,7 @@ namespace Discord.Interactions
/// <returns>
/// The active command permissions after the modification.
/// </returns>
public async Task<GuildApplicationCommandPermission> ModifyContextCommandPermissionsAsync(ContextCommandInfo command, IGuild guild,
public Task<GuildApplicationCommandPermission> ModifyContextCommandPermissionsAsync(ContextCommandInfo command, IGuild guild,
params ApplicationCommandPermission[] permissions)
{
if (command is null)
@@ -1254,7 +1255,7 @@ namespace Discord.Interactions
if (guild is null)
throw new ArgumentNullException(nameof(guild));
return await ModifyApplicationCommandPermissionsAsync(command, guild.Id, permissions).ConfigureAwait(false);
return ModifyApplicationCommandPermissionsAsync(command, guild.Id, permissions);
}
/// <summary>
@@ -1266,8 +1267,9 @@ namespace Discord.Interactions
/// <returns>
/// The active command permissions after the modification.
/// </returns>
public async Task<GuildApplicationCommandPermission> ModifyContextCommandPermissionsAsync(ContextCommandInfo command, ulong guildId,
params ApplicationCommandPermission[] permissions) => await ModifyApplicationCommandPermissionsAsync(command, guildId, permissions).ConfigureAwait(false);
public Task<GuildApplicationCommandPermission> ModifyContextCommandPermissionsAsync(ContextCommandInfo command, ulong guildId,
params ApplicationCommandPermission[] permissions)
=> ModifyApplicationCommandPermissionsAsync(command, guildId, permissions);
private async Task<GuildApplicationCommandPermission> ModifyApplicationCommandPermissionsAsync<T>(T command, ulong guildId,
params ApplicationCommandPermission[] permissions) where T : class, IApplicationCommandInfo, ICommandInfo

View File

@@ -25,8 +25,8 @@ namespace Discord.Interactions
/// A Task representing the operation of creating the interaction response.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown if the interaction isn't a type of <see cref="RestInteraction"/>.</exception>
protected override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
=> await HandleInteractionAsync(x => x.Defer(ephemeral, options));
protected override Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
=> HandleInteractionAsync(x => x.Defer(ephemeral, options));
/// <summary>
/// Respond to a Rest based Discord Interaction using the <see cref="InteractionServiceConfig.RestResponseCallback"/> delegate.
@@ -43,8 +43,8 @@ namespace Discord.Interactions
/// A Task representing the operation of creating the interaction response.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown if the interaction isn't a type of <see cref="RestInteraction"/>.</exception>
protected override async Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, Embed embed = null)
=> await HandleInteractionAsync(x => x.Respond(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options));
protected override Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent components = null, Embed embed = null)
=> HandleInteractionAsync(x => x.Respond(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options));
/// <summary>
/// Responds to the interaction with a modal.
@@ -55,8 +55,8 @@ namespace Discord.Interactions
/// A Task representing the operation of creating the interaction response.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown if the interaction isn't a type of <see cref="RestInteraction"/>.</exception>
protected override async Task RespondWithModalAsync(Modal modal, RequestOptions options = null)
=> await HandleInteractionAsync(x => x.RespondWithModal(modal, options));
protected override Task RespondWithModalAsync(Modal modal, RequestOptions options = null)
=> HandleInteractionAsync(x => x.RespondWithModal(modal, options));
/// <summary>
/// Responds to the interaction with a modal.
@@ -84,10 +84,10 @@ namespace Discord.Interactions
/// A Task representing the operation of creating the interaction response.
/// </returns>
/// <exception cref="InvalidOperationException">Thrown if the interaction isn't a type of <see cref="RestInteraction"/>.</exception>
protected override async Task RespondWithModalAsync<TModal>(string customId, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
=> await HandleInteractionAsync(x => x.RespondWithModal<TModal>(customId, options, modifyModal));
protected override Task RespondWithModalAsync<TModal>(string customId, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
=> HandleInteractionAsync(x => x.RespondWithModal<TModal>(customId, options, modifyModal));
private async Task HandleInteractionAsync(Func<RestInteraction, string> action)
private Task HandleInteractionAsync(Func<RestInteraction, string> action)
{
if (Context.Interaction is not RestInteraction restInteraction)
throw new InvalidOperationException($"Interaction must be a type of {nameof(RestInteraction)} in order to execute this method.");
@@ -95,9 +95,9 @@ namespace Discord.Interactions
var payload = action(restInteraction);
if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null)
await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false);
return restContext.InteractionResponseCallback.Invoke(payload);
else
await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false);
return InteractionService._restResponseCallback(Context, payload);
}
}
}