diff --git a/docs/guides/getting_started/samples/intro/structure.cs b/docs/guides/getting_started/samples/intro/structure.cs index e61016d33..9ba728133 100644 --- a/docs/guides/getting_started/samples/intro/structure.cs +++ b/docs/guides/getting_started/samples/intro/structure.cs @@ -164,7 +164,9 @@ class Program var result = await _commands.ExecuteAsync(context, pos); // Uncomment the following lines if you want the bot - // to send a message if it failed (not advised for most situations). + // to send a message if it failed. + // This does not catch errors from commands with 'RunMode.Async', + // subscribe a handler for '_commands.CommandExecuted' to see those. //if (!result.IsSuccess && result.Error != CommandError.UnknownCommand) // await msg.Channel.SendMessageAsync(result.ErrorReason); } diff --git a/src/Discord.Net.Commands/Builders/ModuleBuilder.cs b/src/Discord.Net.Commands/Builders/ModuleBuilder.cs index 5a3f3a8ca..24a6477ee 100644 --- a/src/Discord.Net.Commands/Builders/ModuleBuilder.cs +++ b/src/Discord.Net.Commands/Builders/ModuleBuilder.cs @@ -122,8 +122,6 @@ namespace Discord.Commands.Builders if (TypeInfo != null) { - //keep this for safety? - //services = services ?? EmptyServiceProvider.Instance; try { var moduleInstance = ReflectionUtils.CreateObject(TypeInfo, service, service._serviceProvider); diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index b213971cd..c7d3e4011 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -43,7 +43,10 @@ namespace Discord.Commands public CommandService() : this(new CommandServiceConfig()) { } public CommandService(CommandServiceConfig config) { - _serviceProvider = config.ServiceProvider ?? EmptyServiceProvider.Instance; + _serviceProvider = config.ServiceProvider + ?? config.ServiceProviderFactory?.Invoke(this) + ?? EmptyServiceProvider.Instance; + _caseSensitive = config.CaseSensitiveCommands; _throwOnError = config.ThrowOnError; _ignoreExtraArgs = config.IgnoreExtraArgs; @@ -91,7 +94,6 @@ namespace Discord.Commands var module = builder.Build(this, null); - //should be fine to pass null here since it'll never get checked from this path anyway return LoadModuleInternal(module); } finally @@ -149,22 +151,6 @@ namespace Discord.Commands { _moduleDefs.Add(module); - //if (module.TypeInfo.IsSpecified) - //{ - // //keep this for safety? - // services = services ?? EmptyServiceProvider.Instance; - // try - // { - // var moduleInstance = ReflectionUtils.CreateObject(module.TypeInfo.Value, this, services); - // moduleInstance.OnModuleBuilding(this); - // } - // catch(Exception) - // { - // //unsure of what to do here - // throw; - // } - //} - foreach (var command in module.Commands) _map.AddCommand(command); diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs index c7157cf51..c33fbd180 100644 --- a/src/Discord.Net.Commands/CommandServiceConfig.cs +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -21,6 +21,8 @@ namespace Discord.Commands /// Determines whether extra parameters should be ignored. public bool IgnoreExtraArgs { get; set; } = false; - public IServiceProvider ServiceProvider { get; set; } = EmptyServiceProvider.Instance; + public IServiceProvider ServiceProvider { get; set; } + + public Func ServiceProviderFactory { get; set; } } }