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 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); } /// /// 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 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); } } }