Discord.Addons.Interactive icon indicating copy to clipboard operation
Discord.Addons.Interactive copied to clipboard

InteractiveService not found

Open LeytonV opened this issue 6 years ago • 11 comments

I added the NuGet package to my project and linked everything up, and upon trying to start the bot I get an error message saying "dependency"InteractiveService" was not found. I came over here to use the Github version, and the same issue came up. Is there any way to fix this?

LeytonV avatar Jun 20 '19 18:06 LeytonV

Same issue :/

alexlyee avatar Sep 13 '19 01:09 alexlyee

Hi there,

It is working for me. Maybe I can be of assistant.

How are you Linking it up?

Snowflakethepony avatar Sep 18 '19 15:09 Snowflakethepony

Simply adding an InteractiveService to the ServiceCollection, and then inheriting InteractiveBase in one of my command modules.

            _services = new ServiceCollection() // Microsoft.Extensions.DependencyInjection
                            .AddSingleton(_client)
                            .AddSingleton(_commands)
                            .AddSingleton<CommandHandler>();
                            //.AddSingleton<InteractiveService>()
using System;
using System.Threading.Tasks;
using Discord.Addons.Interactive;
using Discord.Commands;

// https://github.com/foxbot/Discord.Addons.Interactive/tree/master/SampleApp

namespace HSBot.Modules
{
    public class SampleModule : InteractiveBase
    {

alexlyee avatar Sep 22 '19 19:09 alexlyee

Okay, please try the following: _services = new ServiceCollection() // Microsoft.Extensions.DependencyInjection .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton<CommandHandler>(); .AddSingleton(new InteractiveService(_client))

Snowflakethepony avatar Sep 23 '19 20:09 Snowflakethepony

After some further poking around, I fixed my issue! I was simply using an IServiceCollection rather than an IServiceProvider. Thank you for helping me, I probably wouldn't have looked further if it weren't for it. :)

alexlyee avatar Sep 24 '19 23:09 alexlyee

Actually I spoke too soon, for some reason when I execute a command in a module that implements InteractiveBase, it spits out "Failed to create "HSBot.Modules.SampleModule", dependency "InteractiveService" was not found." I'm implementing it in my main like this:

            _services = new ServiceCollection() // Microsoft.Extensions.DependencyInjection
                            .AddSingleton(_client)
                            .AddSingleton(_commands)
                            .AddSingleton<CommandHandler>()
                            .AddSingleton<InteractiveService>()
                            .BuildServiceProvider();

alexlyee avatar Sep 25 '19 00:09 alexlyee

Arh yes, it has to be a ServiceProvider. Could you try building it like this:

_services = new ServiceCollection() // Microsoft.Extensions.DependencyInjection .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton<CommandHandler>() .AddSingleton(new InteractiveService(client)) .BuildServiceProvider();

Snowflakethepony avatar Sep 25 '19 16:09 Snowflakethepony

Correction for your client variable name (sorry):

_services = new ServiceCollection() // Microsoft.Extensions.DependencyInjection .AddSingleton(_client) .AddSingleton(_commands) .AddSingleton() .AddSingleton(new InteractiveService(_client)) .BuildServiceProvider();

Snowflakethepony avatar Sep 25 '19 16:09 Snowflakethepony

Having the same problem.

System.InvalidOperationException: 'Failed to create "DiscordBot.Modules.UtilityModule", dependency "InteractiveService" was not found.'

class Program
    {
        static void Main(string[] args)
            => new Program().MainAsync().GetAwaiter().GetResult();
private IServiceProvider ConfigureServices()
{
    return new ServiceCollection()
        // Base
        .AddSingleton(_client)
        .AddSingleton<CommandService>()
        .AddSingleton<CommandHandlingService>()
        .AddSingleton<InteractiveService>(new InteractiveService(_client))
        // Logging
        .AddLogging()
        .AddSingleton<LogService>()
        // Extra
        .AddSingleton(Config)
        // Add additional services here...
        .BuildServiceProvider();
}
...
using Discord;
using Discord.Commands;
using Discord.Addons.Interactive;
using Discord.WebSocket;

namespace DiscordBot.Modules
{
    [Name("Utility")]
    public class UtilityModule : InteractiveBase

Have also tried

.AddSingleton<InteractiveService>()

Project only compiles when I change UtilityModule.cs to inherit from ModuleBase<SocketCommandContext>, and that's not what I want to do.

Edit: and the sample project works perfectly, but I'm not able to find the discrepancy.

Edit2: Found the problem in CommandHandlingService.cs

public async Task InitializeAsync(IServiceProvider provider)
        {
            _provider = provider;
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), services: --->null<---);
            // Add additional initialization code here...
        }

slinkstr avatar Apr 18 '20 10:04 slinkstr

This issue still exists

Thomas-Valkenburg avatar May 24 '21 00:05 Thomas-Valkenburg

For all the people still having this issue I (might have) found a solution:

Be sure to not only add the services in AddModulesAsync, but also in the ExecuteAsync. LOL

_services = new ServiceCollection().
                AddSingleton(_client).
                AddSingleton<InteractiveService>().
                BuildServiceProvider();

CommandService.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: _services);
await CommandService.ExecuteAsync(context, argPos, _services);

Thomas-Valkenburg avatar May 25 '21 15:05 Thomas-Valkenburg