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

Discord.py版本中无法加载extensions/cogs问题求助

问题原因及解决方法

核心问题

你使用discord.Client创建客户端,但这个基础客户端类没有load_extension方法——加载扩展(cog)是discord.ext.commands.Bot类专属的功能,Bot继承自Client,同时扩展了命令、cog相关的API。

具体修复步骤

  1. 替换客户端类型:把discord.Client()换成commands.Bot(),Bot类兼容Client的所有功能,同时支持cog加载。
  2. 修正load_extension参数:需要传入字符串格式的模块路径(比如"cogs.Mod"),而非直接引用模块对象。

修改后的完整代码

import discord
from discord import app_commands
from discord.ext import commands

intents = discord.Intents.default()
# 若使用文本命令,需开启message_content权限
# intents.message_content = True

bot = commands.Bot(
    command_prefix="你的命令前缀",  # Bot必须指定命令前缀(即使只用slash命令也建议设置)
    intents=intents,
    description='REDACTED',
    case_insensitive=True,
    owner_id=REDACTED
)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name} - {bot.user.id}')
    # 可选:同步slash命令到服务器
    # await bot.tree.sync()

# 传入字符串形式的模块路径
bot.load_extension("cogs.Mod")
bot.run("token")

额外注意

  • 确保cogs目录下存在__init__.py(空文件即可),让Python识别该目录为可导入模块。
  • 即使仅使用slash命令(app_commands),仍需依赖commands.Bot来加载cog,Client不支持cog的加载机制。

内容的提问来源于stack exchange,提问作者Leon P. Keates

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 23:15:36