Pycord 2.3.2报错AttributeError: discord模块无Bot属性求助
解决Pycord中
AttributeError: module 'discord' has no attribute 'Bot'的问题 问题重现
你遇到的报错:
Traceback (most recent call last): File "c:\Users\****\source\repos\DBot\main.py", line 16, in <module> bot = discord.Bot() AttributeError: module 'discord' has no attribute 'Bot'
测试代码:
import discord import os from dotenv import load_dotenv # 导入环境变量 load_dotenv() TOKEN = os.getenv("DISCORD_TOKEN") GUILD = os.getenv("DISCORD_GUILD") APP_ID = os.getenv("APP_ID") PUB_KEY = os.getenv("PUBLIC_KEY") GUILD_ID = os.getenv("GUILD_ID") GENERAL_CHAN_ID = os.getenv("CHANNEL_GENERAL_ID") # 初始化Bot bot = discord.Bot() @bot.slash_command() async def hello(ctx, name: str = None): name = name or ctx.author.name await ctx.respond(f"Hello {name}!") @bot.user_command(name="Say Hello") async def hi(ctx, user): await ctx.respond(f"{ctx.author.mention} says hello to {user.name}!") bot.run("token")
核心原因
你的Python环境中同时安装了discord.py和Pycord,两者共用同一个包名discord。当执行import discord时,Python优先导入了discord.py,而discord.py中没有Bot类(它使用的是commands.Bot),因此触发报错。
解决方案
1. 卸载冲突的discord.py
打开终端执行以下命令:
pip uninstall discord.py
如果不确定是否安装了discord.py,可以先执行pip list查看已安装包列表,确认是否存在discord.py。
2. 重新安装指定版本的Pycord(可选,确保环境纯净)
pip install --upgrade py-cord==2.3.2
3. 验证导入正确性
在代码开头添加以下代码,确认导入的是Pycord:
import discord print(discord.__version__) # 应输出2.3.2 print(discord.__file__) # 路径应指向py-cord的安装目录,而非discord.py
执行后如果输出正确版本号,即可正常运行你的命令代码。
内容的提问来源于stack exchange,提问作者null
相关产品推荐
相关产品推荐

