[SOLVED] 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 is nextcord, not discord.py. In nextcord the order of the parameters in button callbacks is different.

Discord.py uses interaction, button, while nextcord uses button, interaction. Swap the order of your parameters.

Your error also suggests this – you’re calling interaction.response but the error says you called Button.response.

I would consider switching over to discord.py instead, though.

Two official examples side-to-side: discord.pynextcord. If you want to use a fork, make sure to look at the correct docs & examples. You can’t mix them across libraries.


This Question was asked in StackOverflow by Alex Satoru and Answered by stijndcl It 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?