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

Discord.py 2.0加载Cogs时提示'ping'命令未找到的问题求助

解决Discord.py 2.0加载Cogs后命令找不到的问题

你的核心问题是Cogs根本没被加载,因为main.py里的代码逻辑错误,导致加载Cogs的流程完全没有执行。

问题分析

你定义了load()和main()函数来加载Cogs,但直接调用bot.run()会创建独立的事件循环,完全跳过了你写的加载逻辑,自然找不到ping命令。

修正后的main.py代码

import discord
import os
import asyncio
from discord.ext import commands

intents = discord.Intents.all()
intents.members = True
token = "你的Bot Token"
bot = commands.Bot(command_prefix='!', intents=intents, help_command=None)


async def load():
    for filename in os.listdir('./cogs'):
        if filename.endswith('.py'):
            await bot.load_extension(f'cogs.{filename[:-3]}')

async def main():
    async with bot:
        await load()
        await bot.start(token)

asyncio.run(main())

关键修改点

  • 移除直接调用的bot.run(),改用asyncio.run(main())触发你的异步加载流程
  • 使用async with bot上下文管理器,确保Bot资源正确初始化和清理(Discord.py 2.0推荐写法)

Cogs文件验证

你的Cogs代码本身没有问题,但要确保:

  • Cogs文件放在项目根目录的cogs文件夹下,文件名比如ping.py(和加载路径cogs.ping对应)
  • 运行Bot时,控制台能输出bot Is Online,说明Cog已成功加载

完成以上修改后,发送!ping就能正常触发命令返回pong了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:03:26