discord.py运行报TypeError: 'module' object is not callable问题求助
报错原因与修复方案
根因定位
TypeError: 'module' object is not callable错误触发位置为client = commands.bot(command_prefix = '__')代码行,核心原因有三点:
- 拼写错误:discord.py的
commands模块下用于实例化机器人的类是大写开头的Bot,你写的小写bot实际是子模块而非可调用的类,自然触发模块不可调用的报错。 - 重名文件冲突:如果你的项目目录下存在名为
commands.py或者discord.py的本地文件,导入时会优先加载本地文件而非discord官方的库模块,也会触发该错误。 - 版本迭代影响:如果你近期升级了discord.py到2.0及以上版本,旧版本的部分小写别名已被移除,同时新版本强制要求传入
intents参数才能正常启动。
修复步骤
- 修正类名大小写,将
commands.bot改为commands.Bot - 检查项目目录,删除/重命名和官方库重名的本地文件,同时删掉目录下的
__pycache__缓存文件夹 - 2.0+版本的用户补充声明intents参数
修正后可运行代码
import discord from discord.ext import commands token = 'token goess here' # 声明机器人意图,默认开启基础权限 intents = discord.Intents.default() # 如果需要读取用户消息内容触发命令,需要额外开启message_content权限,同时要在Discord开发者后台的机器人页面对应开关打开 intents.message_content = True client = commands.Bot(command_prefix = '__', intents=intents) @client.event async def on_ready(): print('Bot is ready') client.run(token)
内容的提问来源于stack exchange,提问作者Linkachus 17
相关产品推荐
相关产品推荐

