Skip to content

Commit

Permalink
docs: rework
Browse files Browse the repository at this point in the history
  • Loading branch information
NyuwBot committed Jul 2, 2023
1 parent a1eef5c commit c71facc
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
uid: modules_commandsnext_command_attributes
title: Command Attributes
author: DisCatSharp Team
hasDiscordComponents: true
---

## Built-In Attributes
Expand Down Expand Up @@ -36,6 +38,7 @@ If the above attributes don't meet your needs, CommandsNext also gives you the o
Simply create a new class which inherits from `CheckBaseAttribute` and implement the required method.

Our example below will only allow a command to be ran during a specified year.

```cs
public class RequireYearAttribute : CheckBaseAttribute
{
Expand All @@ -53,9 +56,9 @@ public class RequireYearAttribute : CheckBaseAttribute
}
```

<br/>
You'll also need to apply the `AttributeUsage` attribute to your attribute.<br/>
You'll also need to apply the `AttributeUsage` attribute to your attribute.
For our example attribute, we'll set it to only be usable once on methods.

```cs
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class RequireYearAttribute : CheckBaseAttribute
Expand Down Expand Up @@ -88,8 +91,8 @@ private async Task CmdErroredHandler(CommandsNextExtension _, CommandErrorEventA
}
```

<br/>
Once you've got all of that completed, you'll be able to use it on a command!

```cs
[Command("generic"), RequireYear(2030)]
public async Task GenericCommand(CommandContext ctx, string generic)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
---
uid: modules_commandsnext_argument_converters
uid: modules_commandsnext_customization_argument_converters
title: Argument Converter
author: DisCatSharp Team
hasDiscordComponents: true
---

## Custom Argument Converter

Writing your own argument converter will enable you to convert custom types and replace the functionality of existing converters.
Like many things in DisCatSharp, doing this is straightforward and simple.

First, create a new class which implements `IArgumentConverter<T>` and its method `ConvertAsync`.
Our example will be a boolean converter, so we'll also pass `bool` as the type parameter for `IArgumentConverter`.

```cs
public class CustomArgumentConverter : IArgumentConverter<bool>
{
public Task<Optional<bool>> ConvertAsync(string value, CommandContext ctx)
{
if (bool.TryParse(value, out var boolean))
{
return Task.FromResult(Optional.FromValue(boolean));
return Task.FromResult(Optional.Some<bool>(boolean));
}

switch (value.ToLower())
{
case "yes":
case "y":
case "t":
return Task.FromResult(Optional.FromValue(true));
return Task.FromResult(Optional.Some<bool>(true));

case "no":
case "n":
case "f":
return Task.FromResult(Optional.FromValue(false));
return Task.FromResult(Optional.Some<bool>(false));

default:
return Task.FromResult(Optional.FromNoValue<bool>());
return Task.FromResult(Optional.None);
}
}
}
```

Then register the argument converter with CommandContext.

```cs
var discord = new DiscordClient();
var commands = discord.UseCommandsNext();

commands.RegisterConverter(new CustomArgumentConverter());
```

<br/>
Once the argument converter is written and registered, we'll be able to use it:

```cs
[Command("boolean")]
public async Task BooleanCommand(CommandContext ctx, bool boolean)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
---
uid: modules_commandsnext_command_handler
uid: modules_commandsnext_customization_command_handler
title: Custom Command Handler
author: DisCatSharp Team
hasDiscordComponents: true
---

## Custom Command Handler
> [!IMPORTANT]
> Writing your own handler logic should only be done if *you know what you're doing*.<br/>

>[!IMPORTANT]
> Writing your own handler logic should only be done if *you know what you're doing*.
> You will be responsible for command execution and preventing deadlocks.
### Disable Default Handler
To begin, we'll need to disable the default command handler provided by CommandsNext.<br/>

To begin, we'll need to disable the default command handler provided by CommandsNext.
This is done by setting the `UseDefaultCommandHandler` configuration property to `false`.

```cs
var discord = new DiscordClient();
var commands = discord.UseCommandsNext(new CommandsNextConfiguration()
Expand All @@ -20,7 +25,9 @@ var commands = discord.UseCommandsNext(new CommandsNextConfiguration()
```

### Create Event Handler

We'll then write a new handler for the `MessageCreated` event fired from `DiscordClient`.

```cs
discord.MessageCreated += CommandHandler;

Expand All @@ -34,7 +41,9 @@ private Task CommandHandler(DiscordClient client, MessageCreateEventArgs e)
This event handler will be our command handler, and you'll need to write the logic for it.

### Handle Commands

Start by parsing the message content for a prefix and command string

```cs
var cnext = client.GetCommandsNext();
var msg = e.Message;
Expand All @@ -51,24 +60,29 @@ var cmdString = msg.Content.Substring(cmdStart);
```

Then provide the command string to `CommandsNextExtension#FindCommand`

```cs
var command = cnext.FindCommand(cmdString, out var args);
```

Create a command context using our message and prefix, along with the command and its arguments

```cs
var ctx = cnext.CreateContext(msg, prefix, command, args);
```

And pass the context to `CommandsNextExtension#ExecuteCommandAsync` to execute the command.

```cs
_ = Task.Run(async () => await cnext.ExecuteCommandAsync(ctx));
// Wrapped in Task.Run() to prevent deadlocks.
```


### Finished Product

Altogether, your implementation should function similarly to the following:

```cs
private Task CommandHandler(DiscordClient client, MessageCreateEventArgs e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
---
uid: modules_commandsnext_help_formatter
uid: modules_commandsnext_customization_help_formatter
title: Help Formatter
author: DisCatSharp Team
hasDiscordComponents: true
---

## Custom Help Formatter

The built-in help command provided by CommandsNext is generated with a *help formatter*.
This simple mechanism is given a command and its subcommands then returns a formatted help message.
If you're not happy with the default help formatter, you're able to write your own and customize the output to your liking.

<br/>
Simply inherit from `BaseHelpFormatter` and provide an implementation for each of the required methods.

```cs
public class CustomHelpFormatter : BaseHelpFormatter
{
Expand Down Expand Up @@ -54,8 +57,8 @@ public class CustomHelpFormatter : BaseHelpFormatter
}
```

<br/>
Alternatively, if you're only wanting to make a few small tweaks to the default help, you can write a simple help formatter which inherits from `DefaultHelpFormatter` and modify the inherited `EmbedBuilder` property.

```cs
public class CustomHelpFormatter : DefaultHelpFormatter
{
Expand All @@ -69,15 +72,15 @@ public class CustomHelpFormatter : DefaultHelpFormatter
}
```

<br/>
Your final step is to register your help formatter with CommandsNext.

```cs
var discord = new DiscordClient();
var commands = discord.UseCommandsNext();

commands.SetHelpFormatter<CustomHelpFormatter>();
```

That's all there is to it.

<br/>
![Fresh New Look](/images/commands_help_formatter_01.png)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
uid: modules_commandsnext_dependency_injection
title: Dependency Injection
author: DisCatSharp Team
hasDiscordComponents: true
---

## Dependency Injection
Expand Down
Loading

0 comments on commit c71facc

Please sign in to comment.