PawSharp.Commands
1.1.0-alpha.5.ci.10
This is a prerelease version of PawSharp.Commands.
dotnet add package PawSharp.Commands --version 1.1.0-alpha.5.ci.10
NuGet\Install-Package PawSharp.Commands -Version 1.1.0-alpha.5.ci.10
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PawSharp.Commands" Version="1.1.0-alpha.5.ci.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PawSharp.Commands" Version="1.1.0-alpha.5.ci.10" />
<PackageReference Include="PawSharp.Commands" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PawSharp.Commands --version 1.1.0-alpha.5.ci.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PawSharp.Commands, 1.1.0-alpha.5.ci.10"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PawSharp.Commands@1.1.0-alpha.5.ci.10
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PawSharp.Commands&version=1.1.0-alpha.5.ci.10&prerelease
#tool nuget:?package=PawSharp.Commands&version=1.1.0-alpha.5.ci.10&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
PawSharp.Commands
PawSharp.Commands is a comprehensive, attribute-based command framework for Discord bots built on PawSharp.Client. It provides a clean, extensible API for prefix commands, slash commands, and context menu commands with advanced features like type conversion, middleware, dependency injection, and more.
Features
Core Features
- Attribute-driven command definitions - Use
[Command],[SlashCommand],[UserContextMenu],[MessageContextMenu]attributes - Async command handlers - Full async/await support
- Module organization - Organize commands in modules with lifecycle hooks
- Aliasing - Multiple command names for the same handler
- Descriptions - Rich metadata for help systems
Advanced Features
- Type conversion - Automatic conversion of string arguments to C# types (int, bool, ulong, DateTime, etc.)
- Advanced argument parsing - Quote handling, escape characters, variadic arguments
- Optional parameters -
[Optional]attribute for parameters with default values - Remaining arguments -
[Remaining]attribute to capture all remaining text - Middleware pipeline - Global and command-specific middleware for logging, auditing, timeouts
- Dependency injection - Full DI support with Microsoft.Extensions.DependencyInjection
- Precondition system - Reusable permission checks (guild, DM, NSFW, role, owner, cooldowns)
- Compiled delegates - Performance optimization using compiled delegates instead of reflection
- Structured error handling - Error codes and user-friendly error messages
- Help system - Built-in help command generation
- Command discovery - API for discovering and querying commands
Slash Commands
- Auto-registration - Automatic Discord API registration with bulk operations
- Rich options - Choices, min/max values, min/max lengths, channel type restrictions
- Subcommands and groups - Hierarchical command organization
- Autocomplete support -
[SlashAutocomplete]attribute - Type mapping - Support for User, Channel, Role, Mentionable, Attachment types
- Localization -
[SlashLocalizedName]and[SlashLocalizedDescription]attributes - NSFW support -
[SlashNsfw]attribute - DM permissions -
[SlashDmPermission]attribute - Default permissions -
[SlashDefaultPermission]attribute
Context Menu Commands
- User context menus - Right-click on users
- Message context menus - Right-click on messages
- Auto-registration - Automatic Discord API registration
Requirements
- .NET 10 (
net10.0) PawSharp.ClientPawSharp.APIPawSharp.CoreMicrosoft.Extensions.DependencyInjectionMicrosoft.Extensions.Logging
Installation
dotnet add package PawSharp.Commands --version 1.1.0-alpha.4
Quick Start
Basic Prefix Commands
using PawSharp.Client;
using PawSharp.Commands;
var commands = client.UseCommands(prefix: "!");
public sealed class GeneralCommands : BaseCommandModule
{
[Command("ping")]
[Description("Check whether the bot is responsive")]
public async Task PingAsync(CommandContext ctx)
=> await ctx.ReplyAsync("Pong!");
}
commands.RegisterModule(client, new GeneralCommands());
Type Conversion
[Command("ban")]
[Description("Ban a user by ID")]
public async Task BanAsync(CommandContext ctx, ulong userId, string reason = "No reason provided")
{
// userId is automatically converted from string to ulong
await ctx.ReplyAsync($"Banning user {userId} for: {reason}");
}
Advanced Argument Parsing
[Command("echo")]
[Description("Echo back a message (supports quotes)")]
public async Task EchoAsync(CommandContext ctx, [Remaining] string message)
{
// Captures all remaining text, including quotes
await ctx.ReplyAsync(message);
}
// Usage: !echo "hello world" -> outputs: hello world
Optional Parameters
[Command("greet")]
[Description("Greet a user")]
public async Task GreetAsync(CommandContext ctx, string name, [Optional] string title = "")
{
var greeting = string.IsNullOrEmpty(title) ? $"Hello, {name}!" : $"Hello, {title} {name}!";
await ctx.ReplyAsync(greeting);
}
Slash Commands
[SlashCommand("greet", "Greets a user")]
public async Task GreetAsync(
InteractionCreateEvent interaction,
[SlashOption("name", "User name")] string name,
[SlashOption("title", "User title", Required = false)] string title = "")
{
var greeting = string.IsNullOrEmpty(title) ? $"Hello, {name}!" : $"Hello, {title} {name}!";
await interaction.ResponseAsync(new InteractionResponse
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new InteractionApplicationCommandCallbackData { Content = greeting }
});
}
// Register slash commands
await commands.BulkRegisterSlashModulesAsync(client, modules, applicationId, guildId: guildId);
Slash Commands with Choices
[SlashCommand("role", "Assign a role")]
public async Task RoleAsync(
InteractionCreateEvent interaction,
[SlashOption("role", "Role to assign")]
[SlashChoice("Admin", "admin")]
[SlashChoice("Moderator", "mod")]
[SlashChoice("Member", "member")]
string role)
{
await interaction.ResponseAsync(new InteractionResponse
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new InteractionApplicationCommandCallbackData { Content = $"Assigning role: {role}" }
});
}
Context Menu Commands
[UserContextMenu("Ban User")]
public async Task BanUserAsync(InteractionCreateEvent interaction)
{
var userId = interaction.Data?.TargetId;
// Handle ban logic
await interaction.ResponseAsync(new InteractionResponse
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new InteractionApplicationCommandCallbackData { Content = "User banned!" }
});
}
[MessageContextMenu("Delete Message")]
public async Task DeleteMessageAsync(InteractionCreateEvent interaction)
{
// Handle message deletion
await interaction.ResponseAsync(new InteractionResponse
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new InteractionApplicationCommandCallbackData { Content = "Message deleted!" }
});
}
// Register context menu commands
await commands.BulkRegisterContextMenuModulesAsync(client, modules, applicationId, guildId: guildId);
Preconditions
using PawSharp.Commands.Preconditions;
using PawSharp.Commands.Permissions;
[RequireGuild]
[RequirePermissions(DiscordPermissions.BanMembers | DiscordPermissions.KickMembers)]
[Cooldown(maxUses: 5, perSeconds: 60, bucketType: CooldownBucketType.User)]
[Command("mod")]
[Description("Moderator command")]
public async Task ModCommandAsync(CommandContext ctx)
{
await ctx.ReplyAsync("Moderation command executed!");
}
[RequireNsfw]
[Command("nsfw")]
[Description("NSFW-only command")]
public async Task NsfwCommandAsync(CommandContext ctx)
{
await ctx.ReplyAsync("NSFW content!");
}
[RequireOwner(123456789UL)]
[Command("admin")]
[Description("Owner-only command")]
public async Task AdminCommandAsync(CommandContext ctx)
{
await ctx.ReplyAsync("Admin command executed!");
}
Dependency Injection
// Setup DI
var services = new ServiceCollection();
services.AddCommands(prefix: "!", options =>
{
options.WithPrefix("!");
options.WithCaseSensitivity(false);
options.WithExecutionTimeout(TimeSpan.FromMinutes(5));
options.WithLoggingMiddleware();
options.WithAuditMiddleware();
});
// Register modules
services.AddTransient<MyCommandModule>();
var serviceProvider = services.BuildServiceProvider();
var commands = serviceProvider.GetRequiredService<CommandsExtension>();
// Register with client
commands.RegisterModuleAsync(client, serviceProvider.GetRequiredService<MyCommandModule>());
Custom Middleware
public class CustomMiddleware : IMiddleware
{
public async Task InvokeAsync(CommandContext context, Func<Task> next)
{
// Pre-execution logic
Console.WriteLine($"Command {context.CommandName} invoked by {context.User.Id}");
await next();
// Post-execution logic
Console.WriteLine($"Command {context.CommandName} completed");
}
}
// Register middleware
services.AddCommandMiddleware<CustomMiddleware>();
Custom Type Converters
public class TimeSpanConverter : SyncTypeConverter<TimeSpan>
{
protected override TypeConverterResult<TimeSpan> ConvertSync(string value, CommandContext context)
{
if (TimeSpan.TryParse(value, out var result))
return TypeConverterResult<TimeSpan>.FromSuccess(result);
return TypeConverterResult<TimeSpan>.FromError($"Unable to parse '{value}' as a time span");
}
}
// Register custom converter
commands.TypeConverterService.RegisterConverter(new TimeSpanConverter());
Help System
// The built-in HelpModule provides automatic help generation
services.AddTransient<HelpModule>();
// Register the module
commands.RegisterModule(client, serviceProvider.GetRequiredService<HelpModule>());
// Usage: !help - shows all commands
// Usage: !help ping - shows help for specific command
Command Discovery
var discovery = new CommandDiscoveryService(commands);
// Get all commands
var allCommands = discovery.GetAllCommands();
// Get specific command
var pingCommand = discovery.GetCommand("ping");
// Search commands
var matchingCommands = discovery.SearchCommands("ban");
// Discover command modules from assembly
var moduleTypes = CommandDiscoveryService.DiscoverCommandModules(Assembly.GetExecutingAssembly());
Error Handling with Structured Errors
using PawSharp.Commands.Errors;
commands.CommandErrored = async args =>
{
var error = CommandError.UnexpectedError(args.Exception);
await args.Context.RespondAsync($"Error: {error.Message} (Code: {error.Code})");
};
Slash Command Localization
[SlashCommand("greet", "Greets a user")]
[SlashLocalizedName("en-US", "greet")]
[SlashLocalizedName("fr-FR", "saluer")]
[SlashLocalizedDescription("en-US", "Greets a user")]
[SlashLocalizedDescription("fr-FR", "Salue un utilisateur")]
public async Task GreetAsync(
InteractionCreateEvent interaction,
[SlashOption("name", "User name")]
[SlashLocalizedName("en-US", "name")]
[SlashLocalizedName("fr-FR", "nom")]
string name)
{
// ...
}
Slash Command Permissions
[SlashCommand("admin", "Admin command")]
[SlashNsfw] // NSFW-only
[SlashDmPermission(false)] // Disable in DMs
[SlashDefaultPermission(false)] // Disabled by default
public async Task AdminCommandAsync(InteractionCreateEvent interaction)
{
// ...
}
Module Lifecycle
public class MyModule : BaseCommandModule
{
public override Task InitializeAsync()
{
// Called during module registration
Console.WriteLine("Module initialized!");
return Task.CompletedTask;
}
public override Task BeforeExecutionAsync(CommandContext ctx)
{
// Called before each command in this module
Console.WriteLine($"Before: {ctx.CommandName}");
return Task.CompletedTask;
}
public override Task AfterExecutionAsync(CommandContext ctx)
{
// Called after each command in this module
Console.WriteLine($"After: {ctx.CommandName}");
return Task.CompletedTask;
}
}
Error Handling
commands.CommandErrored = async args =>
{
var errorMessage = args.Exception.Message;
await args.Context.ReplyAsync($"Error: {errorMessage}");
};
Typical Use Cases
- Complex prefix command bots - With type conversion and advanced parsing
- Moderation bots - With permission checks and cooldowns
- Slash command bots - With rich options and autocomplete
- Context menu bots - For user and message interactions
- Enterprise bots - With DI, middleware, and logging
Related Packages
PawSharp.Client: the host client for command executionPawSharp.Interactions: slash command and component workflowsPawSharp.Core: shared models and enumsPawSharp.API: REST API client
Documentation
- Main repository guide: ../../README.md
- Package source: ./
Support
- Join the PawSharp Discord for help, discussion, and community.
- Report bugs or request features via GitHub Issues.
- Start a discussion on GitHub Discussions.
License
MIT. See ../../LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.0)
- Microsoft.Extensions.Logging (>= 10.0.0)
- PawSharp.API (>= 1.1.0-alpha.5.ci.10)
- PawSharp.Client (>= 1.1.0-alpha.5.ci.10)
- PawSharp.Core (>= 1.1.0-alpha.5.ci.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0-alpha.5.ci.10 | 108 | 7/14/2026 |
| 1.1.0-alpha.5 | 88 | 7/14/2026 |
| 1.1.0-alpha.4.ci.9 | 93 | 6/29/2026 |
| 1.1.0-alpha.4 | 87 | 6/29/2026 |
| 1.1.0-alpha.3 | 87 | 6/15/2026 |
| 1.1.0-alpha.2.ci.7 | 79 | 5/4/2026 |
| 1.1.0-alpha.2 | 73 | 5/4/2026 |
| 1.0.0-alpha.4.ci.4 | 90 | 4/27/2026 |
| 1.0.0-alpha.4 | 96 | 4/23/2026 |
| 1.0.0-alpha.2.ci.3 | 86 | 4/15/2026 |
| 1.0.0-alpha.2 | 82 | 4/9/2026 |
| 0.11.0-alpha.1 | 105 | 3/10/2026 |
| 0.10.0-alpha.3 | 89 | 3/8/2026 |
| 0.7.0-alpha.1 | 90 | 3/6/2026 |
| 0.6.1-alpha1 | 92 | 3/4/2026 |
| 0.6.0-alpha1 | 92 | 2/25/2026 |
| 0.5.0-alpha13 | 91 | 2/22/2026 |
| 0.5.0-alpha12 | 90 | 2/22/2026 |
| 0.5.0-alpha11 | 95 | 2/20/2026 |
Loading failed