Why modal window does not shown? Library discord.py

Question:

i tried to do my first modal window in python, i am doing this in my cog with discord.py, but i don’t understand, why it doesn’t works?

When i typing $test command, bot gives nothing. No errors and no answers.

This is my cog code:

    from discord.ext import commands
    import discord
    
    class ModalTest(discord.ui.Modal, title='ModalTest'):
        name = discord.ui.TextInput(
            label='Name',
            placeholder='Your name here...',
        )
    
        feedback = discord.ui.TextInput(
            label='Something',
            style=discord.TextStyle.long,
            placeholder='placeholder',
            required=False,
            max_length=300,
        )
    
        async def on_submit(self, interaction: discord.Interaction):
            await interaction.response.send_message(f'Name, {self.name.value}!', ephemeral=True)
    
        async def on_error(self, interaction: discord.Interaction, error: Exception) -> None:
            await interaction.response.send_message('Oops! Something went wrong.', ephemeral=True)
    
    
    class test(commands.Cog):
        def __init__(self, client):
            self.client = client
    
        @commands.command(description='sad')
        async def test(self,interaction: discord.Interaction):
            await interaction.response.send_modal(ModalTest())
    async def setup(client):
        await client.add_cog(test(client)) 
Asked By: FlamyIce

||

Answers:

You can only send modals as a response to interactions (which are triggered by application commands), not regular message commands.

Your test command is annotated with @commands.command(), so it’s a message command. The parameter is incorrectly named (& type-annotated) as discord.Interaction while it’s actually commands.Context.

Turn your command into an application command (either slash command or context menu) if you want to use interaction-based features (like modals).

The reason why you can’t do this is because message commands only exist in your bot – Discord doesn’t know about them, or when or why they are triggered. If you could show modals, this would mean you’d be able to spam random modals in everyone’s face all the time. There would also be no way to tell who should get to see the modal.

Answered By: stijndcl
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.