Skip to content

Commit

Permalink
AddModule(s)Async should be explicit about IServiceProvider
Browse files Browse the repository at this point in the history
In f19730e, AddModule(s)Async was changed so that the IServiceProvider
was optional, both at compile time and runtime. This had the side effect
of meaning that there was no longer a compile-time hint that users would
need to pass an IServiceProvider to AddModulesAsync. I assumed this
would not be an issue - users would recognize the runtime exception here
and self correct - but activity in our Discord support channel would
indicate otherwise.

We now require the user to explicitly opt-out of dependency injection -
they are still free to pass null in place of an IServiceProvider if they
do not intend to use one, and the library will handle this at runtime.
  • Loading branch information
foxbot committed Mar 18, 2018
1 parent fc5e70c commit b70ae41
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Discord.Net.Commands/CommandService.cs
Expand Up @@ -95,8 +95,15 @@ public async Task<ModuleInfo> CreateModuleAsync(string primaryAlias, Action<Modu
_moduleLock.Release();
}
}
public Task<ModuleInfo> AddModuleAsync<T>(IServiceProvider services = null) => AddModuleAsync(typeof(T), services);
public async Task<ModuleInfo> AddModuleAsync(Type type, IServiceProvider services = null)

/// <summary>
/// Add a command module from a type
/// </summary>
/// <typeparam name="T">The type of module</typeparam>
/// <param name="services">An IServiceProvider for your dependency injection solution, if using one - otherwise, pass null</param>
/// <returns>A built module</returns>
public Task<ModuleInfo> AddModuleAsync<T>(IServiceProvider services) => AddModuleAsync(typeof(T), services);
public async Task<ModuleInfo> AddModuleAsync(Type type, IServiceProvider services)
{
services = services ?? EmptyServiceProvider.Instance;

Expand All @@ -122,7 +129,13 @@ public async Task<ModuleInfo> AddModuleAsync(Type type, IServiceProvider service
_moduleLock.Release();
}
}
public async Task<IEnumerable<ModuleInfo>> AddModulesAsync(Assembly assembly, IServiceProvider services = null)
/// <summary>
/// Add command modules from an assembly
/// </summary>
/// <param name="assembly">The assembly containing command modules</param>
/// <param name="services">An IServiceProvider for your dependency injection solution, if using one - otherwise, pass null</param>
/// <returns>A collection of built modules</returns>
public async Task<IEnumerable<ModuleInfo>> AddModulesAsync(Assembly assembly, IServiceProvider services)
{
services = services ?? EmptyServiceProvider.Instance;

Expand Down Expand Up @@ -278,9 +291,9 @@ public SearchResult Search(ICommandContext context, string input)
return SearchResult.FromError(CommandError.UnknownCommand, "Unknown command.");
}

public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
=> ExecuteAsync(context, context.Message.Content.Substring(argPos), services, multiMatchHandling);
public async Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services = null, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
public async Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
{
services = services ?? EmptyServiceProvider.Instance;

Expand Down

0 comments on commit b70ae41

Please sign in to comment.