Browse Source

Revise post-execution docs

* Fix incorrect Optional<T> usage
* Indent some sample code and add a comment reminding the user that the post-execution basic sample code is not ideal.
pull/1218/head
Still Hsu 6 years ago
parent
commit
26e7b76fdc
No known key found for this signature in database GPG Key ID: 8601A145FDA95209
2 changed files with 12 additions and 5 deletions
  1. +7
    -3
      docs/guides/commands/samples/post-execution/command_executed_demo.cs
  2. +5
    -2
      docs/guides/commands/samples/post-execution/post-execution_basic.cs

+ 7
- 3
docs/guides/commands/samples/post-execution/command_executed_demo.cs View File

@@ -20,15 +20,19 @@ public async Task OnCommandExecutedAsync(Optional<CommandInfo> command, ICommand


// ...or even log the result (the method used should fit into // ...or even log the result (the method used should fit into
// your existing log handler) // your existing log handler)
var commandName = command.HasValue ? command.Name : "A command";
await _log.LogAsync(new LogMessage(LogSeverity.Info, "CommandExecution", $"{commandName} was executed at {DateTime.UtcNow}."));
var commandName = command.IsSpecified ? command.Value.Name : "A command";
await _log.LogAsync(new LogMessage(LogSeverity.Info,
"CommandExecution",
$"{commandName} was executed at {DateTime.UtcNow}."));
} }
public async Task HandleCommandAsync(SocketMessage msg) public async Task HandleCommandAsync(SocketMessage msg)
{ {
var message = messageParam as SocketUserMessage; var message = messageParam as SocketUserMessage;
if (message == null) return; if (message == null) return;
int argPos = 0; int argPos = 0;
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos)) || message.Author.IsBot) return;
if (!(message.HasCharPrefix('!', ref argPos) ||
message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
message.Author.IsBot) return;
var context = new SocketCommandContext(_client, message); var context = new SocketCommandContext(_client, message);
await _commands.ExecuteAsync(context, argPos, _services); await _commands.ExecuteAsync(context, argPos, _services);
} }

+ 5
- 2
docs/guides/commands/samples/post-execution/post-execution_basic.cs View File

@@ -1,11 +1,14 @@
// Bad code!!!
var result = await _commands.ExecuteAsync(context, argPos, _services); var result = await _commands.ExecuteAsync(context, argPos, _services);
if (result.CommandError != null) if (result.CommandError != null)
switch(result.CommandError) switch(result.CommandError)
{ {
case CommandError.BadArgCount: case CommandError.BadArgCount:
await context.Channel.SendMessageAsync("Parameter count does not match any command's.");
await context.Channel.SendMessageAsync(
"Parameter count does not match any command's.");
break; break;
default: default:
await context.Channel.SendMessageAsync($"An error has occurred {result.ErrorReason}");
await context.Channel.SendMessageAsync(
$"An error has occurred {result.ErrorReason}");
break; break;
} }

Loading…
Cancel
Save