Skip to main content

Class CommandService

Provides a framework for building Discord commands.

Assembly: Discord.Net.Commands.dll​
View Source​
Declaration
public class CommandService : IDisposable

Properties​

Modules​

Represents all modules loaded within Discord.Commands.CommandService.

View Source​
Declaration
public IEnumerable<ModuleInfo> Modules { get; }

Commands​

Represents all commands loaded within Discord.Commands.CommandService.

View Source​
Declaration
public IEnumerable<CommandInfo> Commands { get; }

TypeReaders​

Represents all Discord.Commands.TypeReader loaded within Discord.Commands.CommandService.

View Source​
Declaration
public ILookup<Type, TypeReader> TypeReaders { get; }

Methods​

CreateModuleAsync(String, Action<ModuleBuilder>)​

View Source​
Declaration
public async Task<ModuleInfo> CreateModuleAsync(string primaryAlias, Action<ModuleBuilder> buildFunc)
Returns​

System.Threading.Tasks.Task<Discord.Commands.ModuleInfo>

Parameters​
TypeName
System.StringprimaryAlias
System.Action<Discord.Commands.Builders.ModuleBuilder>buildFunc

AddModuleAsync<T>(IServiceProvider)​

Add a command module from a System.Type.

View Source​
Declaration
public Task<ModuleInfo> AddModuleAsync<T>(IServiceProvider services)
Returns​

System.Threading.Tasks.Task<Discord.Commands.ModuleInfo>: A task that represents the asynchronous operation for adding the module. The task result contains the built module.

Parameters​
TypeNameDescription
System.IServiceProviderservicesThe System.IServiceProvider for your dependency injection solution if using one; otherwise, pass null.
Type Parameters​
NameDescription
TThe type of module.

AddModuleAsync(Type, IServiceProvider)​

Adds a command module from a System.Type.

View Source​
Declaration
public async Task<ModuleInfo> AddModuleAsync(Type type, IServiceProvider services)
Returns​

System.Threading.Tasks.Task<Discord.Commands.ModuleInfo>: A task that represents the asynchronous operation for adding the module. The task result contains the built module.

Parameters​
TypeNameDescription
System.TypetypeThe type of module.
System.IServiceProviderservicesThe System.IServiceProvider for your dependency injection solution if using one; otherwise, pass null .

AddModulesAsync(Assembly, IServiceProvider)​

Add command modules from an System.Reflection.Assembly.

View Source​
Declaration
public async Task<IEnumerable<ModuleInfo>> AddModulesAsync(Assembly assembly, IServiceProvider services)
Returns​

System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Discord.Commands.ModuleInfo>>: A task that represents the asynchronous operation for adding the command modules. The task result contains an enumerable collection of modules added.

Parameters​
TypeNameDescription
System.Reflection.AssemblyassemblyThe System.Reflection.Assembly containing command modules.
System.IServiceProviderservicesThe System.IServiceProvider for your dependency injection solution if using one; otherwise, pass null.

RemoveModuleAsync(ModuleInfo)​

Removes the command module.

View Source​
Declaration
public async Task<bool> RemoveModuleAsync(ModuleInfo module)
Returns​

System.Threading.Tasks.Task<System.Boolean>: A task that represents the asynchronous removal operation. The task result contains a value that indicates whether the <code data-dev-comment-type="paramref" class="paramref">module</code> is successfully removed.

Parameters​
TypeNameDescription
Discord.Commands.ModuleInfomoduleThe Discord.Commands.ModuleInfo to be removed from the service.

RemoveModuleAsync<T>()​

Removes the command module.

View Source​
Declaration
public Task<bool> RemoveModuleAsync<T>()
Returns​

System.Threading.Tasks.Task<System.Boolean>: A task that represents the asynchronous removal operation. The task result contains a value that indicates whether the module is successfully removed.

Type Parameters​
NameDescription
TThe of the module.

RemoveModuleAsync(Type)​

Removes the command module.

View Source​
Declaration
public async Task<bool> RemoveModuleAsync(Type type)
Returns​

System.Threading.Tasks.Task<System.Boolean>: A task that represents the asynchronous removal operation. The task result contains a value that indicates whether the module is successfully removed.

Parameters​
TypeNameDescription
System.TypetypeThe System.Type of the module.

AddTypeReader<T>(TypeReader)​

Adds a custom Discord.Commands.TypeReader to this Discord.Commands.CommandService for the supplied object type. If <code data-dev-comment-type="typeparamref" class="typeparamref">T</code> is a System.ValueType, a nullable Discord.Commands.TypeReader will also be added. If a default Discord.Commands.TypeReader exists for <code data-dev-comment-type="typeparamref" class="typeparamref">T</code>, a warning will be logged and the default Discord.Commands.TypeReader will be replaced.

View Source​
Declaration
public void AddTypeReader<T>(TypeReader reader)
Parameters​
TypeNameDescription
Discord.Commands.TypeReaderreaderAn instance of the Discord.Commands.TypeReader to be added.
Type Parameters​
NameDescription
TThe object type to be read by the .

AddTypeReader(Type, TypeReader)​

Adds a custom Discord.Commands.TypeReader to this Discord.Commands.CommandService for the supplied object type. If <code data-dev-comment-type="paramref" class="paramref">type</code> is a System.ValueType, a nullable Discord.Commands.TypeReader for the value type will also be added. If a default Discord.Commands.TypeReader exists for <code data-dev-comment-type="paramref" class="paramref">type</code>, a warning will be logged and the default Discord.Commands.TypeReader will be replaced.

View Source​
Declaration
public void AddTypeReader(Type type, TypeReader reader)
Parameters​
TypeNameDescription
System.TypetypeA System.Type instance for the type to be read.
Discord.Commands.TypeReaderreaderAn instance of the Discord.Commands.TypeReader to be added.

