/// <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); }
/// <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 default to be used.</param> /// <param name="cancellationToken">Cancellation token that can be used for cancelling all tasks.</param> public CommandsService(IWolfClient client, CommandsOptions options, IServiceProvider services = null, CancellationToken cancellationToken = default) : this(client, options, null, services, cancellationToken) { }
/// <summary>Creates a command context.</summary> /// <param name="message">Chat message that triggered the command.</param> /// <param name="client">WOLF client that received the message.</param> /// <param name="options">Default options to use for processing the command.</param> public CommandContext(ChatMessage message, IWolfClient client, CommandsOptions options) { this.Message = message; this.Client = client; this.Options = options; }
/// <summary>Checks if the message matches prefix requirements for command processing.</summary> /// <param name="message">Message to check.</param> /// <param name="options">Commands options to perform the check with.</param> /// <param name="startIndex">Index of start of actual command message, without prefix.</param> /// <returns>True if the message matches prefix requirement; otherwise false.</returns> public static bool MatchesPrefixRequirement(this ChatMessage message, CommandsOptions options, out int startIndex) => MatchesPrefixRequirement(message, options.Prefix, options.RequirePrefix, options.CaseSensitivity, out startIndex);