简体   繁体   English

单击按钮时如何发送模式?

[英]How do I send a modal when a button is clicked?

Im attempting to send a modal when the "ask" button is clicked from the embed.当从嵌入中单击“询问”按钮时,我试图发送模式。 在此处输入图像描述

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`\n\n・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?单击按钮时,询问按钮没有响应,我错过了什么吗?

This is nextcord , not discord.py .这是nextcord ,而不是discord.py In nextcord the order of the parameters in button callbacks is different.在 nextcord 中,按钮回调中的参数顺序是不同的。

Discord.py uses interaction, button , while nextcord uses button, interaction . Discord.py 使用interaction, button ,而 nextcord 使用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 .您的错误也表明了这一点-您正在调用interaction.response但错误说您调用Button.response

I would consider switching over to discord.py instead, though.不过,我会考虑改用discord.py

Two official examples side-to-side: discord.py - nextcord .并排的两个官方示例: discord.py - nextcord If you want to use a fork, make sure to look at the correct docs & examples.如果您想使用 fork,请务必查看正确的文档和示例。 You can't mix them across libraries.您不能在库中混合使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时,如何在Tkinter中弹出? - How do I make a pop up in Tkinter when a button is clicked? 单击时如何销毁循环中的特定按钮? - How do I destroy specific button in the loop when clicked? 右键单击时如何切换 tkinter 按钮文本 - How do I toggle tkinter button text when right clicked 单击按钮时如何禁用它? - How do you disable a button when it is clicked? 在python中单击另一个按钮时,如何停止按钮命令? - How do I stop a button command when another button is clicked in python? 在 Tkinter 中,我如何做到这样,当单击“显示密码按钮”时,它会与“隐藏密码”按钮切换 - In Tkinter how do I make it so when a “Show Password button” is clicked it is switched with a “Hide password” button 如何在单击时创建一个向安全URL发送HTTP GET请求的按钮? - How do I create a button that sends an HTTP GET request to a secure URL when clicked? 使用 pyviz/panel,如何仅在单击按钮时修改窗格中的值? - With pyviz/panel, how do I modify values in panes only when a button is clicked? 如何在 tkinter 中单击时禁用此按钮并说“已预订” - How do I make this button be disabled and say "Booked" when clicked in tkinter 第二次单击导航按钮时,如何阻止 Tkinter window 销毁? - How do I stop Tkinter window from destroy when the navigation button is clicked the second time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM