From 615d9696168606eca6e3fcd188205bfa6c3f226c Mon Sep 17 00:00:00 2001 From: Still Hsu <341464@gmail.com> Date: Wed, 6 Feb 2019 01:53:02 +0800 Subject: [PATCH 1/4] Clarify command samples by adding additional notes about each methods --- .../Modules/PublicModule.cs | 1 + .../Services/CommandHandlingService.cs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/samples/02_commands_framework/Modules/PublicModule.cs b/samples/02_commands_framework/Modules/PublicModule.cs index 8d55d8ba84..b9263649f8 100644 --- a/samples/02_commands_framework/Modules/PublicModule.cs +++ b/samples/02_commands_framework/Modules/PublicModule.cs @@ -60,6 +60,7 @@ public Task EchoAsync([Remainder] string text) public Task ListAsync(params string[] objects) => ReplyAsync("You listed: " + string.Join("; ", objects)); + // Setting a custom ErrorMessage property will help clarify the precondition error [Command("guild_only")] [RequireContext(ContextType.Guild, ErrorMessage = "Sorry, this command must be ran from within a server, not a DM!")] public Task GuildOnlyCommand() diff --git a/samples/02_commands_framework/Services/CommandHandlingService.cs b/samples/02_commands_framework/Services/CommandHandlingService.cs index d29be9201a..32d17a22c5 100644 --- a/samples/02_commands_framework/Services/CommandHandlingService.cs +++ b/samples/02_commands_framework/Services/CommandHandlingService.cs @@ -20,12 +20,16 @@ public CommandHandlingService(IServiceProvider services) _discord = services.GetRequiredService(); _services = services; + // Hook CommandExecuted to handle post-command-execution logic. _commands.CommandExecuted += CommandExecutedAsync; + // Hook MessageReceived so we can process each message to see + // if it qualifies as a command. _discord.MessageReceived += MessageReceivedAsync; } public async Task InitializeAsync() { + // Register modules that are public and inherit ModuleBase. await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); } @@ -37,10 +41,18 @@ public async Task MessageReceivedAsync(SocketMessage rawMessage) // This value holds the offset where the prefix ends var argPos = 0; + // Perform prefix check. You may want to replace this with + // (!message.HasCharPrefix(_prefix, ref argPos)) + // for a more traditional command format like !help. if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos)) return; var context = new SocketCommandContext(_discord, message); - await _commands.ExecuteAsync(context, argPos, _services); // we will handle the result in CommandExecutedAsync + // Perform the execution of the command. In this method, + // the command service will perform precondition and parsing check + // then execute the command if one is matched. + await _commands.ExecuteAsync(context, argPos, _services); + // Note that normally a result will be returned by this format, but here + // we will handle the result in CommandExecutedAsync, } public async Task CommandExecutedAsync(Optional command, ICommandContext context, IResult result) @@ -49,12 +61,12 @@ public async Task CommandExecutedAsync(Optional command, ICommandCo if (!command.IsSpecified) return; - // the command was succesful, we don't care about this result, unless we want to log that a command succeeded. + // the command was successful, 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()}"); + await context.Channel.SendMessageAsync($"error: {result}"); } } } From 8a6988f4a417320e0e1f4c2608fb42389484279c Mon Sep 17 00:00:00 2001 From: Still Hsu <341464@gmail.com> Date: Wed, 6 Feb 2019 01:56:25 +0800 Subject: [PATCH 2/4] Add additional notes for the Program class --- samples/02_commands_framework/Program.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samples/02_commands_framework/Program.cs b/samples/02_commands_framework/Program.cs index 76c11f9f08..11f39860f7 100644 --- a/samples/02_commands_framework/Program.cs +++ b/samples/02_commands_framework/Program.cs @@ -37,10 +37,12 @@ public async Task MainAsync() client.Log += LogAsync; services.GetRequiredService().Log += LogAsync; - // Tokens should be considered secret data, and never hard-coded. + // Tokens should be considered secret data and never hard-coded. + // We can read from the environment variable to prevent such practices. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); await client.StartAsync(); + // Here we initialize the logic required to register our commands. await services.GetRequiredService().InitializeAsync(); await Task.Delay(-1); From 93ccfd2282d39e1e54bee0f143b828071a2f5502 Mon Sep 17 00:00:00 2001 From: Still Hsu <341464@gmail.com> Date: Wed, 6 Feb 2019 01:57:56 +0800 Subject: [PATCH 3/4] Change wording on token read --- samples/02_commands_framework/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/02_commands_framework/Program.cs b/samples/02_commands_framework/Program.cs index 11f39860f7..ccbc8e1657 100644 --- a/samples/02_commands_framework/Program.cs +++ b/samples/02_commands_framework/Program.cs @@ -38,7 +38,7 @@ public async Task MainAsync() services.GetRequiredService().Log += LogAsync; // Tokens should be considered secret data and never hard-coded. - // We can read from the environment variable to prevent such practices. + // We can read from the environment variable to avoid hardcoding. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); await client.StartAsync(); From ee1807f1933760c3d0ab3b1a887e1ed9c67cddfd Mon Sep 17 00:00:00 2001 From: Chris Johnston Date: Wed, 6 Feb 2019 16:32:40 +0800 Subject: [PATCH 4/4] Change prefix sample to match referenced example Co-Authored-By: Still34 <341464@gmail.com> --- .../02_commands_framework/Services/CommandHandlingService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/02_commands_framework/Services/CommandHandlingService.cs b/samples/02_commands_framework/Services/CommandHandlingService.cs index 32d17a22c5..5ec496f788 100644 --- a/samples/02_commands_framework/Services/CommandHandlingService.cs +++ b/samples/02_commands_framework/Services/CommandHandlingService.cs @@ -42,7 +42,7 @@ public async Task MessageReceivedAsync(SocketMessage rawMessage) // This value holds the offset where the prefix ends var argPos = 0; // Perform prefix check. You may want to replace this with - // (!message.HasCharPrefix(_prefix, ref argPos)) + // (!message.HasCharPrefix('!', ref argPos)) // for a more traditional command format like !help. if (!message.HasMentionPrefix(_discord.CurrentUser, ref argPos)) return;