[英]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.