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
补充说明
- 建议用
isinstance(child, discord.ui.Button)代替type(child) == discord.ui.Button,这样能兼容子类情况,代码更健壮。 - 修正参数顺序后,
interaction会正确接收Interaction对象,response和message属性就能正常调用,功能可以正常实现。
内容的提问来源于stack exchange,提问作者adv_nomad
相关产品推荐
相关产品推荐

