【问题标题】:I can't make the slash command half of the hybrid command sync. (discord.py)我不能使斜线命令成为混合命令同步的一半。 (不和谐.py)
【发布时间】:2022-12-19 18:18:19
【问题描述】:

我设置了一个简单的文本命令来同步我的 discord 机器人中的所有命令。 (discord.py)出于某种原因,只有混合命令的文本部分有效。当我输入 o.sync 时,它说已同步 0 个命令,但 o.ping 命令有效。我是否遗漏了任何明显的错误?

import discord
from discord.ext import commands
from discord import app_commands

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

    @commands.Cog.listener()
    async def on_ready(self):
        print('Survey cog loaded.')

    @commands.command()
    async def sync(self, ctx) -> None:
        fmt = await ctx.bot.tree.sync(guild=ctx.guild)

        await ctx.send(
            f"Synced {len(fmt)} commands to the current guild."
        )

    @commands.command()
    async def unsync(self, ctx) -> None:
        ctx.bot.tree.clear_commands(guild=ctx.guild)
        sync_tree = await ctx.bot.tree.sync(guild=ctx.guild)
        await ctx.send(
            f"Unsynced {len(sync_tree)} commands to the current guild."
        )
        return

    @commands.hybrid_command(name='ping', with_app_command=True)
    async def help(self, ctx: commands.Context):
        await ctx.send("pong", ephemeral=True)

async def setup(bot):
    await bot.add_cog(Survey(bot), guilds=[discord.Object(id=874842871801479208)])

`

o.ping 命令显示混合命令正在运行,但斜杠命令根本没有出现在菜单中。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    看起来您正在为 ping 命令使用 @commands.hybrid_command 装饰器,它允许使用提及或命令前缀(例如 @Bot ping 或 !ping)调用命令。但是,对于 sync 和 unsync 命令,您使用的是 @commands.command 装饰器,它只允许使用命令前缀调用命令。

    要解决此问题,您只需将同步和取消同步命令的 @commands.command 装饰器替换为 @commands.hybrid_command 装饰器即可。这将允许使用提及或命令前缀调用命令。

    这是一个示例,说明如何修改代码以使用 @commands.hybrid_command sync 和 unsync 命令的装饰器:

    @commands.hybrid_command()
    async def sync(self, ctx) -> None:
        fmt = await ctx.bot.tree.sync(guild=ctx.guild)
    
        await ctx.send(
            f"Synced {len(fmt)} commands to the current guild."
        )
    
    @commands.hybrid_command()
    async def unsync(self, ctx) -> None:
        ctx.bot.tree.clear_commands(guild=ctx.guild)
        sync_tree = await ctx.bot.tree.sync(guild=ctx.guild)
        await ctx.send(
            f"Unsynced {len(sync_tree)} commands to the current guild."
        )
        return
    

    我希望这有帮助!

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 1970-01-01
      • 2022-10-20
      • 2021-11-20
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多