diff --git a/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs index 9e30c55f4..424c5d3a0 100644 --- a/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/AutocompleteCommandInfo.cs @@ -46,7 +46,7 @@ namespace Discord.Interactions /// protected override Task InvokeModuleEvent(IInteractionContext context, IResult result) => - CommandService._autocompleteCommandExecutedEvent.InvokeAsync(this, context, result); + CommandService.InvokeResultEventsAsync(this, context, result); /// protected override string GetLogString(IInteractionContext context) diff --git a/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs index 22d6aba6c..0b2849c77 100644 --- a/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs @@ -84,7 +84,7 @@ namespace Discord.Interactions } protected override Task InvokeModuleEvent(IInteractionContext context, IResult result) - => CommandService._componentCommandExecutedEvent.InvokeAsync(this, context, result); + => CommandService.InvokeResultEventsAsync(this, context, result); protected override string GetLogString(IInteractionContext context) { diff --git a/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs index 4c2e7af7d..94f5186f6 100644 --- a/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/ContextCommands/ContextCommandInfo.cs @@ -46,6 +46,6 @@ namespace Discord.Interactions /// protected override Task InvokeModuleEvent (IInteractionContext context, IResult result) - => CommandService._contextCommandExecutedEvent.InvokeAsync(this, context, result); + => CommandService.InvokeResultEventsAsync(this, context, result); } } diff --git a/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs index 4866bd1da..eee0cba8f 100644 --- a/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs @@ -85,7 +85,7 @@ namespace Discord.Interactions /// protected override Task InvokeModuleEvent(IInteractionContext context, IResult result) - => CommandService._modalCommandExecutedEvent.InvokeAsync(this, context, result); + => CommandService.InvokeResultEventsAsync(this, context, result); /// protected override string GetLogString(IInteractionContext context) diff --git a/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs index a123ac183..5584f4676 100644 --- a/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/SlashCommandInfo.cs @@ -131,8 +131,8 @@ namespace Discord.Interactions return ParseResult.FromSuccess(readResult.Value); } - protected override Task InvokeModuleEvent (IInteractionContext context, IResult result) - => CommandService._slashCommandExecutedEvent.InvokeAsync(this, context, result); + protected override Task InvokeModuleEvent(IInteractionContext context, IResult result) + => CommandService.InvokeResultEventsAsync(this, context, result); protected override string GetLogString (IInteractionContext context) { diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index deb6fa931..a12349d7e 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -25,40 +25,46 @@ namespace Discord.Interactions internal readonly AsyncEvent> _logEvent = new (); /// + /// Occurs when any type of interaction is executed. + /// + public event Func InteractionExecuted { add { _interactionExecutedEvent.Add(value); } remove { _interactionExecutedEvent.Remove(value); } } + private readonly AsyncEvent> _interactionExecutedEvent = new(); + + /// /// Occurs when a Slash Command is executed. /// public event Func SlashCommandExecuted { add { _slashCommandExecutedEvent.Add(value); } remove { _slashCommandExecutedEvent.Remove(value); } } - internal readonly AsyncEvent> _slashCommandExecutedEvent = new (); + private readonly AsyncEvent> _slashCommandExecutedEvent = new (); /// /// Occurs when a Context Command is executed. /// public event Func ContextCommandExecuted { add { _contextCommandExecutedEvent.Add(value); } remove { _contextCommandExecutedEvent.Remove(value); } } - internal readonly AsyncEvent> _contextCommandExecutedEvent = new (); + private readonly AsyncEvent> _contextCommandExecutedEvent = new (); /// /// Occurs when a Message Component command is executed. /// public event Func ComponentCommandExecuted { add { _componentCommandExecutedEvent.Add(value); } remove { _componentCommandExecutedEvent.Remove(value); } } - internal readonly AsyncEvent> _componentCommandExecutedEvent = new (); + private readonly AsyncEvent> _componentCommandExecutedEvent = new (); /// /// Occurs when a Autocomplete command is executed. /// public event Func AutocompleteCommandExecuted { add { _autocompleteCommandExecutedEvent.Add(value); } remove { _autocompleteCommandExecutedEvent.Remove(value); } } - internal readonly AsyncEvent> _autocompleteCommandExecutedEvent = new(); + private readonly AsyncEvent> _autocompleteCommandExecutedEvent = new(); /// /// Occurs when a AutocompleteHandler is executed. /// public event Func AutocompleteHandlerExecuted { add { _autocompleteHandlerExecutedEvent.Add(value); } remove { _autocompleteHandlerExecutedEvent.Remove(value); } } - internal readonly AsyncEvent> _autocompleteHandlerExecutedEvent = new(); + private readonly AsyncEvent> _autocompleteHandlerExecutedEvent = new(); /// /// Occurs when a Modal command is executed. /// public event Func ModalCommandExecuted { add { _modalCommandExecutedEvent.Add(value); } remove { _modalCommandExecutedEvent.Remove(value); } } - internal readonly AsyncEvent> _modalCommandExecutedEvent = new(); + private readonly AsyncEvent> _modalCommandExecutedEvent = new(); private readonly ConcurrentDictionary _typedModuleDefs; private readonly CommandMap _slashCommandMap; @@ -719,6 +725,7 @@ namespace Discord.Interactions await _cmdLogger.DebugAsync($"Unknown slash command, skipping execution ({string.Join(" ", keywords).ToUpper()})"); await _slashCommandExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false); + await InvokeResultEventsAsync(null, context, result).ConfigureAwait(false); return result; } return await result.Command.ExecuteAsync(context, services).ConfigureAwait(false); @@ -736,6 +743,7 @@ namespace Discord.Interactions await _cmdLogger.DebugAsync($"Unknown context command, skipping execution ({result.Text.ToUpper()})"); await _contextCommandExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false); + await InvokeResultEventsAsync(null, context, result).ConfigureAwait(false); return result; } return await result.Command.ExecuteAsync(context, services).ConfigureAwait(false); @@ -750,6 +758,7 @@ namespace Discord.Interactions await _cmdLogger.DebugAsync($"Unknown custom interaction id, skipping execution ({input.ToUpper()})"); await _componentCommandExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false); + await InvokeResultEventsAsync(null, context, result).ConfigureAwait(false); return result; } return await result.Command.ExecuteAsync(context, services, result.RegexCaptureGroups).ConfigureAwait(false); @@ -779,6 +788,7 @@ namespace Discord.Interactions await _cmdLogger.DebugAsync($"Unknown command name, skipping autocomplete process ({interaction.Data.CommandName.ToUpper()})"); await _autocompleteCommandExecutedEvent.InvokeAsync(null, context, commandResult).ConfigureAwait(false); + await InvokeResultEventsAsync(null, context, commandResult).ConfigureAwait(false); return commandResult; } @@ -794,11 +804,37 @@ namespace Discord.Interactions await _cmdLogger.DebugAsync($"Unknown custom interaction id, skipping execution ({input.ToUpper()})"); await _componentCommandExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false); + await InvokeResultEventsAsync(null, context, result).ConfigureAwait(false); return result; } return await result.Command.ExecuteAsync(context, services, result.RegexCaptureGroups).ConfigureAwait(false); } + internal async Task InvokeResultEventsAsync(ICommandInfo info, IInteractionContext context, IResult result) + { + await _interactionExecutedEvent.InvokeAsync(null, context, result).ConfigureAwait(false); + if (info is not null) + { + switch (info) + { + case SlashCommandInfo slashCmd: + await _slashCommandExecutedEvent.InvokeAsync(slashCmd, context, result).ConfigureAwait(false); + return; + case ContextCommandInfo ctxCmd: + await _contextCommandExecutedEvent.InvokeAsync(ctxCmd, context, result).ConfigureAwait(false); + return; + case ComponentCommandInfo component: + await _componentCommandExecutedEvent.InvokeAsync(component, context, result).ConfigureAwait(false); + return; + case AutocompleteCommandInfo autocomplete: + await _autocompleteCommandExecutedEvent.InvokeAsync(autocomplete, context, result).ConfigureAwait(false); + return; + default: + throw new InvalidOperationException("No event has been designated to this interaction result."); + } + } + } + internal TypeConverter GetTypeConverter(Type type, IServiceProvider services = null) => _typeConverterMap.Get(type, services);