【问题标题】:Why slash command doesnt appear with Cog?为什么斜线命令不会出现在 Cog 中?
【发布时间】:2022-09-24 04:01:31
【问题描述】:

我的齿轮中有这段代码:

class Admin(commands.Cog):
def __init__(self, bot: discord.Bot):
    self.bot = bot

@slash_command(name=\"create-embed\", description=\"Test\")
async def create_embed(self, ctx: discord.ApplicationContext):
    block.has_role(ctx, env.METACORE_MODERATOR_ROLE_ID)

    embed_title = \'Test title\'
    view = v.CreateEmbedView()
    view.bot = self.bot
    view.author_id = ctx.author.id
    result_embed = discord.Embed(title=embed_title)
    embed_message = await ctx.respond(embed=result_embed, view=view, ephemeral=True)
    view.embed = result_embed
    view.embed_message = embed_message

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

但是斜杠命令 create-embed 没有出现。在我的 main.py 中,我有:

@bot.event
async def on_ready():
    admin.setup(bot)
  • 控制台中没有错误(日志)

标签: python discord pycord


【解决方案1】:

我从admin.setup 猜测您正在导入 cog 文件。

首先是在 pycord 中调用 on_ready 之前注册了应用程序命令(斜杠命令)。所以如果你想像这样添加齿轮,你需要这样做:

@bot.event
async def on_connect():
    admin.setup()
    await bot.sync_commands()

然而, cogs 不应该像这样添加。相反,您不应该导入 cog 文件并使用load_extension 来加载您的 cog。例子:

@bot.event
async def on_connect():
    bot.load_extension("admin")
    await bot.sync_commands()

这种方式更简洁,允许您在运行时重新加载您的 cog。通过support server 也是一个更好的提出此类问题的地方。

【讨论】:

    猜你喜欢
    • 2022-08-10
    • 2022-01-03
    • 2022-06-13
    • 2022-11-21
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    相关资源
    最近更新 更多