如何在discord.py中向斜杠命令添加可选参数

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

我如何添加可选参数,例如“thing_to_say”是可选的

@client.tree.command(name="say", description = "whats should the bot say")
@app_commands.describe(thing_to_say = "text")
async def say(interaction: discord.Interaction, thing_to_say: str):
    if not interaction.user.guild_permissions.administrator:
      return await interaction.response.send_message("You requre administator permisions to use this command")
    if len(thing_to_say) != 0:
        await interaction.response.send_message(f'{thing_to_say}')
    else:
        await interaction.response.send_message(f'nothing to be said')
python discord discord.py
1个回答
0
投票

真的很简单。

选项 1:您使用

typing.Optional
作为
thing_to_say
的类型提示:

async def ...(..., thing_to_say: typing.Optional[str], ...):

选项 2:使用

|
None
作为输入提示:

async def ...(..., thing_to_say: str | None, ...):

typing.Optional
文档

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