/// <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);
        }
Example #2
0
        public CommandsHandler(ICommandMessageCleaner cleaner, ICommandParser commandParser, ICommandArgumentsParser argumentsParser, ICommandsLoader commandsLoader,
                               ICommandMethodExecuter methodExecuter, ICommandMethodsParser methodParser, ICommandsConfiguration configuration, IWrongUsageHandler wrongUsageHandler)
        {
            _cleaner           = cleaner;
            _commandParser     = commandParser;
            _argumentsParser   = argumentsParser;
            _methodExecuter    = methodExecuter;
            _methodParser      = methodParser;
            _configuration     = configuration;
            _commandsLoader    = commandsLoader;
            _wrongUsageHandler = wrongUsageHandler;

            Instance = this;
        }
Example #3
0
        /// <summary>Loads all command instance descriptors from methods.</summary>
        /// <param name="loader">The loader to perform loading with.</param>
        /// <param name="methods">Methods to load descriptors from.</param>
        /// <param name="cancellationToken">Token that can be used to stop loading.</param>
        /// <returns>Enumerable of all loaded command descriptors.</returns>
        public static async Task <IEnumerable <ICommandInstanceDescriptor> > LoadFromMethodsAsync(this ICommandsLoader loader, IEnumerable <MethodInfo> methods, CancellationToken cancellationToken = default)
        {
            List <ICommandInstanceDescriptor> results = new List <ICommandInstanceDescriptor>();

            foreach (MethodInfo method in methods)
            {
                results.AddRange(await loader.LoadFromMethodAsync(method, cancellationToken).ConfigureAwait(false));
            }
            return(results);
        }
Example #4
0
        /// <summary>Loads all command instance descriptors from types.</summary>
        /// <param name="loader">The loader to perform loading with.</param>
        /// <param name="types">Types to load descriptors from.</param>
        /// <param name="cancellationToken">Token that can be used to stop loading.</param>
        /// <returns>Enumerable of all loaded command descriptors.</returns>
        public static async Task <IEnumerable <ICommandInstanceDescriptor> > LoadFromTypesAsync(this ICommandsLoader loader, IEnumerable <TypeInfo> types, CancellationToken cancellationToken = default)
        {
            List <ICommandInstanceDescriptor> results = new List <ICommandInstanceDescriptor>();

            foreach (TypeInfo type in types)
            {
                results.AddRange(await loader.LoadFromTypeAsync(type, cancellationToken).ConfigureAwait(false));
            }
            return(results);
        }
Example #5
0
        /// <summary>Loads all command instance descriptors from assemblies.</summary>
        /// <param name="loader">The loader to perform loading with.</param>
        /// <param name="assemblies">Assemblies to load descriptors from.</param>
        /// <param name="cancellationToken">Token that can be used to stop loading.</param>
        /// <returns>Enumerable of all loaded command descriptors.</returns>
        public static async Task <IEnumerable <ICommandInstanceDescriptor> > LoadFromAssembliesAsync(this ICommandsLoader loader, IEnumerable <Assembly> assemblies, CancellationToken cancellationToken = default)
        {
            List <ICommandInstanceDescriptor> results = new List <ICommandInstanceDescriptor>();

            foreach (Assembly asm in assemblies)
            {
                results.AddRange(await loader.LoadFromAssemblyAsync(asm, cancellationToken).ConfigureAwait(false));
            }
            return(results);
        }