首页 > 解决方案 > discord.ext.commands.errors.CommandInvokeError:命令引发异常:NoEntryPointError:扩展“cogs.help”没有“设置”功能

问题描述

我正在尝试为我的 Discord Bot 制作一个齿轮。

但是,尝试加载 cog 会提示我错误

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NoEntryPointError: Extension 'cogs.help' has no 'setup' function.

我的装载机的代码是;

@bot.command()
async def load(ctx, extension):
    id = str(ctx.author.id)
    if id == '721029142602056328':
        bot.load_extension(f'cogs.{extension}')
        print(f'Specified cog {extension} loaded!')
        author = ctx.message.author

        embed = discord.Embed(
            colour = discord.Colour.from_rgb(255, 237, 76)
        )

        embed.add_field(name='Cog Loaded', value=f"Specified cog {extension} loaded by {author}.", inline=False)

        await ctx.send(embed=embed)
    else:
        embed = discord.Embed(
            colour = discord.Colour.from_rgb(255, 237, 76)
        )

        embed.add_field(name="You can't do this!", value=f"You can't load the {extension} cog.", inline=False)

        await ctx.send(embed=embed)

@bot.command()
async def unload(ctx, extension):
    id = str(ctx.author.id)
    if id == '721029142602056328':
        bot.unload_extension(f'cogs.{extension}')
        print(f'Specified cog {extension} unloaded!')
        author = ctx.message.author

        embed = discord.Embed(
            colour = discord.Colour.from_rgb(255, 237, 76)
        )

        embed.add_field(name='Cog Unloaded', value=f"Specified cog {extension} unloaded by {author}.", inline=False)

        await ctx.send(embed=embed)
    else:
        embed = discord.Embed(
            colour = discord.Colour.from_rgb(255, 237, 76)
        )

        embed.add_field(name="You can't do this!", value=f"You can't unload the {extension} cog.", inline=False)

        await ctx.send(embed=embed)

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

如果有人能解决我的错误,将不胜感激。值得注意的是,我可以启动机器人,但是我必须删除“用于 os.listdir 中的文件名......”

谢谢

编辑

我刚刚意识到我在发布这篇文章时忘记将代码包含在我的 cog 中。

import discord
from discord.ext import commands


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

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

标签: pythonpython-3.xdiscorddiscord.pydiscord.py-rewrite

解决方案


这是一个缩进问题。

请确保设置功能与__init__

import discord
from discord.ext import commands


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

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

推荐阅读