From 1a788d467b029dfa9a5165c23fbd94d05e66d407 Mon Sep 17 00:00:00 2001 From: zobweyt Date: Sat, 2 Sep 2023 23:57:17 +0300 Subject: [PATCH] Remove code duplication --- .../RestInteractionModuleBase.cs | 59 ++++++++----------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/Discord.Net.Interactions/RestInteractionModuleBase.cs b/src/Discord.Net.Interactions/RestInteractionModuleBase.cs index f878078820..570b9a2aa3 100644 --- a/src/Discord.Net.Interactions/RestInteractionModuleBase.cs +++ b/src/Discord.Net.Interactions/RestInteractionModuleBase.cs @@ -26,17 +26,7 @@ public abstract class RestInteractionModuleBase : InteractionModuleBase /// /// Thrown if the interaction isn't a type of . protected override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null) - { - if (Context.Interaction is not RestInteraction restInteraction) - throw new InvalidOperationException($"Invalid interaction type. Interaction must be a type of {nameof(RestInteraction)} in order to execute this method"); - - var payload = restInteraction.Defer(ephemeral, options); - - if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) - await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); - else - await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false); - } + => await HandleInteractionAsync(x => x.Defer(ephemeral, options)); /// /// Respond to a Rest based Discord Interaction using the delegate. @@ -54,45 +44,42 @@ protected override async Task DeferAsync(bool ephemeral = false, RequestOptions /// /// Thrown if the interaction isn't a type of . 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) - { - if (Context.Interaction is not RestInteraction restInteraction) - throw new InvalidOperationException($"Invalid interaction type. Interaction must be a type of {nameof(RestInteraction)} in order to execute this method"); - - var payload = restInteraction.Respond(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options); - - if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) - await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); - else - await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false); - } + => await HandleInteractionAsync(x => x.Respond(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options)); /// /// Responds to the interaction with a modal. /// /// The modal to respond with. /// The request options for this request. - /// A string that contains json to write back to the incoming http request. - /// - /// + /// + /// A Task representing the operation of creating the interaction response. + /// + /// Thrown if the interaction isn't a type of . protected override async Task RespondWithModalAsync(Modal modal, RequestOptions options = null) - { - if (Context.Interaction is not RestInteraction restInteraction) - throw new InvalidOperationException($"Invalid interaction type. Interaction must be a type of {nameof(RestInteraction)} in order to execute this method"); + => await HandleInteractionAsync(x => x.RespondWithModal(modal, options)); var payload = restInteraction.RespondWithModal(modal, options); - if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) - await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); - else - await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false); - } - + /// + /// Responds to the interaction with a modal. + /// + /// + /// The custom ID of the modal. + /// The request options for this request. + /// Delegate that can be used to modify the modal. + /// + /// A Task representing the operation of creating the interaction response. + /// + /// Thrown if the interaction isn't a type of . protected override async Task RespondWithModalAsync(string customId, RequestOptions options = null, Action modifyModal = null) + => await HandleInteractionAsync(x => x.RespondWithModal(customId, options, modifyModal)); + + private async Task HandleInteractionAsync(Func action) { if (Context.Interaction is not RestInteraction restInteraction) - throw new InvalidOperationException($"Invalid interaction type. Interaction must be a type of {nameof(RestInteraction)} in order to execute this method"); + throw new InvalidOperationException($"Interaction must be a type of {nameof(RestInteraction)} in order to execute this method."); - var payload = restInteraction.RespondWithModal(customId, options, modifyModal); + var payload = action(restInteraction); if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false);