Adding application commands with the .NET Generic Host is very easy. Use AddApplicationCommands<TInteraction, TContext>(IServiceCollection) to add the application command service to your host builder. Then, use AddSlashCommand, AddUserCommand or AddMessageCommand to add an application command using the minimal APIs way and/or use AddModules(IHost, Assembly) to add application command modules from an assembly. You also need to use UseGatewayEventHandlers(IHost) to bind the service event handlers.
using Microsoft.Extensions.Hosting;
using NetCord;
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<ApplicationCommandInteraction, ApplicationCommandContext>();
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();
First, add the following lines to the using section.
using NetCord.Rest;
using NetCord.Services;
using NetCord.Services.ApplicationCommands;
Now, it's time to create ApplicationCommandService<TContext> instance and add application commands to it. You can do it by using AddSlashCommand, AddUserCommand or AddMessageCommand to add an application command using the minimal APIs way and/or by using AddModules(Assembly) to add application command modules from an assembly.
// Create the application command service
ApplicationCommandService<ApplicationCommandContext> 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);
We can add a command handler now. If you used other context than SlashCommandContext, you should change the interaction type of the handler to the appropriate one.
// Add the handler to handle interactions
client.InteractionCreate += async interaction =>
{
// Check if the interaction is an application command interaction
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;
// 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));
}
catch
{
}
};
Now, we should send the commands to Discord, to make them usable. Add the following line under the handler:
// Create the commands so that you can use them in the Discord client
await applicationCommandService.CreateCommandsAsync(client.Rest, client.Id);
The Final Product
Program.cs
using NetCord;
using NetCord.Gateway;
using NetCord.Rest;
using NetCord.Services;
using NetCord.Services.ApplicationCommands;
GatewayClient client = new(new BotToken("Token from Discord Developer Portal"), new GatewayClientConfiguration()
{
Intents = default,
});
// Create the application command service
ApplicationCommandService<ApplicationCommandContext> 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 =>
{
// Check if the interaction is an application command interaction
if (interaction is not ApplicationCommandInteraction applicationCommandInteraction)
return;
// 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));
}
catch
{
}
};
// Create the commands so that you can use them in the Discord client
await applicationCommandService.CreateCommandsAsync(client.Rest, client.Id);
client.Log += message =>
{
Console.WriteLine(message);
return default;
};
await client.StartAsync();
await Task.Delay(-1);