#Slash Command Pre Execution Check Attribute Dependency Injection

1 messages · Page 1 of 1 (latest)

summer sundial
#

Is it possible to get Dependency Injection Services in a SlashCheckBaseAttribute?

arctic herald
#

using the service provider from the context, yes

summer sundial
#

@arctic herald This is the code I was trying to use and it wasn't getting the IUserRepository at all:

{
    public IUserRepository _userRepository { private get; set; } // The get accessor is optionally public, but the set accessor must be public.

    public override async Task<bool> ExecuteChecksAsync(InteractionContext ctx)
    {
        //return ctx.User.Id == 337045211362623493;

        var isAdminUser = await _userRepository.IsAdminUser(ctx.User.Id);
        return isAdminUser;
    }
}```
arctic herald
#

use the service provider from the context

summer sundial
#

OH!... Do you mean something like this:
ctx.Services.GetService<IUserRepository>();

arctic herald
#

something along those lines, yes

summer sundial
#

Okay! Thank you for the help!!

#

@arctic herald Slight side question about the Pre Execution Attributes, are they supposed to block the code execution of the command until the attribute completes?

arctic herald
#

yes