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

Pycord使用Cogs组织命令报错:Extension 'cogs.Fun'无setup函数

Discord机器人Cogs加载报错:Extension 'cogs.Fun' has no 'setup' function 解决方法

问题描述

将单文件Discord机器人重构为Cogs结构后,反复触发错误:Extension 'cogs.Fun' has no 'setup' function,已尝试调整setup函数缩进、添加await,问题仍未解决。

相关代码

主文件代码

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

intents = Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='$', intents=intents)

@bot.event
async def on_ready():
    print('We Have logged in as {0.user}'.format(bot))

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

async def main():
    await load()
    await bot.start(TOKEN)

asyncio.run(main())

原Fun.py Cog代码

import discord
from discord.ext import commands
from discord import Intents

intents = Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='$', intents=intents)

class Fun(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    @commands.command()
    async def ping(self, ctx):
        await ctx.send('Pong')

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

完整报错信息

Traceback (most recent call last):
  File "e:\HypePartyBOT\test5.py", line 29, in <module>
    asyncio.run(main())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
    return future.result()
  File "e:\HypePartyBOT\test5.py", line 26, in main
    await load()
  File "e:\HypePartyBOT\test5.py", line 23, in load
    bot.load_extension(f"cogs.{filename[:-3]}")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\cog.py", line 893, in load_extension
    self._load_from_module_spec(spec, name)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\cog.py", line 766, in _load_from_module_spec
    raise errors.NoEntryPointError(key)
discord.errors.NoEntryPointError: Extension 'cogs.Fun' has no 'setup' function.

解决方法

错误根源在于Cog文件中不必要地重复创建了Bot实例,这会干扰Discord.py对setup函数的识别。修改Fun.py代码,移除多余的Bot实例定义:

修改后的Fun.py代码

import discord
from discord.ext import commands

class Fun(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def ping(self, ctx):
        await ctx.send('Pong')

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

说明

  • Cog的作用是将命令模块化,不需要自行创建Bot实例,setup函数会自动接收主文件中创建的Bot实例作为参数,完成Cog的绑定。
  • 确保主文件所在目录下存在cogs文件夹,且Fun.py位于该文件夹内,路径匹配主文件中的加载逻辑。
  • 主文件代码无需修改,仅需替换TOKEN为你的机器人令牌。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 08:00:56