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

Discord机器人加载Cog遇RuntimeWarning及TypeError问题求助

Discord机器人Cog加载问题排查与解决

开发Discord机器人时,在加载Cog环节陷入问题循环。参考教程编写的Cog加载函数如下:

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

运行后触发如下报错:

C:\Users\galan\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py:618: RuntimeWarning: coroutine 'setup' was never awaited setup(self) 
RuntimeWarning: Enable tracemalloc to get the object allocation traceback 

Traceback (most recent call last): File "c:/Users/galan/Desktop/new sambot/main.py", line 118, in <module> asyncio.get_event_loop().run_until_complete(main())
 
File "C:\Users\galan\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_completereturn future.result() 

File "c:/Users/galan/Desktop/new sambot/main.py", line 115, in main await load_auto() File "c:/Users/galan/Desktop/new sambot/main.py", line 16, in load_auto await bot.load_extension(f'cogs.{filename[:-3]}') 

TypeError: object NoneType can't be used in 'await' expression

尝试移除await后,仍出现RuntimeWarning且Cog无法加载。以下是main.py的部分代码:

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

intents = discord.Intents.all()
intents.members= True
sambot_var = ('sambot', 'sambot!', 'sambot?')
bot = commands.Bot(command_prefix='$', intents=intents)

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

async def main(): 
    await load_auto() 
    await bot.start('token')

asyncio.get_event_loop().run_until_complete(main())

asyncio.run(main())

以及其中一个Cog的代码片段:

from discord.ext 
import commands 
import sys 
import discord 
import random 
sys.path.append("..") 
import datetime 
import pytz 
import re 
import asyncio

class Personality(commands.Cog): 
    def init(self, client): self.client = client

...

async def setup(bot): await bot.add_cog(Personality(bot))

提问

  1. await bot.load_extension(cogs)是否真的不需要await?
  2. 我哪里出错了?
  3. 解决方案是什么?

最终解决

编辑:最终发现问题是discord包版本过旧,代码本身无问题,更新包后即可解决。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 16:35:23