Skip to content

Commit

Permalink
[Featured] Consider AutoServiceScopes in autocomplete handler execu…
Browse files Browse the repository at this point in the history
…tions (#3068)

* Add AutoServiceScopes to autocomplete handlers

* Fix disposing behavior

* Update AutocompleteHandler.cs

---------

Co-authored-by: Mihail Gribkov <[email protected]>
  • Loading branch information
minisbett and Misha-133 authored Mar 1, 2025
1 parent 5f6c26b commit 7fb537f
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace Discord.Interactions
{
Expand All @@ -29,6 +30,9 @@ protected virtual string GetLogString(IInteractionContext context)
public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
IServiceProvider services)
{
using IServiceScope scope = InteractionService._autoServiceScopes ? services?.CreateScope() : null;
services = InteractionService._autoServiceScopes ? scope?.ServiceProvider ?? EmptyServiceProvider.Instance : services;

switch (InteractionService._runMode)
{
case RunMode.Sync:
Expand All @@ -44,7 +48,7 @@ public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInte
default:
throw new InvalidOperationException($"RunMode {InteractionService._runMode} is not supported.");
}

return Task.FromResult((IResult)ExecuteResult.FromSuccess());
}

Expand Down

0 comments on commit 7fb537f

Please sign in to comment.