using System;
using System.Threading.Tasks;
namespace Discord.Interactions
{
///
/// Provides a base class for a command module to inherit from.
///
/// Type of interaction context to be injected into the module.
public abstract class InteractionModuleBase : IInteractionModuleBase where T : class, IInteractionContext
{
///
/// Gets the underlying context of the command.
///
public T Context { get; private set; }
///
public virtual void AfterExecute (ICommandInfo command) { }
///
public virtual void BeforeExecute (ICommandInfo command) { }
///
public virtual Task BeforeExecuteAsync(ICommandInfo command) => Task.CompletedTask;
///
public virtual Task AfterExecuteAsync(ICommandInfo command) => Task.CompletedTask;
///
public virtual void OnModuleBuilding (InteractionService commandService, ModuleInfo module) { }
///
public virtual void Construct (Builders.ModuleBuilder builder, InteractionService commandService) { }
internal void SetContext (IInteractionContext context)
{
var newValue = context as T;
Context = newValue ?? throw new InvalidOperationException($"Invalid context type. Expected {typeof(T).Name}, got {context.GetType().Name}.");
}
///
protected virtual async Task DeferAsync(bool ephemeral = false, RequestOptions options = null) =>
await Context.Interaction.DeferAsync(ephemeral, options).ConfigureAwait(false);
///
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 async Task 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 async Task ReplyAsync (string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null,
AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null) =>
await Context.Channel.SendMessageAsync(text, false, embed, options, allowedMentions, messageReference, components).ConfigureAwait(false);
///
protected virtual async Task DeleteOriginalResponseAsync ( )
{
var response = await Context.Interaction.GetOriginalResponseAsync().ConfigureAwait(false);
await response.DeleteAsync().ConfigureAwait(false);
}
//IInteractionModuleBase
///
void IInteractionModuleBase.SetContext (IInteractionContext context) => SetContext(context);
}
///
/// Provides a base class for a command module to inherit from.
///
public abstract class InteractionModuleBase : InteractionModuleBase { }
}