nefarius / Nefarius.DSharpPlus.Extensions.Hosting

Glues .NET Core Hosting and DSharpPlus together for use with DI.
https://discord.nefarius.at
MIT License
15 stars 7 forks source link

I'm getting an error, can you help me with this? #195

Open detmach opened 8 months ago

detmach commented 8 months ago

I want to both send commands and run existing ones with the help of a web api.

BaseCommandModule or ApplicationCommandModule are among the ones I inherited

var roblox = ctx.Services.GetRequiredService< IRobloxCommands>();

This is the error I get when I call

System.InvalidOperationException: 'Cannot resolve scoped service 'MertBotSystem.RobloxRepo.IRobloxCommands' from root provider.'

This is the starting code

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddScoped<IRobloxCommands, RobloxCommands>();

builder.Services.AddSingleton<MusicModule>();
builder.Services.AddSingleton<RobloxModule>();
builder.Services.AddSingleton<SlashRobloxModule>();
builder.Services.AddDiscord(options =>
{
#if DEBUG
    options.Token = "xxx";

#else
                options.Token = "xxx";

#endif
    options.TokenType = TokenType.Bot;
    options.Intents = DiscordIntents.All;
    options.AutoReconnect = true;
    options.MinimumLogLevel = LogLevel.Debug;
    options.LogUnknownEvents = true;
    var client = new DiscordClient(options);
});
builder.Services.AddDiscordCommandsNext(options =>
{
    options.StringPrefixes = new[] { "!" };
    options.EnableDms = false;
    options.EnableMentionPrefix = true;

}, extension =>
{
    extension.RegisterCommands<RobloxModule>();
});
builder.Services.AddDiscordSlashCommands(options =>
{

}, extension =>
{
    extension.RegisterCommands<SlashRobloxModule>();
});
builder.Services.AddDiscordInteractivity(options =>
{
    options.PaginationBehaviour = PaginationBehaviour.WrapAround;
    options.ResponseBehavior = InteractionResponseBehavior.Ack;
    options.ResponseMessage = "That's not a valid button";
    options.Timeout = TimeSpan.FromMinutes(2);
});
builder.Services.AddDiscordGuildAvailableEventSubscriber<BotModuleForGuildAndMemberEvents>();
builder.Services.AddDiscordGuildMemberAddedEventSubscriber<BotModuleForGuildAndMemberEvents>();
builder.Services.AddDiscordComponentInteractionCreatedEventSubscriber<BotModuleForMiscEvents>();
builder.Services.AddDiscordClientErroredEventSubscriber<BotModuleForMiscEvents>();
nefarius commented 8 months ago

You are not creating a scope. Either make the service Transient or wrap the GetRequired... into a scope and it will work.

nefarius commented 8 months ago

Also this line

var client = new DiscordClient(options)

makes no sense, remove it.

detmach commented 8 months ago

Also this line

var client = new DiscordClient(options)

makes no sense, remove it.

yes is removed it

detmach commented 8 months ago

You are not creating a scope. Either make the service Transient or wrap the GetRequired... into a scope and it will work.

builder.Services.AddTransient<IRobloxCommands, RobloxCommands>();

image

System.InvalidOperationException: 'Cannot resolve scoped service 'MertBotSystem.RobloxRepo.IRobloxCommands' from root provider.'
nefarius commented 8 months ago

This doesn't tell enough to debug, pls either share the entire project or the complete involved classes, how they're registered etc.

detmach commented 8 months ago

sendmail nefarius@dhmx.at

nefarius commented 8 months ago

sendmail nefarius@dhmx.at

Received, I'll check it out when I got some time.