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

Discord机器人报错AttributeError: 'diff_btns'对象无'ctx'属性求助

解决Discord机器人AttributeError: 'diff_btns' object has no attribute 'ctx'错误

问题概述

开发小型服务器Discord机器人时,实现了带Easy/Medium/Hard按钮的交互消息,要求仅命令发起者能操作按钮,但运行时触发错误:AttributeError: 'diff_btns' object has no attribute 'ctx',错误出现在interaction_check方法中试图访问self.ctx.author的位置。

错误原因

你的diff_btns类(继承自discord.ui.View)没有存储ctx对象,创建View实例时只是调用diff_btns(),没有将命令的ctx传入类中,导致类内部无法访问self.ctx。另外按钮回调里还有一个隐藏问题:调用interaction.response.send_message后再调用interaction.response.edit_message会报错,因为同一个interaction的response只能被使用一次。

修复方案

  1. 给diff_btns类添加__init__方法,接收并保存ctx参数
  2. 在quizgk命令中创建View实例时传入ctx:diff_btns(ctx)
  3. 将按钮回调里的interaction.response.edit_message替换为interaction.edit_original_response,因为发送消息后需要修改原消息的View,而不是重复使用response

修复后完整代码

#Import libraries:
from discord.interactions import Interaction
from discord.ui import Button, View
import discord
import quiz
'''
code to get question:
quiz.get_data(quiz.diff1)
await ctx.respond(quiz.q_output)
'''


bot = discord.Bot()


@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')
    

class diff_btns(discord.ui.View):
    def __init__(self, ctx):
        super().__init__()
        self.ctx = ctx  # 保存ctx对象
    
    @discord.ui.button(label="Easy", row=0, style=discord.ButtonStyle.blurple)
    async def button_callback1(self, button, interaction):
        await interaction.response.send_message("You picked easy")
        for child in self.children:
            child.disabled = True
        await interaction.edit_original_response(view=self)  # 修改原消息的View
    
    @discord.ui.button(label="Medium", row=0, style=discord.ButtonStyle.blurple)
    async def button_callback2(self, button, interaction):
        await interaction.response.send_message("You picked medium")
        for child in self.children:
            child.disabled = True
        await interaction.edit_original_response(view=self)

    @discord.ui.button(label="Hard", row=0, style=discord.ButtonStyle.blurple)
    async def button_callback3(self, button, interaction):
        await interaction.response.send_message("You picked hard")
        for child in self.children:
            child.disabled = True
        await interaction.edit_original_response(view=self)
    
    async def interaction_check(self, interaction) -> bool:
        if interaction.user != self.ctx.author:
            await interaction.response.send_message("Use /gkquiz to make your own quiz.", ephemeral=True)
            return False
        else:
            return True

#Quiz question:
@bot.command(description="Starts a general knowledge quiz.")
async def quizgk(ctx):
    #Send message,传入ctx到View实例
    await ctx.respond("How difficult do you want the questions to be?", view=diff_btns(ctx))


bot.run()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 03:40:32