python - 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))
解决方案
这是一个缩进问题。
请确保设置功能与__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))
推荐阅读
- r - 在 R 中的多个字符串中选择可变长度的字符串
- vba - 我需要一种更快的方法来递归查找文件元数据标签,我的 VBA 脚本太慢了
- arrays - Bash:如何声明空数组,然后向其中添加变量
- python - 当我将字符串连接到 pandas 列时,为什么 pandas 会神秘地将列转换为小数?
- react-native - SetInterval 无限运行
- mysql - 如何在 MySQL 中的 groupby 之后为每个唯一列值保留一行?
- javascript - 较新节点目标的 babel7 配置在“import”语句中失败
- reactjs - 尝试将 PostForm 与 React 函数集成
- lua - 每次触发事件时,如何使所有玩家都能看到 screenGui 元素?
- javascript - 表单验证后 isValid 保持为假