Discord-Net-Labs/Discord.Net-Labs

Optional service scope creation in Interaction framework

kuru-rin opened this issue · 1 comments

I am currently busy converting my commands to interactions, but I am having one issue with using the interaction framework.

I have found the problem for me is that it is creating a new service scope when executing the interaction handler.

protected async Task<IResult> RunAsync(IInteractionContext context, object[] args, IServiceProvider services)
{
    switch (RunMode)
    {
        case RunMode.Sync:
            {
                using var scope = services?.CreateScope(); // here
                return await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false);
            }
        case RunMode.Async:
            _ = Task.Run(async () =>
            {
                using var scope = services?.CreateScope(); // and here
                await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false);
            });
            break;
        default:
            throw new InvalidOperationException($"RunMode {RunMode} is not supported.");
    }

    return ExecuteResult.FromSuccess();
}

This would clear out any state that I have given it prior to executing the interaction,
in my case I am creating a service scope to provide a context to all commands (for guild config, language, etc).

Is there any chance we can make the services.CreateScope in this snippet optional?