/// <summary>Initializes a command service.</summary>
        /// <param name="client">WOLF client. Required.</param>
        /// <param name="options">Commands options that will be used as default when running a command. Required.</param>
        /// <param name="services">Services provider that will be used by all commands. Null will cause a backup provider to be used.</param>
        /// <param name="log">Logger to log messages and errors to. If null, all logging will be disabled.</param>
        /// <param name="cancellationToken">Cancellation token that can be used for cancelling all tasks.</param>
        public CommandsService(IWolfClient client, CommandsOptions options, ILogger log, IServiceProvider services = null, CancellationToken cancellationToken = default)
        {
            // init private
            this._commands           = new Dictionary <ICommandInstanceDescriptor, ICommandInstance>();
            this._lock               = new SemaphoreSlim(1, 1);
            this._cts                = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
            this._disposableServices = new List <IDisposable>(2);
            this._started            = false;

            // init required
            this._client  = client ?? services?.GetService <IWolfClient>() ?? throw new ArgumentNullException(nameof(client));
            this._options = options ?? services?.GetService <CommandsOptions>() ?? throw new ArgumentNullException(nameof(options));

            // init optionals
            this._log = log ?? services?.GetService <ILogger <CommandsService> >() ?? services?.GetService <ILogger <ICommandsService> >() ?? services.GetService <ILogger>();
            this._argumentConverterProvider = services?.GetService <IArgumentConverterProvider>() ?? CreateAsDisposable <ArgumentConverterProvider>();
            this._handlerProvider           = services?.GetService <ICommandsHandlerProvider>() ?? CreateAsDisposable <CommandsHandlerProvider>();
            this._argumentsParser           = services?.GetService <IArgumentsParser>() ?? new ArgumentsParser();
            this._parameterBuilder          = services?.GetService <IParameterBuilder>() ?? new ParameterBuilder();
            this._initializers   = services?.GetService <ICommandInitializerProvider>() ?? new CommandInitializerProvider();
            this._commandsLoader = services?.GetService <ICommandsLoader>() ?? new CommandsLoader(this._initializers, this._log);

            // init service provider - use combine, to use fallback one as well
            this._fallbackServices = this.CreateFallbackServiceProvider();
            this._services         = CombinedServiceProvider.Combine(services, this._fallbackServices);

            // register event handlers
            this._client.AddMessageListener <ChatMessage>(OnMessageReceived);
        }
Exemple #2
0
 /// <summary>Creates a new loader instance.</summary>
 /// <param name="initializers">Command initializers mapping.</param>
 /// <param name="log">Logger to log messages and errors to. If null, all logging will be disabled.</param>
 public CommandsLoader(ICommandInitializerProvider initializers, ILogger log)
 {
     this._initializers = initializers;
     this._log          = log;
 }
 /// <summary>Gets a command initializer for the command type.</summary>
 /// <typeparam name="T">Type of the command.</typeparam>
 /// <returns>An initializer for provided command type.</returns>
 public static ICommandInitializer GetMappedInitializer <T>(this ICommandInitializerProvider map) where T : CommandAttributeBase
 => map.GetInitializer(typeof(T));