From f20b0d2fad7dada623b8cf58d7c2495037e292b5 Mon Sep 17 00:00:00 2001 From: KubaZ2 Date: Mon, 18 Nov 2024 23:07:14 +0100 Subject: [PATCH] Update the documentation --- .../Introduction/ExampleModule.cs | 17 +++++++++ .../Introduction/MessageCommandModule.cs | 9 ----- .../Introduction/Program.cs | 19 ++++++++-- .../Introduction/SlashCommandModule.cs | 9 ----- .../Introduction/UserCommandModule.cs | 9 ----- .../IntroductionHosting/Program.cs | 25 ++++++++----- .../application-commands/introduction.md | 37 ++++++------------- .../application-commands/parameters.md | 36 ++++++++++++------ .../Introduction/Program.cs | 11 ++++++ .../IntroductionHosting/Program.cs | 16 +++++--- .../component-interactions/introduction.md | 14 ++++--- .../text-commands/Introduction/Program.cs | 11 ++++++ .../IntroductionHosting/Program.cs | 14 +++++-- .../services/text-commands/introduction.md | 10 ++--- README.md | 6 +-- 15 files changed, 143 insertions(+), 100 deletions(-) create mode 100644 Documentation/guides/services/application-commands/Introduction/ExampleModule.cs delete mode 100644 Documentation/guides/services/application-commands/Introduction/MessageCommandModule.cs delete mode 100644 Documentation/guides/services/application-commands/Introduction/SlashCommandModule.cs delete mode 100644 Documentation/guides/services/application-commands/Introduction/UserCommandModule.cs diff --git a/Documentation/guides/services/application-commands/Introduction/ExampleModule.cs b/Documentation/guides/services/application-commands/Introduction/ExampleModule.cs new file mode 100644 index 00000000..afad139a --- /dev/null +++ b/Documentation/guides/services/application-commands/Introduction/ExampleModule.cs @@ -0,0 +1,17 @@ +using NetCord; +using NetCord.Rest; +using NetCord.Services.ApplicationCommands; + +namespace MyBot; + +public class ExampleModule : ApplicationCommandModule +{ + [SlashCommand("pong", "Pong!")] + public static string Pong() => "Ping!"; + + [UserCommand("ID")] + public static string Id(User user) => user.Id.ToString(); + + [MessageCommand("Timestamp")] + public static string Timestamp(RestMessage message) => message.CreatedAt.ToString(); +} diff --git a/Documentation/guides/services/application-commands/Introduction/MessageCommandModule.cs b/Documentation/guides/services/application-commands/Introduction/MessageCommandModule.cs deleted file mode 100644 index 0a0ac78e..00000000 --- a/Documentation/guides/services/application-commands/Introduction/MessageCommandModule.cs +++ /dev/null @@ -1,9 +0,0 @@ -using NetCord.Services.ApplicationCommands; - -namespace MyBot; - -public class MessageCommandModule : ApplicationCommandModule -{ - [MessageCommand("Timestamp")] - public string Timestamp() => Context.Target.CreatedAt.ToString(); -} diff --git a/Documentation/guides/services/application-commands/Introduction/Program.cs b/Documentation/guides/services/application-commands/Introduction/Program.cs index 3d94a754..4295b357 100644 --- a/Documentation/guides/services/application-commands/Introduction/Program.cs +++ b/Documentation/guides/services/application-commands/Introduction/Program.cs @@ -9,20 +9,32 @@ Intents = default, }); -ApplicationCommandService applicationCommandService = new(); +// Create the application command service +ApplicationCommandService applicationCommandService = new(); + +// Add commands using minimal APIs applicationCommandService.AddSlashCommand("ping", "Ping!", () => "Pong!"); +applicationCommandService.AddUserCommand("Username", (User user) => user.Username); +applicationCommandService.AddMessageCommand("Length", (RestMessage message) => message.Content.Length.ToString()); + +// Add commands from modules applicationCommandService.AddModules(typeof(Program).Assembly); +// Add the handler to handle interactions client.InteractionCreate += async interaction => { - if (interaction is not SlashCommandInteraction slashCommandInteraction) + // Check if the interaction is an application command interaction + if (interaction is not ApplicationCommandInteraction applicationCommandInteraction) return; - var result = await applicationCommandService.ExecuteAsync(new SlashCommandContext(slashCommandInteraction, client)); + // Execute the command + var result = await applicationCommandService.ExecuteAsync(new ApplicationCommandContext(applicationCommandInteraction, client)); + // Check if the execution failed if (result is not IFailResult failResult) return; + // Return the error message to the user if the execution failed try { await interaction.SendResponseAsync(InteractionCallback.Message(failResult.Message)); @@ -32,6 +44,7 @@ } }; +// Create the commands so that you can use them in the Discord client await applicationCommandService.CreateCommandsAsync(client.Rest, client.Id); client.Log += message => diff --git a/Documentation/guides/services/application-commands/Introduction/SlashCommandModule.cs b/Documentation/guides/services/application-commands/Introduction/SlashCommandModule.cs deleted file mode 100644 index 388f3972..00000000 --- a/Documentation/guides/services/application-commands/Introduction/SlashCommandModule.cs +++ /dev/null @@ -1,9 +0,0 @@ -using NetCord.Services.ApplicationCommands; - -namespace MyBot; - -public class SlashCommandModule : ApplicationCommandModule -{ - [SlashCommand("pong", "Pong!")] - public static string Pong() => "Ping!"; -} diff --git a/Documentation/guides/services/application-commands/Introduction/UserCommandModule.cs b/Documentation/guides/services/application-commands/Introduction/UserCommandModule.cs deleted file mode 100644 index 28d2c8a5..00000000 --- a/Documentation/guides/services/application-commands/Introduction/UserCommandModule.cs +++ /dev/null @@ -1,9 +0,0 @@ -using NetCord.Services.ApplicationCommands; - -namespace MyBot; - -public class UserCommandModule : ApplicationCommandModule -{ - [UserCommand("ID")] - public string Id() => Context.Target.Id.ToString(); -} diff --git a/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs b/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs index b048a888..6f9fd621 100644 --- a/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs +++ b/Documentation/guides/services/application-commands/IntroductionHosting/Program.cs @@ -4,21 +4,26 @@ using NetCord.Hosting.Gateway; using NetCord.Hosting.Services; using NetCord.Hosting.Services.ApplicationCommands; +using NetCord.Rest; using NetCord.Services.ApplicationCommands; var builder = Host.CreateApplicationBuilder(args); builder.Services .AddDiscordGateway() - .AddApplicationCommands() - .AddApplicationCommands() - .AddApplicationCommands(); - -var host = builder.Build() - .AddSlashCommand("ping", "Ping!", () => "Pong!") - .AddUserCommand("Username", (UserCommandContext context) => context.Target.Username) - .AddMessageCommand("Length", (MessageCommandContext context) => context.Target.Content.Length.ToString()) - .AddModules(typeof(Program).Assembly) - .UseGatewayEventHandlers(); + .AddApplicationCommands(); + +var host = builder.Build(); + +// Add commands using minimal APIs +host.AddSlashCommand("ping", "Ping!", () => "Pong!") + .AddUserCommand("Username", (User user) => user.Username) + .AddMessageCommand("Length", (RestMessage message) => message.Content.Length.ToString()); + +// Add commands from modules +host.AddModules(typeof(Program).Assembly); + +// Add handlers to handle the commands +host.UseGatewayEventHandlers(); await host.RunAsync(); diff --git a/Documentation/guides/services/application-commands/introduction.md b/Documentation/guides/services/application-commands/introduction.md index 5e0ab25e..827f4367 100644 --- a/Documentation/guides/services/application-commands/introduction.md +++ b/Documentation/guides/services/application-commands/introduction.md @@ -6,22 +6,22 @@ uid: application-commands ## [.NET Generic Host](#tab/generic-host) -Adding application commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceServiceCollectionExtensions.AddApplicationCommands``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add an application command service to your host builder. Then, use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddSlashCommand*, @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddUserCommand* or @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddMessageCommand* to add an application command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. -[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=13-15,18-22)] +Adding application commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceServiceCollectionExtensions.AddApplicationCommands``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the application command service to your host builder. Then, use @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddSlashCommand*, @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddUserCommand* or @NetCord.Hosting.Services.ApplicationCommands.ApplicationCommandServiceHostExtensions.AddMessageCommand* to add an application command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add application command modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. +[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=14,19-21,24,27)] ## [Bare Bones](#tab/bare-bones) -First, add the following lines to using the section. -[!code-cs[Program.cs](Introduction/Program.cs#L4-L5)] +First, add the following lines to the using section. +[!code-cs[Program.cs](Introduction/Program.cs#L3-L5)] -Now, it's time to create @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 instance and add application commands to it. You can do it by using @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddSlashCommand*, @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddUserCommand* or @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddMessageCommand* to add an application command using the minimal APIs way and/or by using @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddModules(System.Reflection.Assembly) to add modules from an assembly. You can use a context of your choice, it can be for example @NetCord.Services.ApplicationCommands.SlashCommandContext, @NetCord.Services.ApplicationCommands.UserCommandContext or @NetCord.Services.ApplicationCommands.MessageCommandContext. In this example, we will use @NetCord.Services.ApplicationCommands.SlashCommandContext. -[!code-cs[Program.cs](Introduction/Program.cs#L12-L14)] +Now, it's time to create @NetCord.Services.ApplicationCommands.ApplicationCommandService`1 instance and add application commands to it. You can do it by using @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddSlashCommand*, @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddUserCommand* or @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddMessageCommand* to add an application command using the minimal APIs way and/or by using @NetCord.Services.ApplicationCommands.ApplicationCommandService`1.AddModules(System.Reflection.Assembly) to add application command modules from an assembly. +[!code-cs[Program.cs](Introduction/Program.cs#L12-L21)] We can add a command handler now. If you used other context than @NetCord.Services.ApplicationCommands.SlashCommandContext, you should change the interaction type of the handler to the appropriate one. -[!code-cs[Program.cs](Introduction/Program.cs#L16-L33)] +[!code-cs[Program.cs](Introduction/Program.cs#L23-L45)] Now, we should send the commands to Discord, to make them usable. Add the following line under the handler: -[!code-cs[Program.cs](Introduction/Program.cs#L35)] +[!code-cs[Program.cs](Introduction/Program.cs#L47-L48)] ### The Final Product @@ -34,22 +34,9 @@ Now, we should send the commands to Discord, to make them usable. Add the follow > If you don't see the commands in Discord, try refreshing the Discord client using `Ctrl + R` on PC or `⌘ + R` on Mac. > [!IMPORTANT] -> Please note that names of: -> - slash commands -> - sub slash commands -> - slash command parameters -> -> **must** be lowercase. +> Please note that names of slash commands must be lowercase. -### Example Modules +### Example Module -Here you can see example modules for each type of application command. - -#### Slash Command Module -[!code-cs[SlashCommandModule.cs](Introduction/SlashCommandModule.cs)] - -#### User Command Module -[!code-cs[UserCommandModule.cs](Introduction/UserCommandModule.cs)] - -#### Message Command Module -[!code-cs[MessageCommandModule.cs](Introduction/MessageCommandModule.cs)] +Here you can see an example module showing how to use modules with application commands. +[!code-cs[ExampleModule.cs](Introduction/ExampleModule.cs)] diff --git a/Documentation/guides/services/application-commands/parameters.md b/Documentation/guides/services/application-commands/parameters.md index 90d2726c..83e7effb 100644 --- a/Documentation/guides/services/application-commands/parameters.md +++ b/Documentation/guides/services/application-commands/parameters.md @@ -1,35 +1,47 @@ # Parameters -> [!WARNING] -> Parameters are supported only by slash commands. +## Slash Commands + +Slash commands support up to 25 parameters. + +### Optional parameters -## Optional parameters To mark parameters as optional, give them a default value, example: [!code-cs[ExampleModule.cs](Parameters/ExampleModule.cs#L8-L13)] -## Parameter name and description - -> [!IMPORTANT] -> Parameter names **must** be lowercase. +### Parameter name and description You can change parameter name and parameter description using @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute, example: [!code-cs[ExampleModule.cs](Parameters/ExampleModule.cs#L15-L21)] -## Min and Max Values +### Min and Max Values + You can specify min and max parameter values by setting @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute.MinValue and @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute.MaxValue properties in @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute. It's only possible for numeric types. -## Min and Max Length +### Min and Max Length + You can specify min and max parameter length by setting @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute.MinLength and @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute.MaxLength properties in @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute. It's only possible for text types. -## Choices and Autocomplete +### Choices and Autocomplete + Choices are constants for a given parameter, autocomplete may depend on a text entered by a user. -### Choices +#### Choices + Choices are automatically generated when you set `enum` as a parameter type, you can override choices' names using @NetCord.Services.ApplicationCommands.SlashCommandChoiceAttribute on enum fields, example: [!code-cs[ExampleModule.cs](Parameters/ExampleModule.cs#L23-L24)] [!code-cs[Animal.cs](Parameters/Animal.cs#l5-L12)] You can also define own choices in the Type Reader by overriding @NetCord.Services.ApplicationCommands.SlashCommandTypeReader`1.ChoicesProvider property or in @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute by setting @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute.ChoicesProviderType property. -### Autocomplete +#### Autocomplete + You can turn on autocomplete in Type Reader by overriding @NetCord.Services.ApplicationCommands.SlashCommandTypeReader`1.AutocompleteProviderType property or in @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute by setting @NetCord.Services.ApplicationCommands.SlashCommandParameterAttribute.AutocompleteProviderType property. You run it using @NetCord.Services.ApplicationCommands.ApplicationCommandService`2.ExecuteAutocompleteAsync(`1,System.IServiceProvider). + +## User Commands + +User commands only support a single parameter of type @NetCord.User. It's is not required, but allows you to access the target user of the user command easily. + +## Message Commands + +Message commands only support a single parameter of type @NetCord.Rest.RestMessage. It's is not required, but allows you to access the target message of the message command easily. diff --git a/Documentation/guides/services/component-interactions/Introduction/Program.cs b/Documentation/guides/services/component-interactions/Introduction/Program.cs index 76b66a0f..fdaf5641 100644 --- a/Documentation/guides/services/component-interactions/Introduction/Program.cs +++ b/Documentation/guides/services/component-interactions/Introduction/Program.cs @@ -9,19 +9,30 @@ Intents = default, }); +// Create the component interaction service with the button interaction context ComponentInteractionService interactionService = new(); + +// Add a component interaction using minimal APIs +interactionService.AddInteraction("ping", () => "Pong!"); + +// Add component interactions from modules interactionService.AddModules(typeof(Program).Assembly); +// Add the handler to handle interactions client.InteractionCreate += async interaction => { + // Check if the interaction is a button interaction if (interaction is not ButtonInteraction buttonInteraction) return; + // Execute the interaction var result = await interactionService.ExecuteAsync(new ButtonInteractionContext(buttonInteraction, client)); + // Check if the execution failed if (result is not IFailResult failResult) return; + // Return the error message to the user if the execution failed try { await interaction.SendResponseAsync(InteractionCallback.Message(failResult.Message)); diff --git a/Documentation/guides/services/component-interactions/IntroductionHosting/Program.cs b/Documentation/guides/services/component-interactions/IntroductionHosting/Program.cs index f924c434..50a21d6a 100644 --- a/Documentation/guides/services/component-interactions/IntroductionHosting/Program.cs +++ b/Documentation/guides/services/component-interactions/IntroductionHosting/Program.cs @@ -18,15 +18,21 @@ .AddComponentInteractions() .AddComponentInteractions(); -var host = builder.Build() - .AddComponentInteraction("ping", () => "Pong!") +var host = builder.Build(); + +// Add component interactions using minimal APIs +host.AddComponentInteraction("ping", () => "Pong!") .AddComponentInteraction("string", (StringMenuInteractionContext context) => string.Join("\n", context.SelectedValues)) .AddComponentInteraction("user", (UserMenuInteractionContext context) => string.Join("\n", context.SelectedUsers)) .AddComponentInteraction("role", (RoleMenuInteractionContext context) => string.Join("\n", context.SelectedRoles)) .AddComponentInteraction("mentionable", (MentionableMenuInteractionContext context) => string.Join("\n", context.SelectedMentionables)) .AddComponentInteraction("channel", (ChannelMenuInteractionContext context) => string.Join("\n", context.SelectedChannels)) - .AddComponentInteraction("modal", (ModalInteractionContext context) => context.Components[0].Value) - .AddModules(typeof(Program).Assembly) - .UseGatewayEventHandlers(); + .AddComponentInteraction("modal", (ModalInteractionContext context) => context.Components[0].Value); + +// Add component interactions from modules +host.AddModules(typeof(Program).Assembly); + +// Add handlers to handle the component interactions +host.UseGatewayEventHandlers(); await host.RunAsync(); diff --git a/Documentation/guides/services/component-interactions/introduction.md b/Documentation/guides/services/component-interactions/introduction.md index 82bd732e..9417c587 100644 --- a/Documentation/guides/services/component-interactions/introduction.md +++ b/Documentation/guides/services/component-interactions/introduction.md @@ -6,19 +6,21 @@ uid: component-interactions ## [.NET Generic Host](#tab/generic-host) -Adding component interactions with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceServiceCollectionExtensions.AddComponentInteractions``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add a component interaction service to your host builder. Then, use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceHostExtensions.AddComponentInteraction* to add a component interaction using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. -[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=13-19,22-30)] +Adding component interactions with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceServiceCollectionExtensions.AddComponentInteractions``2(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add a component interaction service to your host builder. Then, use @NetCord.Hosting.Services.ComponentInteractions.ComponentInteractionServiceHostExtensions.AddComponentInteraction* to add a component interaction using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add component interaction modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. + +Unlike application commands, component interactions require maintaining context for each interaction, as the data between component interaction types can vary significantly. While it's possible to use a single context for all component interactions and cast them as needed, it's generally recommended to use distinct contexts for different types of component interactions. This is why multiple services are added to the service collection here - each one handles a specific type of component interaction. However, you likely won't need all of them, so feel free to remove the ones that aren't relevant to your use case. +[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=13-19,24-30,33,36)] ## [Bare Bones](#tab/bare-bones) First, add the following lines to the using section. -[!code-cs[Program.cs](Introduction/Program.cs#L4-L5)] +[!code-cs[Program.cs](Introduction/Program.cs#L3-L5)] -Now, it's time to create @NetCord.Services.ComponentInteractions.ComponentInteractionService`1 instance and add modules to it. In this example, we will use @NetCord.Services.ComponentInteractions.ButtonInteractionContext. -[!code-cs[Program.cs](Introduction/Program.cs#L12-L13)] +Now, it's time to create @NetCord.Services.ComponentInteractions.ComponentInteractionService`1 instance and add component interactions to it. In this example, we will use @NetCord.Services.ComponentInteractions.ButtonInteractionContext. +[!code-cs[Program.cs](Introduction/Program.cs#L12-L19)] We can add an interaction handler now. -[!code-cs[Program.cs](Introduction/Program.cs#L15-L32)] +[!code-cs[Program.cs](Introduction/Program.cs#L21-L43)] ### The Final Product diff --git a/Documentation/guides/services/text-commands/Introduction/Program.cs b/Documentation/guides/services/text-commands/Introduction/Program.cs index 139cc63c..3a0c8303 100644 --- a/Documentation/guides/services/text-commands/Introduction/Program.cs +++ b/Documentation/guides/services/text-commands/Introduction/Program.cs @@ -8,19 +8,30 @@ Intents = GatewayIntents.GuildMessages | GatewayIntents.DirectMessages | GatewayIntents.MessageContent, }); +// Create the command service CommandService commandService = new(); + +// Add commands using minimal APIs +commandService.AddCommand(["ping"], () => "Pong!"); + +// Add commands from modules commandService.AddModules(typeof(Program).Assembly); +// Add the handler to handle commands client.MessageCreate += async message => { + // Check if the message is a command (starts with '!' and is not from a bot) if (!message.Content.StartsWith('!') || message.Author.IsBot) return; + // Execute the command var result = await commandService.ExecuteAsync(prefixLength: 1, new CommandContext(message, client)); + // Check if the execution failed if (result is not IFailResult failResult) return; + // Return the error message to the user if the execution failed try { await message.ReplyAsync(failResult.Message); diff --git a/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs b/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs index ff807aa5..9247123d 100644 --- a/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs +++ b/Documentation/guides/services/text-commands/IntroductionHosting/Program.cs @@ -11,9 +11,15 @@ .AddDiscordGateway() .AddCommands(); -var host = builder.Build() - .AddCommand(["ping"], () => "Pong!") - .AddModules(typeof(Program).Assembly) - .UseGatewayEventHandlers(); +var host = builder.Build(); + +// Add a command using minimal APIs +host.AddCommand(["ping"], () => "Pong!"); + +// Add commands from modules +host.AddModules(typeof(Program).Assembly); + +// Add handlers to handle the commands +host.UseGatewayEventHandlers(); await host.RunAsync(); diff --git a/Documentation/guides/services/text-commands/introduction.md b/Documentation/guides/services/text-commands/introduction.md index cfbe19ba..813abbad 100644 --- a/Documentation/guides/services/text-commands/introduction.md +++ b/Documentation/guides/services/text-commands/introduction.md @@ -2,8 +2,8 @@ ## [.NET Generic Host](#tab/generic-host) -Adding commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.Commands.CommandServiceServiceCollectionExtensions.AddCommands``1(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add a command service to your host builder. Then, use @NetCord.Hosting.Services.Commands.CommandServiceHostExtensions.AddCommand* to add a command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. -[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=12,15-17)] +Adding commands with the .NET Generic Host is very easy. Use @NetCord.Hosting.Services.Commands.CommandServiceServiceCollectionExtensions.AddCommands``1(Microsoft.Extensions.DependencyInjection.IServiceCollection) to add the command service to your host builder. Then, use @NetCord.Hosting.Services.Commands.CommandServiceHostExtensions.AddCommand* to add a command using the minimal APIs way and/or use @NetCord.Hosting.Services.ServicesHostExtensions.AddModules(Microsoft.Extensions.Hosting.IHost,System.Reflection.Assembly) to add command modules from an assembly. You also need to use @NetCord.Hosting.Gateway.GatewayEventHandlerHostExtensions.UseGatewayEventHandlers(Microsoft.Extensions.Hosting.IHost) to bind the service event handlers. +[!code-cs[Program.cs](IntroductionHosting/Program.cs?highlight=12,17,20,23)] ### Specifying a prefix @@ -15,11 +15,11 @@ You can specify a prefix in the configuration. You can for example use `appsetti First, add the following lines to the using section. [!code-cs[Program.cs](Introduction/Program.cs#L3-L4)] -Now, it's time to create @NetCord.Services.Commands.CommandService`1 instance and add modules to it. -[!code-cs[Program.cs](Introduction/Program.cs#L11-L12)] +Now, it's time to create @NetCord.Services.Commands.CommandService`1 instance and add commands to it. +[!code-cs[Program.cs](Introduction/Program.cs#L11-L18)] We can add a command handler now. -[!code-cs[Program.cs](Introduction/Program.cs#L14-L31)] +[!code-cs[Program.cs](Introduction/Program.cs#L20-L42)] ### The Final Product diff --git a/README.md b/README.md index 95e71464..036da36e 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,10 @@ The following example sets up a bot with a minimal API-style approach for the `/ ```cs var builder = Host.CreateDefaultBuilder(args) .UseDiscordGateway() - .UseApplicationCommands(); + .UseApplicationCommands(); var host = builder.Build() - .AddSlashCommand("square", "Square!", (int a) => $"{a}² = {a * a}") + .AddSlashCommand("square", "Square!", (int a) => $"{a}² = {a * a}") .UseGatewayEventHandlers(); await host.RunAsync(); @@ -71,7 +71,7 @@ await host.RunAsync(); Moreover, you can use a module-based approach. Here's an example of a `/greet` command that greets a specified user: ```cs -public class GreetingModule : ApplicationCommandModule +public class GreetingModule : ApplicationCommandModule { [SlashCommand("greet", "Greet someone!")] public string Greet(User user) => $"{Context.User} greets {user}!";