How do I send a modal when a button is clicked?

Issue

This Content is from Stack Overflow. Question asked by Alex Satoru

Im attempting to send a modal when the “ask” button is clicked from the embed. enter image description here

Error message:

AttributeError: 'Button' object has no attribute 'response'
class AskModal(nextcord.ui.Modal):
    def __init__(self):
        super().__init__(
            "Ask",
        )
    
        self.emQuestion = nextcord.ui.TextInput(label = "Question", required = True, placeholder = "What level shall I reach to stay in the group?", style = nextcord.TextInputStyle.paragraph)
        self.add_item(self.emQuestion)

    async def callback(self, interaction: nextcord.Interaction) -> None:
        question = self.emQuestion.value
        channel = bot.get_channel(1017234639770894377)
        questionmsg = (f"{question} ^ {interaction.user.id}")
        message = ("Your question has been sent!")

        await channel.send(questionmsg)

        return await interaction.response.send_message(message, ephemeral=True)

class askbutton(nextcord.ui.View):
    def __int__(self):
        super().__init__(
            "AskButton",
        )

    @nextcord.ui.button(label="Ask", style= nextcord.ButtonStyle.red, custom_id = "ask button")
    async def asks(self, interaction: nextcord.Interaction, button: nextcord.ui.Button):
        return await interaction.response.send_modal(AskModal())

@bot.command()
async def ask(ctx) :
    embed = nextcord.Embed(title="QNA", description="・You may ask a question by using the slash command `/ask`nn・You'll be notified that your question has been answered by receiving a ping in the <#928507764714651698> channel", color= 0x303136)
    await ctx.send(embed = embed, view = askbutton())

The ask button doesn’t respond when the button is clicked, did I miss something?



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?