Giter Club home page Giter Club logo

dsharpplus.modalcommands's Introduction

Table of content

1. Badges

Forks Stars Watchers Contributors

Issues Issues closed

Issues-pr Issues-pr closed PRs welcome

Version Downloads

Awesome Badges

2. What is this package

This is an extension for DSharpPlus to use modals like commands from CommandsNext.

3. Example

3.1 Registration

var modalCommands = _client.UseModalCommands(new ModalCommandsConfiguration()
{
    Services = _sp
});
modalCommands.RegisterModals(Assembly.GetExecutingAssembly());

3.2 Creating a modal

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.

3.3 Listen for modal submit

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());
    }
}

4. Custom arguments converter

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());

5. Credits

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

dsharpplus.modalcommands's People

Contributors

github-actions[bot] avatar xeroxdev avatar xeythhhh avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dsharpplus.modalcommands's Issues

[QUESTION]: <Dispose Implementation Error>

Consent

  • I have searched the existing issues and verified that there is no open issue for the same subject.

Question

Hey,

While trying to start my program, I continue to get an error about a dispose method not having an implementation. I did not see any related issue so I am wondering if I am doing something wrong.

System.TypeLoadException: 'Method 'Dispose' in type 'DSharpPlus.ModalCommands.ModalCommandsExtension' from assembly 'XeroxDev.DSharpPlus.ModalCommands, Version=1.3.1.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.'

[QUESTION]: Readme tutorial

Consent

  • I have searched the existing issues and verified that there is no open issue for the same subject.

Question

Hi,

On the readme, what is _sp at Services = _sp ?
I don't see any variable initialized with this name or type in the example.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.