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

Discord Bot运行报错:TypeError: 'module' object is not callable求助

Discord Bot 运行报错排查

错误原因

你遇到的TypeError: 'module' object is not callable错误,核心问题是代码中创建Bot实例的语句写错了:commands.bot是模块对象,而非可调用的类。正确的写法是commands.Bot(类名首字母大写)。

修正后的代码

import discord
from discord.ext import commands
import dotenv
import logging
from dotenv import load_dotenv
import os

load_dotenv()
token = os.getenv('DISCORD_TOKEN')
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True

# 修正此处:把commands.bot改为commands.Bot
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f"this is a test message from {bot.user.name}")

bot.run(token, log_handler=handler, log_level=logging.DEBUG)

额外注意事项

  • 确保虚拟环境中安装的discord.py版本与教程一致,可通过pip show discord.py查看版本,若版本不匹配可重新安装对应版本。
  • 确认Discord开发者平台中,你的Bot应用已开启Message Content Intent和Server Members Intent权限,否则即使代码正确也可能无法正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 02:41:02