You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击Embed中Ask按钮触发Modal失败,报错AttributeError问题排查

问题原因与修复方案

核心错误点

  • 构造函数拼写错误:askbutton类的构造函数写成了__int__,正确写法应为__init__。这个拼写失误导致View的初始化逻辑完全未执行,按钮的交互上下文无法正确绑定,最终触发'Button' object has no attribute 'response'错误。
  • View初始化参数错误:super().__init__("AskButton")不符合nextcord.ui.View的构造要求,该类初始化不需要传入标题参数,正确写法是super().__init__()(默认超时180秒,如需自定义可传入timeout参数)。

修复后的完整代码

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)
        await interaction.response.send_message(message, ephemeral=True)

class askbutton(nextcord.ui.View):
    def __init__(self):  # 修正拼写错误:__int__ → __init__
        super().__init__()  # 移除错误的标题参数

    @nextcord.ui.button(label="Ask", style= nextcord.ButtonStyle.red, custom_id = "ask button")
    async def asks(self, interaction: nextcord.Interaction, button: nextcord.ui.Button):
        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())

额外优化说明

  • 移除了回调函数中多余的return语句,异步操作完成后无需返回值。
  • 简化了字符串拼接的冗余括号,提升代码简洁度。

内容的提问来源于stack exchange,提问作者Alex Satoru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 21:20:43