Kuylar.DSharpPlus.ButtonCommands
1.0.0
See the version list below for details.
dotnet add package Kuylar.DSharpPlus.ButtonCommands --version 1.0.0
NuGet\Install-Package Kuylar.DSharpPlus.ButtonCommands -Version 1.0.0
<PackageReference Include="Kuylar.DSharpPlus.ButtonCommands" Version="1.0.0" />
paket add Kuylar.DSharpPlus.ButtonCommands --version 1.0.0
#r "nuget: Kuylar.DSharpPlus.ButtonCommands, 1.0.0"
// Install Kuylar.DSharpPlus.ButtonCommands as a Cake Addin #addin nuget:?package=Kuylar.DSharpPlus.ButtonCommands&version=1.0.0 // Install Kuylar.DSharpPlus.ButtonCommands as a Cake Tool #tool nuget:?package=Kuylar.DSharpPlus.ButtonCommands&version=1.0.0
DSharpPlus.ButtonCommands
An extension for DSharpPlus to use buttons like commands from CommandsNext
Examples
Example code can be found in the DSharpPlus.ButtonCommands.TestBot
project. You can run that project after setting the TOKEN
environment variable to a valid bot token.
Documentation
Setup
You can register a ButtonCommandsExtension
on your DiscordClient
, similar to how you register a CommandsNextExtension
.
ButtonCommandsExtension buttonCommands = discord.UseButtonCommands();
You can also pass in a ButtonCommandsConfiguration
if you want to customize the button ID handling.
ButtonCommandsExtension buttonCommands = discord.UseButtonCommands(new ButtonCommandsConfiguration() {
ArgumentSeparator = "-",
Prefix = "?"
});
Creating and registering button commands
Just like CommandsNext, to add button commands, you should create a new class that inherits from ButtonCommandModule
.
public class MyButtonCommands : ButtonCommandModule
{ }
Now, you can add a method that will be your button command. Make sure to add the ButtonCommand
attribute on top of it.
[ButtonCommand("ban")]
public async Task BanMemberButton(ButtonContext context, DiscordMember member)
{ }
Note: Don't forget that button IDs have a 100 character limit, so try not to have long command names, since the arguments will also gonna be stored in that 100 characters.
Now, you can write the actual command code in that method. Don't forget to respond to the interaction.
[ButtonCommand("ban")]
public async Task BanMemberButton(ButtonContext context, DiscordMember member, string reason)
{
context.Guild.BanMemberAsync(member, 7, reason);
await context.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"{context.Member.Mention} has just banned {member.Username} ({member.Id})!"));
}
Now, for this command to get called, you have to register it where you called UseButtonCommands
. There are 2 ways to register a command module.
ButtonCommandsExtension buttonCommands = client.UseButtonCommands();
buttonCommands.RegisterButtons<MyButtonCommands>();
While this method would work fine, it might cause confusion later on, or might look bad. So, unless you want to have some conditions per module, you can just register the entire assembly.
ButtonCommandsExtension buttonCommands = client.UseButtonCommands();
buttonCommands.RegisterButtons(Assembly.GetExecutingAssembly());
Using button commands
To use button commands, you can just create any interaction with the id as your command name and arguments, formatted as following
<prefix><commandname><separator><parameter1><separator><parameter2>...
Example:
?ban-310651646302486528
Make sure that the prefix and the separators are the ones that you set in the ButtonCommandsConfiguration
. By default, the prefix is @
and the separator is .
..
Try not to use the prefixes and separators in any of the parameters, since they may cause issues while reading the button ID
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- DSharpPlus (>= 4.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.