This is an extension for DSharpPlus to use modals like commands from CommandsNext.
var modalCommands = _client.UseModalCommands(new ModalCommandsConfiguration()
{
Services = _sp
});
modalCommands.RegisterModals(Assembly.GetExecutingAssembly());
var modal = ModalBuilder.Create("food")
.WithTitle("Super cool modal!")
.AddComponents(new TextInputComponent("Favorite food", "fav-food", "Pizza, Icecream, etc", max_length: 30))
.AddComponents(new TextInputComponent("Why?", "why-fav", "Because it tastes good", required: false, style: TextInputStyle.Paragraph));
You can also create a modal in the normal way, but remember to add the Prefix
set in ModalCommandsConfiguration
(
Default >
) with WithCustomId
.
public class FoodModal : ModalCommandModule
{
[ModalCommand("food")]
public async Task GetFavFoodAsync(ModalContext ctx, string food, string reason)
{
await ctx.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"Your favorite food is: {food}. Reason: {reason}").AsEphemeral());
}
}
By default, only the following arguments are supported:
- string
- bool
- int
- uint
- long
- ulong
- float
- double
- DiscordUser
- DiscordMember
- DiscordRole
- DiscordChannel
If you want to add more arguments, you can create a new argument converter like so:
public class IntConverter : IModalArgumentConverter<int>
{
// This method will convert the string to an int.
public Task<Optional<int>> ConvertAsync(string value, ModalContext ctx)
{
return Task.FromResult(int.TryParse(value, out var res) ? Optional.FromValue(res) : Optional.FromNoValue<int>());
}
// This method will convert the int to a string.
public string ConvertToString(int value) => value.ToString();
}
After writing your argument converter, you can register it in your ModalCommandsExtension like so:
buttonCommands.RegisterConverter(new ShortConverter());
I stole nearly all of this from Kuylars DSharpPlus.ButtonCommands extension
And because of this: If something doesn't work, blame Kuylar. I'm a lazy person :P