- Reword TypeReader comment to avoid giving the idea that the sample itself is "obsolete" - Remove CommandException logging comment regarding C#7.0 as the version is now the standard across VS2017 and up - Remove suggestion about handling result in command handler since it is now advised to use CommandExecuted instead + Add additional comment to clarify ctor for DI setuppull/1304/head
@@ -3,6 +3,9 @@ public class Initialize | |||||
private readonly CommandService _commands; | private readonly CommandService _commands; | ||||
private readonly DiscordSocketClient _client; | private readonly DiscordSocketClient _client; | ||||
// Ask if there are existing CommandService and DiscordSocketClient | |||||
// instance. If there are, we retrieve them and add them to the | |||||
// DI container; if not, we create our own. | |||||
public Initialize(CommandService commands = null, DiscordSocketClient client = null) | public Initialize(CommandService commands = null, DiscordSocketClient client = null) | ||||
{ | { | ||||
_commands = commands ?? new CommandService(); | _commands = commands ?? new CommandService(); | ||||
@@ -3,6 +3,7 @@ public class CommandHandler | |||||
private readonly DiscordSocketClient _client; | private readonly DiscordSocketClient _client; | ||||
private readonly CommandService _commands; | private readonly CommandService _commands; | ||||
// Retrieve client and CommandService instance via ctor | |||||
public CommandHandler(DiscordSocketClient client, CommandService commands) | public CommandHandler(DiscordSocketClient client, CommandService commands) | ||||
{ | { | ||||
_commands = commands; | _commands = commands; | ||||
@@ -46,19 +47,9 @@ public class CommandHandler | |||||
// Execute the command with the command context we just | // Execute the command with the command context we just | ||||
// created, along with the service provider for precondition checks. | // created, along with the service provider for precondition checks. | ||||
// Keep in mind that result does not indicate a return value | |||||
// rather an object stating if the command executed successfully. | |||||
var result = await _commands.ExecuteAsync( | |||||
await _commands.ExecuteAsync( | |||||
context: context, | context: context, | ||||
argPos: argPos, | argPos: argPos, | ||||
services: null); | services: null); | ||||
// Optionally, we may inform the user if the command fails | |||||
// to be executed; however, this may not always be desired, | |||||
// as it may clog up the request queue should a user spam a | |||||
// command. | |||||
// if (!result.IsSuccess) | |||||
// await context.Channel.SendMessageAsync(result.ErrorReason); | |||||
} | } | ||||
} | } |
@@ -1,6 +1,5 @@ | |||||
public async Task LogAsync(LogMessage logMessage) | public async Task LogAsync(LogMessage logMessage) | ||||
{ | { | ||||
// This casting type requries C#7 | |||||
if (logMessage.Exception is CommandException cmdException) | if (logMessage.Exception is CommandException cmdException) | ||||
{ | { | ||||
// We can tell the user that something unexpected has happened | // We can tell the user that something unexpected has happened | ||||
@@ -1,5 +1,6 @@ | |||||
// Note: This example is obsolete, a boolean type reader is bundled | |||||
// with Discord.Commands | |||||
// Please note that the library already supports type reading | |||||
// primitive types such as bool. This example is merely used | |||||
// to demonstrate how one could write a simple TypeReader. | |||||
using Discord; | using Discord; | ||||
using Discord.Commands; | using Discord.Commands; | ||||