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

开发Discord Bot遇AttributeError:discord模块无interaction属性

解决discord.py中AttributeError: module 'discord' has no attribute 'interaction'的问题

错误原因很明确:你在类型注解里写的discord.interaction是错误的,discord.py中交互对象的正确类型是大写开头的discord.Interaction,小写的interaction并非discord模块的有效属性。

修正后的代码如下:

@bot.tree.command(name="help")
async def help(interaction: discord.Interaction):
    await interaction.response.send_message(f"Hey, das ist ein test")

额外注意事项:

  • 确保你的discord.py版本为2.0及以上,因为bot.tree.command是Slash命令的实现,仅在2.0+版本支持。版本过低的话执行以下命令升级:
    pip install -U discord.py
    
  • 启动Bot后需要同步Slash命令到服务器,否则命令无法被用户调用,可在on_ready事件中添加同步逻辑:
    @bot.event
    async def on_ready():
        await bot.tree.sync()
        print(f"Bot {bot.user} ist online!")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:09:53