Wolfringo Wolfringo
Wolfringo Wolfringo
Wolfringo (c) 2020 TehGM
DocFX, DiscordFX theme.
Search Results for

    Customizing Wolfringo

    Wolfringo is designed with customizability from ground up. It achieves it mainly thanks to heavy use of Abstractions and Dependency Injection. Constructors of both WolfClient and CommandsService take an IServiceProvider to resolve their dependencies, and WolfClientBuilder and CommandsServiceBuilder have special methods for registering/overwriting services. This allows you to replace most components of Wolfringo without having to clone and recompile it yourself!

    Check navigation menu for guides how to customize specific parts of Wolfringo.

    • Without Wolfringo.Hosting (Normal Bot)
    • With Wolfringo.Hosting (.NET Generic Host/ASP.NET Core)

    Replacing WolfClient dependencies

    To replace any of WolfClient dependencies, use With... commands on the builder.

    _client = new WolfClientBuilder()
        .WithResponseTypeResolver<MyResponseTypeResolver>()
        // alternatively, factory and instance patterns are also allowed
        //.WithResponseTypeResolver(provider => new MyResponseTypeResolver())
        //.WithResponseTypeResolver(_responseTypeResolver)
        .Build();
    

    Replacing CommandsService dependencies

    To replace any of CommandsService dependencies, use With... commands on the builder.

    _client = new WolfClientBuilder()
        // ... client config ...
        .WithCommands(commands =>
        {
            commands
                .WithArgumentsParser<MyArgumentsParser>()
                // alternatively, factory and instance patterns are also allowed
                //.WithArgumentsParser(provider => new MyArgumentsParser())
                //.WithArgumentsParser(precreatedCustomResolver)
        })
        .Build();
    

    To replace any interface of Wolfringo, simply register it as you would register any other service. Wolfringo should automatically pick it up.

    services.AddTransient<IResponseTypeResolver, MyResponseTypeResolver>();
    services.AddTransient<IArgumentsParser, MyArgumentsParser>();
    

    Refer to Dependency injection in .NET and Dependency injection in ASP.NET Core for more information about Dependency Injection in .NET Generic Host/ASP.NET Core.

    Tip

    Check WolfClientServiceCollectionExtensions and CommandsServiceCollectionExtensions to check what lifetimes the services are registered with by default.

    IDisposable services

    Your custom service can implement @System.IDisposable - both WolfClient and CommandsService handle disposable services depending on how they were registered.

    Service will be automatically disposed for you when WolfClient/CommandsService is disposing service was registered by either:

    • using generic method (WithResponseTypeResolver<MyResponseTypeResolver>();
    • using factory method (WithResponseTypeResolver(provider => new MyResponseTypeResolver());
    • using Wolfringo.Hosting.

    If service was registered using concrete type, or you didn't use WolfClientBuilder or CommandsServiceBuilder and opted to use constructors directly, you'll need to dispose these services manually.
    This is because Wolfringo has no way to know whether you're using these services in other places - disposing them in such case would break your code.