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

Discord Bot加载Cog触发RuntimeWarning,命令无法识别求助

问题解决步骤

1. 修复load_extension未使用await的问题

在discord.py v2.0及以上版本中,load_extension是异步协程函数,必须使用await关键字调用,否则会出现"coroutine was never awaited"警告,且Cog模块不会被实际加载。

修改main.py中的load_modules函数:

async def load_modules():
    for f in os.listdir("./cogs"):
        if f.endswith(".py"):
            # 给load_extension添加await
            await bot.load_extension("cogs." + f[:-3])
            print(f"Module {f} chargé.")

2. 修复test.py的缩进语法错误

你的test.py中class Test(commands.Cog):一行存在多余缩进,会导致Python语法错误,无法正常加载该Cog模块。

修正后的test.py代码:

from discord.ext import commands

class Test(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    
    @commands.Cog.listener()
    async def on_ready(self):
        print('Test module ready.')
    
    @commands.Cog.command(name='test')
    async def test(self, ctx):
        await ctx.send('Test command executed.')

def setup(bot):
    bot.add_cog(Test(bot))

3. 可选:确保cogs文件夹是Python包(非必须,但推荐)

在cogs文件夹中创建一个空的__init__.py文件,将其标记为Python包,避免潜在的导入问题。

完成以上修改后,重新启动Bot,警告会消失,!test和!ping命令即可正常识别执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 13:15:59