#Why modals are such a pain?

1 messages · Page 1 of 1 (latest)

true cairn
#
// creating modal
var modalBuilder = new ModalBuilder()
    .WithCustomId("modal.interaction")
    .WithTitle($"Update something")
    .AddTextInput("Name",
        "modal.interaction.name",
        TextInputStyle.Short,
        "Name!",
        1, 64, true);

await Context.Interaction.RespondWithModalAsync(modalBuilder.Build());
// IModal implementation for handling
public class UpdateModal : IModal
{
    public string Title { get; } = "Update something";
    
    [InputLabel("Name")]
    [RequiredInput(true)]
    [ModalTextInput(
        "modal.interaction.name",
        TextInputStyle.Short,
        "Name!",
        1,
        64)]
    public string Name { get; set; } = string.Empty;
}
// hanlding modal interaction
[ModalInteraction("modal.interaction")]
public async Task HandleUpdateModal(UpdateModal modal)
{
    Console.WriteLine(modal.Name);
}

Why Modal doesn't implement IModal???
Is there a better way to handle modal interactions?

#

Why modals are such a pain?

#

I'm thinking of creating of something like a mapper using reflection for this.

#

If there is no better way.

lethal grotto
#

ehm. You know you could use ...RespondWithModalAsync<UpdateModal>();

#

If that's what you meant

true cairn
#

It would be really nice if RespondWithModalAsync accepted Modal or ModalBuilder.

#

I should have shown this in my example.

lethal grotto
#

if the latter - then just make a generic modal class
and use it for handling

true cairn
true cairn
lethal grotto
#

ok
as I said
Make a generic modal & use it with a modal builder

#

all you need to careful with - field ids
that's the only thing that has to match

true cairn
#
  • #support-dnet message
rich widgetBOT
#
keylami
Message:

Continuation of the previous Generic Modal writeup. This one uses typeconverters.

// This code will handle all current layouts possible with modals, in 25 lines of code.
public class GenericInputModal<T1> : IModal
{
  public string Title => string.Empty;
  [ModalTextInput("first")]
  public T1 First {get;set;}
}
public class GenericInputModal<T1, T2> : GenericInputModal<T1>
{
  [ModalTextInput("second")]
  public T2 Second {get;set;}
}
public class GenericInputModal<T1, T2, T3> : GenericInputModal<T1,T2>
{
  [ModalTextInput("third")]
  public T3 Third {get;set;}
}
public class GenericInputModal<T1, T2, T3, T4> : GenericInputModal<T1, T2, T3>
{
    [ModalTextInput("fourth")]
    public T4 Fourth {get;set;}
}
public class GenericInputModal<T1, T2, T3, T4, T5> : GenericInputModal<T1, T2, T3, T4>
{
    [ModalTextInput("fifth")]
    public T5 Fifth {get;set;}
}
var ModalBuilderA = new ModalBuilder();
ModalBuilderA.WithTitle("Modal A");
ModalBuilderA.WithCustomId("modal-a");
ModalBuilderA.AddTe...
Quoted By:

@true cairn from #support-dnet in Discord.Net

lethal grotto
#

basically yeah

#

Ah, and also
actually I don't recall exactly if the title can be changed when using a "normal" modal class

#

the respond with modal async also has an overload that takes an instance of IModal

#

you could try setting the title through the constructor

true cairn