python-3.x 如何创建一个可选变量

xwbd5t1u  于 2023-05-08  发布在  Python
关注(0)|答案(1)|浏览(91)
@bee.command(name="돈얼마", description="지갑 지갑을 보자!")
async def 돈얼마(interaction: discord.Interaction, 멘션: discord.User):
    userid = 멘션.id
    embed=discord.Embed(title="돈 지급", description=f"잔액 : {money[userid]}원\n" + f"이정도 밖에 없는건 아니죠? 에이", color=0x5cff67)
    await interaction.response.send_message(embed=embed)

这段代码,我想让这部分成为一个可选参数>“:用户”
我不知道请帮帮我

pvcm50d1

pvcm50d11#

检查此:https://www.w3schools.com/python/gloss_python_function_default_parameter.asp
您也可以使用None作为默认参数,并以其他方式处理它:

@bee.command(name="돈얼마", description="지갑 지갑을 보자!")
async def 돈얼마(interaction: discord.Interaction, 멘션: discord.User = None):
    if 멘션 is not None:
        userid = 멘션.id
        embed=discord.Embed(title="돈 지급", description=f"잔액 : {money[userid]}원\n" + f"이정도 밖에 없는건 아니죠? 에이", color=0x5cff67)
        await interaction.response.send_message(embed=embed)
    else:
        foo() # replace with code to execute when 멘션 is not given

当给出时,它将像往常一样执行,否则将执行foo()

相关问题