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

Discord.py视图按钮点击报错:无法访问Interaction的response属性

问题解决:按钮回调参数顺序错误导致的AttributeError

你的代码报错核心原因是按钮回调函数的参数顺序写反了。在discord.py的discord.ui.button装饰器对应的回调函数中,正确的参数顺序是:self → interaction: discord.Interaction → button: discord.ui.Button。

你现在把button和interaction的位置调换了,导致原本应该接收Interaction对象的参数,实际接收到的是Button对象,自然会触发'Button' object has no attribute 'response'的错误。

修正后的代码

import discord

class Signup(discord.ui.View):
    def __init__(self):
        super().__init__()

    @discord.ui.button(label="Button 1")
    # 调整参数顺序:先interaction,再button
    async def one_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.defer()

        for child in self.children:
            if isinstance(child, discord.ui.Button) and child.label == "Button 2":
                child.disabled = False
                await interaction.message.edit(view=self)

        await interaction.followup.send("Button 1 Pressed, Button 2 Enabled")

    @discord.ui.button(label="Button 2", disabled=True)
    # 同样调整这里的参数顺序
    async def two_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
        pass

补充说明

  1. 建议用isinstance(child, discord.ui.Button)代替type(child) == discord.ui.Button,这样能兼容子类情况,代码更健壮。
  2. 修正参数顺序后,interaction会正确接收Interaction对象,response和message属性就能正常调用,功能可以正常实现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 20:20:17