Python开发Discord Bot遇TypeError:'module'对象不可调用,如何解决?
解决Discord机器人TypeError: 'module' object is not callable错误
问题代码
from discord.ext import commands import discord BOT_TOKEN = 'REDACTED' CHANNEL_ID = 1234 # Not actual CHANNEL_ID int due to privacy bot = commands.bot(command_prefix = '!', intents = discord.Intents.all()) @bot.event async def on_ready(): print('Bot is ready') channel = bot.get_channel(CHANNEL_ID) await channel.send('Bot is ready') bot.run(BOT_TOKEN)
报错信息
Traceback (most recent call last): File "/Users/[REDACTED INFO]/[REDACTED INFO]/bot.py", line 7, in <module> bot = commands.bot(command_prefix = '!', intents = discord.Intents.all()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **TypeError: 'module' object is not callable**
解决方案
错误根源是你用了commands.bot,这里的bot是commands模块下的子模块,不是可实例化的类。正确的类名是首字母大写的Bot,修改这一行代码即可:
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
修改后重新运行代码,该TypeError即可修复。
内容的提问来源于stack exchange,提问作者frootyloops
相关产品推荐
相关产品推荐

