Browse Source

Adjust according to recent CommandExecuted changes

See:
+ f549da50e0
+ 6260749095
pull/1218/head
Still Hsu 6 years ago
parent
commit
1bbf3e8ee6
No known key found for this signature in database GPG Key ID: 8601A145FDA95209
3 changed files with 15 additions and 15 deletions
  1. +12
    -7
      docs/faq/commands/general.md
  2. +2
    -2
      docs/guides/commands/post-execution.md
  3. +1
    -6
      docs/guides/commands/samples/post-execution/command_executed_demo.cs

+ 12
- 7
docs/faq/commands/general.md View File

@@ -119,24 +119,29 @@ The following are the known caveats with `RunMode.Async`,
3. [ExecuteAsync] will immediately return [ExecuteResult] instead of
other result types (this is particularly important for those who wish
to utilize [RuntimeResult] in 2.0).
4. Exceptions are swallowed.
4. Exceptions are swallowed in the `ExecuteAsync` result.

However, there are ways to remedy some of these.

For #3, in Discord.Net 2.0, the library introduces a new event called
[CommandExecuted], which is raised whenever the command is
**successfully executed**. This event will be raised regardless of
the `RunMode` type and will return the appropriate execution result.
[CommandService.CommandExecuted], which is raised whenever the command is executed.
This event will be raised regardless of
the `RunMode` type and will return the appropriate execution result
and the associated @Discord.Commands.CommandInfo if applicable.

For #4, exceptions are caught in [CommandService.Log] event under
[LogMessage.Exception] as [CommandException].
[LogMessage.Exception] as [CommandException] and in the
[CommandService.CommandExecuted] event under the [IResult] as
[ExecuteResult.Exception].

[Task.Run]: https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run
[async state machine]: https://www.red-gate.com/simple-talk/dotnet/net-tools/c-async-what-is-it-and-how-does-it-work/
[ExecuteAsync]: xref:Discord.Commands.CommandService.ExecuteAsync*
[ExecuteResult]: xref:Discord.Commands.ExecuteResult
[RuntimeResult]: xref:Discord.Commands.RuntimeResult
[CommandExecuted]: xref:Discord.Commands.CommandService.CommandExecuted
[CommandService.CommandExecuted]: xref:Discord.Commands.CommandService.CommandExecuted
[CommandService.Log]: xref:Discord.Commands.CommandService.Log
[LogMessage.Exception]: xref:Discord.LogMessage.Exception*
[CommandException]: xref:Discord.Commands.CommandException
[ExecuteResult.Exception]: xref:Discord.Commands.ExecuteResult.Exception*
[CommandException]: xref:Discord.Commands.CommandException
[IResult]: xref:Discord.Commands.IResult

+ 2
- 2
docs/guides/commands/post-execution.md View File

@@ -31,13 +31,13 @@ be a violation of the SRP (Single Responsibility Principle).
Another major issue is if your command is marked with
`RunMode.Async`, [ExecuteAsync] will **always** return a successful
[ExecuteResult] instead of the actual result. You can learn more
about the impact in the [FAQ](xref:FAQ.Commands.General).
about the impact in @FAQ.Commands.General.

## CommandExecuted Event

Enter [CommandExecuted], an event that was introduced in
Discord.Net 2.0. This event is raised whenever a command is
executed, regardless of its execution status. This means this
executed regardless of its execution status. This means this
event can be used to streamline your post-execution design, and the
best thing about this event is that it is not prone
to `RunMode.Async`'s [ExecuteAsync] drawbacks.


+ 1
- 6
docs/guides/commands/samples/post-execution/command_executed_demo.cs View File

@@ -30,10 +30,5 @@ public async Task HandleCommandAsync(SocketMessage msg)
int argPos = 0;
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos)) || message.Author.IsBot) return;
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
// Optionally, you may pass the result manually into your
// CommandExecuted event handler if you wish to handle parsing or
// precondition failures in the same method.

// await OnCommandExecutedAsync(null, context, result);
await _commands.ExecuteAsync(context, argPos, _services);
}

Loading…
Cancel
Save