@@ -1,5 +1,5 @@ | |||||
An example of how this class is used the command system can be seen | An example of how this class is used the command system can be seen | ||||
below: | below: | ||||
[!code[Sample module](../../guides/commands/samples/empty-module.cs)] | |||||
[!code[Command handler](../../guides/commands/samples/command_handler.cs)] | |||||
[!code[Sample module](../../guides/commands/samples/intro/empty-module.cs)] | |||||
[!code[Command handler](../../guides/commands/samples/intro/command_handler.cs)] |
@@ -19,7 +19,7 @@ DI when writing your modules. | |||||
### Example - Setting up Injection | ### Example - Setting up Injection | ||||
[!code-csharp[IServiceProvider Setup](samples/dependency_map_setup.cs)] | |||||
[!code-csharp[IServiceProvider Setup](samples/dependency-injection/dependency_map_setup.cs)] | |||||
## Usage in Modules | ## Usage in Modules | ||||
@@ -41,6 +41,7 @@ manner. | |||||
### Example - Injection in Modules | ### Example - Injection in Modules | ||||
[!code-csharp[IServiceProvider in Modules](samples/dependency_module.cs)] | |||||
[!code-csharp[Injection Modules](samples/dependency-injection/dependency_module.cs)] | |||||
[!code-csharp[Disallow Dependency Injection](samples/dependency-injection/dependency_module_noinject.cs)] | |||||
[DontInjectAttribute]: xref:Discord.Commands.DontInjectAttribute | [DontInjectAttribute]: xref:Discord.Commands.DontInjectAttribute |
@@ -23,7 +23,7 @@ minimum. | |||||
> look over the properties in [CommandServiceConfig] and their default | > look over the properties in [CommandServiceConfig] and their default | ||||
> values. | > values. | ||||
[!code-csharp[Command Handler](samples/command_handler.cs)] | |||||
[!code-csharp[Command Handler](samples/intro/command_handler.cs)] | |||||
[Command Service]: xref:Discord.Commands.CommandService | [Command Service]: xref:Discord.Commands.CommandService | ||||
[CommandServiceConfig]: xref:Discord.Commands.CommandServiceConfig | [CommandServiceConfig]: xref:Discord.Commands.CommandServiceConfig | ||||
@@ -56,7 +56,7 @@ your module must: | |||||
By now, your module should look like this: | By now, your module should look like this: | ||||
[!code-csharp[Empty Module](samples/empty-module.cs)] | |||||
[!code-csharp[Empty Module](samples/intro/empty-module.cs)] | |||||
> [!NOTE] | > [!NOTE] | ||||
> [ModuleBase] is an `abstract` class, meaning that you may extend it | > [ModuleBase] is an `abstract` class, meaning that you may extend it | ||||
@@ -162,7 +162,7 @@ accessing the channel through the [Context] and sending a message. | |||||
> [!TIP] | > [!TIP] | ||||
> At this point, your module should look comparable to this example: | > At this point, your module should look comparable to this example: | ||||
> [!code-csharp[Example Module](samples/module.cs)] | |||||
> [!code-csharp[Example Module](samples/intro/module.cs)] | |||||
#### Loading Modules Automatically | #### Loading Modules Automatically | ||||
@@ -218,4 +218,4 @@ Submodules are "modules" that reside within another one. Typically, | |||||
submodules are used to create nested groups (although not required to | submodules are used to create nested groups (although not required to | ||||
create nested groups). | create nested groups). | ||||
[!code-csharp[Groups and Submodules](samples/groups.cs)] | |||||
[!code-csharp[Groups and Submodules](samples/intro/groups.cs)] |
@@ -13,7 +13,7 @@ for you to work with. | |||||
If you recall, in the [Command Guide], we have shown the following | If you recall, in the [Command Guide], we have shown the following | ||||
example for executing and handling commands, | example for executing and handling commands, | ||||
[!code[Command Handler](samples/command_handler.cs)] | |||||
[!code[Command Handler](samples/intro/command_handler.cs)] | |||||
You may notice that after we perform [ExecuteAsync], we store the | You may notice that after we perform [ExecuteAsync], we store the | ||||
result and print it to the chat, essentially creating the most | result and print it to the chat, essentially creating the most | ||||
@@ -21,7 +21,7 @@ fundamental form of a post-execution handler. | |||||
With this in mind, we could start doing things like the following, | With this in mind, we could start doing things like the following, | ||||
[!code[Basic Command Handler](samples/post-execution_basic.cs)] | |||||
[!code[Basic Command Handler](samples/post-execution/post-execution_basic.cs)] | |||||
However, this may not always be preferred, because you are | However, this may not always be preferred, because you are | ||||
creating your post-execution logic *with* the essential command | creating your post-execution logic *with* the essential command | ||||
@@ -45,7 +45,7 @@ about this event is that it is not prone to `RunMode.Async`'s | |||||
Thus, we can begin working on code such as: | Thus, we can begin working on code such as: | ||||
[!code[CommandExecuted demo](samples/command_executed_demo.cs)] | |||||
[!code[CommandExecuted demo](samples/post-execution/command_executed_demo.cs)] | |||||
So now we have a streamlined post-execution pipeline, great! What's | So now we have a streamlined post-execution pipeline, great! What's | ||||
next? We can take this further by using [RuntimeResult]. | next? We can take this further by using [RuntimeResult]. | ||||
@@ -69,7 +69,7 @@ class. | |||||
The following creates a bare-minimum required for a sub-class | The following creates a bare-minimum required for a sub-class | ||||
of `RuntimeResult`, | of `RuntimeResult`, | ||||
[!code[Base Use](samples/customresult_base.cs)] | |||||
[!code[Base Use](samples/post-execution/customresult_base.cs)] | |||||
The sky is the limit from here. You can add any additional information | The sky is the limit from here. You can add any additional information | ||||
you would like regarding the execution result. | you would like regarding the execution result. | ||||
@@ -78,7 +78,7 @@ For example, you may want to add your result type or other | |||||
helpful information regarding the execution, or something | helpful information regarding the execution, or something | ||||
simple like static methods to help you create return types easily. | simple like static methods to help you create return types easily. | ||||
[!code[Extended Use](samples/customresult_extended.cs)] | |||||
[!code[Extended Use](samples/post-execution/customresult_extended.cs)] | |||||
After you're done creating your [RuntimeResult], you can | After you're done creating your [RuntimeResult], you can | ||||
implement it in your command by marking the command return type to | implement it in your command by marking the command return type to | ||||
@@ -91,11 +91,11 @@ implement it in your command by marking the command return type to | |||||
Here's an example of a command that utilizes such logic: | Here's an example of a command that utilizes such logic: | ||||
[!code[Usage](samples/customresult_usage.cs)] | |||||
[!code[Usage](samples/post-execution/customresult_usage.cs)] | |||||
And now we can check for it in our [CommandExecuted] handler: | And now we can check for it in our [CommandExecuted] handler: | ||||
[!code[Usage](samples/command_executed_adv_demo.cs)] | |||||
[!code[Usage](samples/post-execution/command_executed_adv_demo.cs)] | |||||
## CommandService.Log Event | ## CommandService.Log Event | ||||
@@ -110,7 +110,7 @@ as a [CommandException] type. The [CommandException] class allows | |||||
us to access the exception thrown, as well as the context | us to access the exception thrown, as well as the context | ||||
of the command. | of the command. | ||||
[!code[Logger Sample](samples/command_exception_log.cs)] | |||||
[!code[Logger Sample](samples/post-execution/command_exception_log.cs)] | |||||
[CommandException]: xref:Discord.Commands.CommandException | [CommandException]: xref:Discord.Commands.CommandException | ||||
[LogMessage.Exception]: xref:Discord.LogMessage.Exception | [LogMessage.Exception]: xref:Discord.LogMessage.Exception | ||||
@@ -0,0 +1,37 @@ | |||||
// After setting up dependency injection, modules will need to request | |||||
// the dependencies to let the library know to pass | |||||
// them along during execution. | |||||
// Dependency can be injected in two ways with Discord.Net. | |||||
// You may inject any required dependencies via... | |||||
// the module constructor | |||||
// -or- | |||||
// public settable properties | |||||
// Injection via constructor | |||||
public class DatabaseModule : ModuleBase<SocketCommandContext> | |||||
{ | |||||
private readonly DatabaseService _database; | |||||
public DatabaseModule(DatabaseService database) | |||||
{ | |||||
_database = database; | |||||
} | |||||
[Command("read")] | |||||
public async Task ReadFromDbAsync() | |||||
{ | |||||
await ReplyAsync(_database.GetData()); | |||||
} | |||||
} | |||||
// Injection via public settable properties | |||||
public class DatabaseModule : ModuleBase<SocketCommandContext> | |||||
{ | |||||
public DatabaseService DbService { get; set; } | |||||
[Command("read")] | |||||
public async Task ReadFromDbAsync() | |||||
{ | |||||
await ReplyAsync(_database.GetData()); | |||||
} | |||||
} |
@@ -0,0 +1,29 @@ | |||||
// Sometimes injecting dependencies automatically with the provided | |||||
// methods in the prior example may not be desired. | |||||
// You may explicitly tell Discord.Net to **not** inject the properties | |||||
// by either... | |||||
// restricting the access modifier | |||||
// -or- | |||||
// applying DontInjectAttribute to the property | |||||
// Restricting the access modifier of the property | |||||
public class ImageModule : ModuleBase<SocketCommandContext> | |||||
{ | |||||
public ImageService ImageService { get; } | |||||
public ImageModule() | |||||
{ | |||||
ImageService = new ImageService(); | |||||
} | |||||
} | |||||
// Applying DontInjectAttribute | |||||
public class ImageModule : ModuleBase<SocketCommandContext> | |||||
{ | |||||
[DontInject] | |||||
public ImageService ImageService { get; set; } | |||||
public ImageModule() | |||||
{ | |||||
ImageService = new ImageService(); | |||||
} | |||||
} |
@@ -1,30 +0,0 @@ | |||||
public class DatabaseModule : ModuleBase<SocketCommandContext> | |||||
{ | |||||
private readonly DatabaseService _database; | |||||
// Dependencies can be injected via the constructor | |||||
public DatabaseModule(DatabaseService database) | |||||
{ | |||||
_database = database; | |||||
} | |||||
[Command("read")] | |||||
public async Task ReadFromDbAsync() | |||||
{ | |||||
await ReplyAsync(_database.GetData()); | |||||
} | |||||
} | |||||
public class MixModule : ModuleBase<SocketCommandContext> | |||||
{ | |||||
// Public settable properties will be injected | |||||
public AnnounceService AnnounceService { get; set; } | |||||
// Public properties without setters will not be injected | |||||
public ImageService ImageService { get; } | |||||
// Public properties annotated with [DontInject] will not | |||||
// be injected | |||||
[DontInject] | |||||
public NotificationService NotificationService { get; set; } | |||||
} |
@@ -18,8 +18,9 @@ By default, the following Types are supported arguments: | |||||
* `ulong`/`long` | * `ulong`/`long` | ||||
* `float`, `double`, `decimal` | * `float`, `double`, `decimal` | ||||
* `string` | * `string` | ||||
* `enum` | |||||
* `DateTime`/`DateTimeOffset`/`TimeSpan` | * `DateTime`/`DateTimeOffset`/`TimeSpan` | ||||
* `Nullable<T>` where applicible | |||||
* `Nullable<T>` where applicable | |||||
* Any implementation of `IChannel`/`IMessage`/`IUser`/`IRole` | * Any implementation of `IChannel`/`IMessage`/`IUser`/`IRole` | ||||
## Creating a Type Reader | ## Creating a Type Reader | ||||
@@ -49,7 +50,7 @@ necessary. | |||||
### Example - Creating a Type Reader | ### Example - Creating a Type Reader | ||||
[!code-csharp[TypeReaders](samples/typereader.cs)] | |||||
[!code-csharp[TypeReaders](samples/typereaders/typereader.cs)] | |||||
## Registering a Type Reader | ## Registering a Type Reader | ||||
@@ -66,4 +67,4 @@ To register a TypeReader, invoke [CommandService.AddTypeReader]. | |||||
### Example - Adding a Type Reader | ### Example - Adding a Type Reader | ||||
[!code-csharp[Adding TypeReaders](samples/typereader-register.cs)] | |||||
[!code-csharp[Adding TypeReaders](samples/typereaders/typereader-register.cs)] |