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

Discord.py机器人启动失败,报TypeError错误求助

Discord机器人运行报错修复

问题说明

运行Discord机器人代码时触发以下错误:

回溯(最近的调用最后):
文件 "c:\Users\Mathis\Documents\bot python\main.py",第4行,在 中
bot = commands.bot (command_prefix = "!")
类型错误:'module' 对象不可调用

错误原因

代码中错误使用了小写的commands.bot,discord.ext.commands库中用于创建机器人实例的是大写的Bot类,小写的bot是一个子模块,无法直接调用实例化。

修正后的代码

from discord.ext import commands

# 将 commands.bot 改为 commands.Bot
bot = commands.Bot(command_prefix="!")

@bot.event 
async def on_ready():
    print("已就绪!")

# 替换为你的机器人Token
bot.run("你的机器人Token")

额外提示

  • 确保机器人Token正确,且已在Discord开发者平台为机器人开启必要权限
  • 若使用discord.py v2.x版本,实例化Bot时需添加intents参数,否则会触发权限相关报错,示例如下:
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True  # 需处理消息内容时开启
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event 
async def on_ready():
    print("已就绪!")

bot.run("你的机器人Token")

内容的提问来源于stack exchange,提问作者Mathi'S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 12:01:18