Class CommandService
Provides a framework for building Discord commands.
public class CommandService : IDisposable- Inheritance
- 
      
      CommandService
- Implements
- Inherited Members
- Extension Methods
Remarks
The service provides a framework for building Discord commands both dynamically via runtime builders or statically via compile-time modules. To create a command module at compile-time, see ModuleBase (most common); otherwise, see ModuleBuilder.
This service also provides several events for monitoring command usages; such as Log for any command-related log events, and CommandExecuted for information about commands that have been successfully executed.
Constructors
CommandService()
Initializes a new CommandService class.
public CommandService()CommandService(CommandServiceConfig)
Initializes a new CommandService class with the provided configuration.
public CommandService(CommandServiceConfig config)Parameters
- configCommandServiceConfig
- The configuration class. 
Exceptions
Properties
Commands
Represents all commands loaded within CommandService.
public IEnumerable<CommandInfo> Commands { get; }Property Value
Modules
Represents all modules loaded within CommandService.
public IEnumerable<ModuleInfo> Modules { get; }Property Value
TypeReaders
Represents all TypeReader loaded within CommandService.
public ILookup<Type, TypeReader> TypeReaders { get; }Property Value
Methods
AddModuleAsync(Type, IServiceProvider)
Adds a command module from a Type.
public Task<ModuleInfo> AddModuleAsync(Type type, IServiceProvider services)Parameters
- typeType
- The type of module. 
- servicesIServiceProvider
- The IServiceProvider for your dependency injection solution if using one; otherwise, pass null . 
Returns
- Task<ModuleInfo>
- A task that represents the asynchronous operation for adding the module. The task result contains the built module. 
Exceptions
- ArgumentException
- This module has already been added. 
- InvalidOperationException
- The ModuleInfo fails to be built; an invalid type may have been provided. 
AddModuleAsync<T>(IServiceProvider)
Add a command module from a Type.
public Task<ModuleInfo> AddModuleAsync<T>(IServiceProvider services)Parameters
- servicesIServiceProvider
- The IServiceProvider for your dependency injection solution if using one; otherwise, pass null. 
Returns
- Task<ModuleInfo>
- A task that represents the asynchronous operation for adding the module. The task result contains the built module. 
Type Parameters
- T
- The type of module. 
Examples
The following example registers the module MyModule to commandService.
await commandService.AddModuleAsync<MyModule>(serviceProvider);Exceptions
- ArgumentException
- This module has already been added. 
- InvalidOperationException
- The ModuleInfo fails to be built; an invalid type may have been provided. 
AddModulesAsync(Assembly, IServiceProvider)
Add command modules from an Assembly.
public Task<IEnumerable<ModuleInfo>> AddModulesAsync(Assembly assembly, IServiceProvider services)Parameters
- assemblyAssembly
- The Assembly containing command modules. 
- servicesIServiceProvider
- The IServiceProvider for your dependency injection solution if using one; otherwise, pass null. 
Returns
- Task<IEnumerable<ModuleInfo>>
- A task that represents the asynchronous operation for adding the command modules. The task result contains an enumerable collection of modules added. 
AddTypeReader(Type, TypeReader)
Adds a custom TypeReader to this CommandService for the supplied object
type.
If type is a ValueType, a nullable TypeReader for the
value type will also be added.
If a default TypeReader exists for type, a warning will be logged and
the default TypeReader will be replaced.
public void AddTypeReader(Type type, TypeReader reader)Parameters
- typeType
- A Type instance for the type to be read. 
- readerTypeReader
- An instance of the TypeReader to be added. 
AddTypeReader(Type, TypeReader, bool)
Adds a custom TypeReader to this CommandService for the supplied object
type.
If type is a ValueType, a nullable TypeReader for the
value type will also be added.
public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault)Parameters
- typeType
- A Type instance for the type to be read. 
- readerTypeReader
- An instance of the TypeReader to be added. 
- replaceDefaultbool
- Defines whether the TypeReader should replace the default one for Type if it exists. 
AddTypeReader<T>(TypeReader)
Adds a custom TypeReader to this CommandService for the supplied object
type.
If T is a ValueType, a nullable TypeReader will
also be added.
If a default TypeReader exists for T, a warning will be logged
and the default TypeReader will be replaced.
public void AddTypeReader<T>(TypeReader reader)Parameters
- readerTypeReader
- An instance of the TypeReader to be added. 
Type Parameters
- T
- The object type to be read by the TypeReader. 
AddTypeReader<T>(TypeReader, bool)
Adds a custom TypeReader to this CommandService for the supplied object
type.
If T is a ValueType, a nullable TypeReader will
also be added.
public void AddTypeReader<T>(TypeReader reader, bool replaceDefault)Parameters
- readerTypeReader
- An instance of the TypeReader to be added. 
- replaceDefaultbool
- Defines whether the TypeReader should replace the default one for Type if it exists. 
Type Parameters
- T
- The object type to be read by the TypeReader. 
CreateModuleAsync(string, Action<ModuleBuilder>)
public Task<ModuleInfo> CreateModuleAsync(string primaryAlias, Action<ModuleBuilder> buildFunc)Parameters
- primaryAliasstring
- buildFuncAction<ModuleBuilder>
Returns
Dispose(bool)
protected virtual void Dispose(bool disposing)Parameters
- disposingbool
ExecuteAsync(ICommandContext, int, IServiceProvider, MultiMatchHandling)
Executes the command.
public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)Parameters
- contextICommandContext
- The context of the command. 
- argPosint
- The position of which the command starts at. 
- servicesIServiceProvider
- The service to be used in the command's dependency injection. 
- multiMatchHandlingMultiMatchHandling
- The handling mode when multiple command matches are found. 
Returns
- Task<IResult>
- A task that represents the asynchronous execution operation. The task result contains the result of the command execution. 
ExecuteAsync(ICommandContext, string, IServiceProvider, MultiMatchHandling)
Executes the command.
public Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)Parameters
- contextICommandContext
- The context of the command. 
- inputstring
- The command string. 
- servicesIServiceProvider
- The service to be used in the command's dependency injection. 
- multiMatchHandlingMultiMatchHandling
- The handling mode when multiple command matches are found. 
Returns
- Task<IResult>
- A task that represents the asynchronous execution operation. The task result contains the result of the command execution. 
RemoveModuleAsync(ModuleInfo)
Removes the command module.
public Task<bool> RemoveModuleAsync(ModuleInfo module)Parameters
- moduleModuleInfo
- The ModuleInfo to be removed from the service. 
Returns
- Task<bool>
- A task that represents the asynchronous removal operation. The task result contains a value that indicates whether the - moduleis successfully removed.
RemoveModuleAsync(Type)
Removes the command module.
public Task<bool> RemoveModuleAsync(Type type)Parameters
Returns
- Task<bool>
- A task that represents the asynchronous removal operation. The task result contains a value that indicates whether the module is successfully removed. 
RemoveModuleAsync<T>()
Removes the command module.
public Task<bool> RemoveModuleAsync<T>()Returns
- Task<bool>
- A task that represents the asynchronous removal operation. The task result contains a value that indicates whether the module is successfully removed. 
Type Parameters
- T
- The Type of the module. 
Search(ICommandContext, int)
Searches for the command.
public SearchResult Search(ICommandContext context, int argPos)Parameters
- contextICommandContext
- The context of the command. 
- argPosint
- The position of which the command starts at. 
Returns
- SearchResult
- The result containing the matching commands. 
Search(ICommandContext, string)
Searches for the command.
public SearchResult Search(ICommandContext context, string input)Parameters
- contextICommandContext
- The context of the command. 
- inputstring
- The command string. 
Returns
- SearchResult
- The result containing the matching commands. 
Search(string)
public SearchResult Search(string input)Parameters
- inputstring
Returns
TryRemoveTypeReader(Type, bool, out IDictionary<Type, TypeReader>)
Removes a type reader from the list of type readers.
public bool TryRemoveTypeReader(Type type, bool isDefaultTypeReader, out IDictionary<Type, TypeReader> readers)Parameters
- typeType
- The type to remove the readers from. 
- isDefaultTypeReaderbool
- true if the default readers for - typeshould be removed; otherwise false.
- readersIDictionary<Type, TypeReader>
- The removed collection of type readers. 
Returns
Remarks
Removing a TypeReader from the CommandService will not dereference the TypeReader from the loaded module/command instances. You need to reload the modules for the changes to take effect.
ValidateAndGetBestMatch(SearchResult, ICommandContext, IServiceProvider, MultiMatchHandling)
Validates and gets the best CommandMatch from a specified SearchResult
public Task<IResult> ValidateAndGetBestMatch(SearchResult matches, ICommandContext context, IServiceProvider provider, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)Parameters
- matchesSearchResult
- The SearchResult. 
- contextICommandContext
- The context of the command. 
- providerIServiceProvider
- The service provider to be used on the command's dependency injection. 
- multiMatchHandlingMultiMatchHandling
- The handling mode when multiple command matches are found. 
Returns
- Task<IResult>
- A task that represents the asynchronous validation operation. The task result contains the result of the command validation as a MatchResult or a SearchResult if no matches were found. 
Events
CommandExecuted
Occurs when a command is executed.
public event Func<Optional<CommandInfo>, ICommandContext, IResult, Task> CommandExecutedEvent Type
Remarks
This event is fired when a command has been executed, successfully or not. When a command fails to execute during parsing or precondition stage, the CommandInfo may not be returned.
Log
Occurs when a command-related information is received.
public event Func<LogMessage, Task> Log