我如何添加可选参数,例如“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')
真的很简单。
选项 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
文档