开发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
相关产品推荐
相关产品推荐