AddTypeReader<T>(TypeReader, Boolean)​

Adds a custom Discord.Commands.TypeReader to this Discord.Commands.CommandService for the supplied object type. If <code data-dev-comment-type="typeparamref" class="typeparamref">T</code> is a System.ValueType, a nullable Discord.Commands.TypeReader will also be added.

View Source​
Declaration
public void AddTypeReader<T>(TypeReader reader, bool replaceDefault)
Parameters​
TypeNameDescription
Discord.Commands.TypeReaderreaderAn instance of the Discord.Commands.TypeReader to be added.
System.BooleanreplaceDefault

Defines whether the Discord.Commands.TypeReader should replace the default one for System.Type if it exists. |

Type Parameters​
NameDescription
TThe object type to be read by the .

AddTypeReader(Type, TypeReader, Boolean)​

Adds a custom Discord.Commands.TypeReader to this Discord.Commands.CommandService for the supplied object type. If <code data-dev-comment-type="paramref" class="paramref">type</code> is a System.ValueType, a nullable Discord.Commands.TypeReader for the value type will also be added.

View Source​
Declaration
public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault)
Parameters​
TypeNameDescription
System.TypetypeA System.Type instance for the type to be read.
Discord.Commands.TypeReaderreaderAn instance of the Discord.Commands.TypeReader to be added.
System.BooleanreplaceDefault

Defines whether the Discord.Commands.TypeReader should replace the default one for System.Type if it exists. |

Search(ICommandContext, Int32)​

Searches for the command.

View Source​
Declaration
public SearchResult Search(ICommandContext context, int argPos)
Returns​

Discord.Commands.SearchResult: The result containing the matching commands.

Parameters​
TypeNameDescription
Discord.Commands.ICommandContextcontextThe context of the command.
System.Int32argPosThe position of which the command starts at.

Search(ICommandContext, String)​

Searches for the command.

View Source​
Declaration
public SearchResult Search(ICommandContext context, string input)
Returns​

Discord.Commands.SearchResult: The result containing the matching commands.

Parameters​
TypeNameDescription
Discord.Commands.ICommandContextcontextThe context of the command.
System.StringinputThe command string.

Search(String)​

View Source​
Declaration
public SearchResult Search(string input)
Returns​

Discord.Commands.SearchResult

Parameters​
TypeName
System.Stringinput

ExecuteAsync(ICommandContext, Int32, IServiceProvider, MultiMatchHandling)​

Executes the command.

View Source​
Declaration
public Task<IResult> ExecuteAsync(ICommandContext context, int argPos, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Returns​

System.Threading.Tasks.Task<Discord.Commands.IResult>: A task that represents the asynchronous execution operation. The task result contains the result of the command execution.

Parameters​
TypeNameDescription
Discord.Commands.ICommandContextcontextThe context of the command.
System.Int32argPosThe position of which the command starts at.
System.IServiceProviderservicesThe service to be used in the command's dependency injection.
Discord.Commands.MultiMatchHandlingmultiMatchHandlingThe handling mode when multiple command matches are found.

ExecuteAsync(ICommandContext, String, IServiceProvider, MultiMatchHandling)​

Executes the command.

View Source​
Declaration
public async Task<IResult> ExecuteAsync(ICommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Returns​

System.Threading.Tasks.Task<Discord.Commands.IResult>: A task that represents the asynchronous execution operation. The task result contains the result of the command execution.

Parameters​
TypeNameDescription
Discord.Commands.ICommandContextcontextThe context of the command.
System.StringinputThe command string.
System.IServiceProviderservicesThe service to be used in the command's dependency injection.
Discord.Commands.MultiMatchHandlingmultiMatchHandlingThe handling mode when multiple command matches are found.

ValidateAndGetBestMatch(SearchResult, ICommandContext, IServiceProvider, MultiMatchHandling)​

Validates and gets the best Discord.Commands.CommandMatch from a specified Discord.Commands.SearchResult

View Source​
Declaration
public async Task<IResult> ValidateAndGetBestMatch(SearchResult matches, ICommandContext context, IServiceProvider provider, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
Returns​

System.Threading.Tasks.Task<Discord.Commands.IResult>: A task that represents the asynchronous validation operation. The task result contains the result of the command validation as a Discord.Commands.MatchResult or a Discord.Commands.SearchResult if no matches were found.

Parameters​
TypeNameDescription
Discord.Commands.SearchResultmatchesThe SearchResult.
Discord.Commands.ICommandContextcontextThe context of the command.
System.IServiceProviderproviderThe service provider to be used on the command's dependency injection.
Discord.Commands.MultiMatchHandlingmultiMatchHandlingThe handling mode when multiple command matches are found.

Dispose(Boolean)​

View Source​
Declaration
protected virtual void Dispose(bool disposing)
Parameters​
TypeName
System.Booleandisposing

IDisposable.Dispose()​

View Source​
Declaration
void IDisposable.Dispose()

Events​

Log​

Occurs when a command-related information is received.

View Source​
Declaration
public event Func<LogMessage, Task> Log
Event Type​

System.Func<Discord.LogMessage,System.Threading.Tasks.Task>

CommandExecuted​

Occurs when a command is executed.

View Source​
Declaration
public event Func<Optional<CommandInfo>, ICommandContext, IResult, Task> CommandExecuted
Event Type​

System.Func<Discord.Optional<Discord.Commands.CommandInfo>,Discord.Commands.ICommandContext,Discord.Commands.IResult,System.Threading.Tasks.Task>

Implements​

  • System.IDisposable

Extension Methods​

  • Discord.Commands.CommandService.Discord.Commands.CommandServiceExtensions.GetExecutableCommandsAsync(Discord.Commands.ICommandContext,System.IServiceProvider)