Discord.Net icon indicating copy to clipboard operation
Discord.Net copied to clipboard

[Bug]: `ModalInteractionAttribute` handlers do not allow for a handler with no parameters

Open lfshr opened this issue 1 year ago • 6 comments

Check The Docs

  • [X] I double checked the docs and couldn't find any useful information.

Verify Issue Source

  • [X] I verified the issue was caused by Discord.Net.

Check your intents

  • [X] I double checked that I have the required intents.

Description

Handlers defined via ModalInteractionAttribute do not allow for a handler with no parameters. This stops the modal handler from being called after generating a modal via the fluent API.

Version

3.10.0

Working Version

No response

Logs

System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
   at Discord.Interactions.Builders.ModuleClassBuilder.IsValidModalCommanDefinition(MethodInfo methodInfo)
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at Discord.Interactions.Builders.ModuleClassBuilder.BuildModule(ModuleBuilder builder, TypeInfo typeInfo, InteractionService commandService, IServiceProvider services)
   at Discord.Interactions.Builders.ModuleClassBuilder.BuildAsync(IEnumerable`1 validTypes, InteractionService commandService, IServiceProvider services)
   at Discord.Interactions.InteractionService.AddModulesAsync(Assembly assembly, IServiceProvider services)
   at DiscordBot.V2.Workers.DiscordBotWorker.ExecuteAsync(CancellationToken stoppingToken) in D:\repos\PoliceMP-Discord\DiscordBot.V2\Workers\DiscordInteractionsWorker.cs:line 37
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at Program.<Main>$(String[] args) in D:\repos\PoliceMP-Discord\DiscordBot.V2\Program.cs:line 52

Sample

    [ModalInteraction("my-modal")]
    public async Task ModalInteract()
    {
    }

Packages

N/A

lfshr avatar Jun 26 '23 09:06 lfshr

ModalInteractions require anything that inherits IModal as the parameter. Try using IModal as the parameter; so it would become:

    [ModalInteraction("my-modal")]
    public async Task ModalInteract(IModal modal)
    {
    }

exsersewo avatar Jun 26 '23 09:06 exsersewo

ModalInteractions require anything that inherits IModal as the parameter. Try using IModal as the parameter; so it would become:

    [ModalInteraction("my-modal")]
    public async Task ModalInteract(IModal modal)
    {
    }

This doesn't work as you can't create instances of an interface. You can create a stub modal and use that as a workaround.

public class StubModal : IModal
{
    public string Title => string.Empty;
}

[ModalInteraction("my-modal")]
public async Task ModalInteract(StubModal _)
{
    
}

lfshr avatar Jun 26 '23 10:06 lfshr

Closing due to lack of followup

Misha-133 avatar Aug 11 '23 22:08 Misha-133

Closing due to lack of followup

Hi @Misha-133.

I'm confused as to what I had to follow up with here. The issue still persists, regardless of the fact that there is a workaround present.

lfshr avatar Aug 11 '23 22:08 lfshr

Huh, yeah, sorry, your previous reply didn't load in mobile app...


Speaking about the issue tho I believe that is not a workaround rather that's the intended behavior. It's not possible to create a modal with no fields - you need to add at least one, hence the interaction service has to have a place where to store the data. I shall ask @quinchs 's thoughts on this... but IMO it behaves correctly

Misha-133 avatar Aug 11 '23 22:08 Misha-133

Huh, yeah, sorry, your previous reply didn't load in mobile app...

Speaking about the issue tho I believe that is not a workaround rather that's the intended behavior. It's not possible to create a modal with no fields - you need to add at least one, hence the interaction service has to have a place where to store the data. I shall ask @quinchs 's thoughts on this... but IMO it behaves correctly

If it's desired behaviour then I think the error message should at least be changed. The reason we hit this issue is because we generate the model based on configuration, so need to reflect on the answer rather than have a concrete set of answers.

lfshr avatar Aug 14 '23 10:08 lfshr