docs: update command sample to use CommandExecuted, Log
This commit is contained in:
@@ -20,6 +20,8 @@ namespace _02_commands_framework.Services
|
|||||||
_discord = services.GetRequiredService<DiscordSocketClient>();
|
_discord = services.GetRequiredService<DiscordSocketClient>();
|
||||||
_services = services;
|
_services = services;
|
||||||
|
|
||||||
|
_commands.CommandExecuted += CommandExecutedAsync;
|
||||||
|
_commands.Log += LogAsync;
|
||||||
_discord.MessageReceived += MessageReceivedAsync;
|
_discord.MessageReceived += MessageReceivedAsync;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,11 +41,28 @@ namespace _02_commands_framework.Services
|
|||||||
if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos)) return;
|
if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos)) return;
|
||||||
|
|
||||||
var context = new SocketCommandContext(_discord, message);
|
var context = new SocketCommandContext(_discord, message);
|
||||||
var result = await _commands.ExecuteAsync(context, argPos, _services);
|
await _commands.ExecuteAsync(context, argPos, _services); // we will handle the result in CommandExecutedAsync
|
||||||
|
}
|
||||||
|
|
||||||
if (result.Error.HasValue &&
|
public async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
|
||||||
result.Error.Value != CommandError.UnknownCommand) // it's bad practice to send 'unknown command' errors
|
{
|
||||||
await context.Channel.SendMessageAsync(result.ToString());
|
// command is unspecified when there was a search failure (command not found); we don't care about these errors
|
||||||
|
if (!command.IsSpecified)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// the command was succesful, we don't care about this result, unless we want to log that a command succeeded.
|
||||||
|
if (result.IsSuccess)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// the command failed, let's notify the user that something happened.
|
||||||
|
await context.Channel.SendMessageAsync($"error: {result.ToString()}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Task LogAsync(LogMessage log)
|
||||||
|
{
|
||||||
|
Console.WriteLine(log.ToString());
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user