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
config
CommandServiceConfigThe 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
type
TypeThe type of module.
services
IServiceProviderThe 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
services
IServiceProviderThe 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
assembly
AssemblyThe Assembly containing command modules.
services
IServiceProviderThe 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
type
TypeA Type instance for the type to be read.
reader
TypeReaderAn 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
type
TypeA Type instance for the type to be read.
reader
TypeReaderAn instance of the TypeReader to be added.
replaceDefault
boolDefines 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
reader
TypeReaderAn 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
reader
TypeReaderAn instance of the TypeReader to be added.
replaceDefault
boolDefines 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
primaryAlias
stringbuildFunc
Action<ModuleBuilder>
Returns
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
ExecuteAsync(ICommandContext, int, IServiceProvider, MultiMatchHandling)
Executes the command.
public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Parameters
context
ICommandContextThe context of the command.
argPos
intThe position of which the command starts at.
services
IServiceProviderThe service to be used in the command's dependency injection.
multiMatchHandling
MultiMatchHandlingThe 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
context
ICommandContextThe context of the command.
input
stringThe command string.
services
IServiceProviderThe service to be used in the command's dependency injection.
multiMatchHandling
MultiMatchHandlingThe 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
module
ModuleInfoThe 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
module
is 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
context
ICommandContextThe context of the command.
argPos
intThe 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
context
ICommandContextThe context of the command.
input
stringThe command string.
Returns
- SearchResult
The result containing the matching commands.
Search(string)
public SearchResult Search(string input)
Parameters
input
string
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
type
TypeThe type to remove the readers from.
isDefaultTypeReader
booltrue if the default readers for
type
should be removed; otherwise false.readers
IDictionary<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
matches
SearchResultThe SearchResult.
context
ICommandContextThe context of the command.
provider
IServiceProviderThe service provider to be used on the command's dependency injection.
multiMatchHandling
MultiMatchHandlingThe 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> CommandExecuted
Event 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