如何在discord.py中将组或子斜杠命令与Bot一起使用?

问题描述 投票:0回答:1

我不知道如何使用discord.py中的

Bot
配置子斜杠命令或斜杠命令组。我已经尝试过在网上阅读的各种内容,但这就是我现在所拥有的,只是想让任何最小的事情发挥作用:

intents: discord.Intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
fooGroup = discord.app_commands.Group(name='foo', description='foo!')

@fooGroup.command()
async def bar(ctx):
    await ctx.send(f"bar")

bot.tree.add_command(fooGroup)

@bot.event
async def on_ready() -> None:
    await bot.tree.sync(guild=discord.Object(id=ServerId))
    print(f'{bot.user} is now running!')

bot.run(TOKEN)

它运行了,但我从未看到

/foo
出现。我期待或试图实现类似的目标:

/foo bar arg1 arg2 ...
/foo baz arg1 arg2 ...

我的理解是

foo
是根命令或组,
bar
/
baz
是子命令,但我还没弄清楚如何实现它。

我还尝试使用

app_commands.CommandTree(bot)
@tree.command
,我之前曾将其用于单级斜杠命令,但我也无法使其工作......

discord.py
1个回答
0
投票

这里的问题是,当您没有公会命令时,您正在同步到公会。

@fooGroup.command() # This is a global command
await bot.tree.sync(guild=discord.Object(id=ServerId)) # However, you're syncing a to guild.

您需要将

guilds=
kwarg 添加到
fooGroup.command()
或删除
guild=
中的
bot.tree.sync

© www.soinside.com 2019 - 2024. All rights reserved.