using Discord.Rest; using System; using System.Threading.Tasks; namespace Discord.Interactions { /// /// Provides a base class for a Rest based command module to inherit from. /// /// Type of interaction context to be injected into the module. public abstract class RestInteractionModuleBase : InteractionModuleBase where T : class, IInteractionContext { /// /// Gets or sets the underlying Interaction Service. /// public InteractionService InteractionService { get; set; } /// /// Defer a Rest based Discord Interaction using the delegate. /// /// if the response should be hidden to everyone besides the invoker of the command, otherwise . /// The request options for this response. /// /// A Task representing the operation of creating the interaction response. /// /// Thrown if the interaction isn't a type of . protected override Task DeferAsync(bool ephemeral = false, RequestOptions options = null) => HandleInteractionAsync(x => x.Defer(ephemeral, options)); /// /// Respond to a Rest based Discord Interaction using the delegate. /// /// The text of the message to be sent. /// A array of embeds to send with this response. Max 10. /// if the message should be read out by a text-to-speech reader, otherwise . /// if the response should be hidden to everyone besides the invoker of the command, otherwise . /// The allowed mentions for this response. /// The request options for this response. /// A to be sent with this response. /// A single embed to send with this response. If this is passed alongside an array of embeds, the single embed will be ignored. /// /// A Task representing the operation of creating the interaction response. /// /// Thrown if the interaction isn't a type of . 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)); /// /// Responds to the interaction with a modal. /// /// The modal to respond with. /// The request options for this request. /// /// A Task representing the operation of creating the interaction response. /// /// Thrown if the interaction isn't a type of . protected override Task RespondWithModalAsync(Modal modal, RequestOptions options = null) => HandleInteractionAsync(x => x.RespondWithModal(modal, options)); /// /// Responds to the interaction with a modal. /// /// The type of the modal. /// The custom ID of the modal. /// The modal to respond with. /// 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, TModal modal, RequestOptions options = null, Action modifyModal = null) => await HandleInteractionAsync(x => x.RespondWithModal(customId, modal, options, modifyModal)); /// /// 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 Task RespondWithModalAsync(string customId, RequestOptions options = null, Action modifyModal = null) => HandleInteractionAsync(x => x.RespondWithModal(customId, options, modifyModal)); private Task HandleInteractionAsync(Func 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."); var payload = action(restInteraction); if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) return restContext.InteractionResponseCallback.Invoke(payload); else return InteractionService._restResponseCallback(Context, payload); } } }