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

Discord Bot报错:'Bot'对象无'interaction'属性求助

解决Discord Bot中'Bot' object has no attribute 'interaction'错误

这个错误的核心原因是discord.py的Bot类本身不存在interaction属性,你混淆了不同Discord机器人库的用法,或者用了错误的斜杠命令实现方式。以下是具体的修复步骤:

1. 确保使用正确的discord.py版本

斜杠命令需要discord.py v2.0及以上版本,先检查当前版本:

pip show discord.py

如果版本低于2.0,执行升级:

pip install -U discord.py

2. 正确实现斜杠命令

discord.py中通过bot.tree来处理斜杠命令,而非interaction属性。以下是基础的正确示例:

import discord
from discord.ext import commands
from discord import app_commands

# 配置必要的intents(v2.0+必须)
intents = discord.Intents.default()
intents.message_content = True  # 若需处理消息内容,开启该权限

bot = commands.Bot(command_prefix="!", intents=intents)

# 定义斜杠命令
@bot.tree.command(name="hello", description="发送问候消息")
async def hello(interaction: discord.Interaction):
    await interaction.response.send_message(f"你好,{interaction.user.mention}!")

# 机器人就绪后同步命令到服务器
@bot.event
async def on_ready():
    await bot.tree.sync()  # 全局同步,如需指定服务器可添加guild参数
    print(f"已登录为 {bot.user}")

# 启动机器人
bot.run("你的机器人令牌")

3. 替换错误的启动逻辑

你之前尝试的bot.interaction.start或interaction.start都是错误的,discord.py中只需调用bot.run("你的令牌")即可启动机器人,斜杠命令的注册和同步通过bot.tree完成。

额外注意事项

  • 如果需要在特定服务器同步命令,可在bot.tree.sync()中传入guild=discord.Object(id=服务器ID),避免全局同步的延迟
  • 确保机器人在Discord开发者门户中开启了message_content intent(如果业务需要用到的话)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 23:47:39