[Featured] Consider AutoServiceScopes in autocomplete handler executions (#3068)

* Add AutoServiceScopes to autocomplete handlers

* Fix disposing behavior

* Update AutocompleteHandler.cs

---------

Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>
This commit is contained in:
mini
2025-03-01 21:52:41 +01:00
committed by GitHub
parent 5f6c26bf9c
commit 7fb537fd72

View File

@@ -4,6 +4,7 @@ using System;
using System.Reflection; using System.Reflection;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Discord.Interactions namespace Discord.Interactions
{ {
@@ -29,6 +30,9 @@ namespace Discord.Interactions
public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
IServiceProvider services) IServiceProvider services)
{ {
using IServiceScope scope = InteractionService._autoServiceScopes ? services?.CreateScope() : null;
services = InteractionService._autoServiceScopes ? scope?.ServiceProvider ?? EmptyServiceProvider.Instance : services;
switch (InteractionService._runMode) switch (InteractionService._runMode)
{ {
case RunMode.Sync: case RunMode.Sync: