如何在命令中使提及成员成为可选的? [英] How do I make mentioning a member optional within a command?

查看:29
本文介绍了如何在命令中使提及成员成为可选的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个代码,它根据命令发送拥抱的gif,并指定它要发送给谁,但是,我还想使其成为提及成员的可选项。

当前代码为:

@client.command()
async def hug(ctx, member):
    username = ctx.message.author.display_name
    embed = discord.Embed(title = (f'{username} has sent a hug to {member}!'), description = ('warm, fuzzy and comforting <3'), color = 0x83B5E3)
    image = random.choice([(url1), (url2),....(url10)])
    embed.set_image(url=image)
    await ctx.channel.send(embed=embed)

我想更改它,以便在作者使用该命令并且没有提到该成员时,该命令仍然有效,并改为发送其中一个gif。我必须创建IF语句吗?

此外,如果可能,如何更改它以使成员的显示名称的使用方式与作者的显示名称的使用方式相同?

我尝试过这样做,但不起作用:

@client.command()
async def hug(ctx, member):
    username = ctx.message.author.display_name
    name = member.display_name
    embed = discord.Embed(title = (f'{username} has sent a hug to {name}!'), description = ('warm, fuzzy and comforting <3'), color = 0x83B5E3)
    image = random.choice([(url1), (url2),...(url10)])
    embed.set_image(url=image)
    await ctx.channel.send(embed=embed)

提前感谢您的帮助

推荐答案

默认情况下,您可以将member参数定义为None。如果在未提及任何人的情况下调用命令,member将以None作为值,if member语句将不会被触发。

另外,通过在函数的参数中将member定义为Member对象,您将能够访问提到的成员的信息。

以下是您的使用方法:

@client.command()
async def hug(ctx, member: discord.Member = None):
    if member:
        embed = discord.Embed(title=f'{ctx.author} has sent a hug to {member}!',
                              description='warm, fuzzy and comforting <3',
                              color=0x83B5E3)
    else:
        embed = discord.Embed(color=0x83B5E3)
        image = random.choice([(url1), (url2),....(url10)])
        embed.set_image(url=image)

    await ctx.channel.send(embed=embed)

这篇关于如何在命令中使提及成员成为可选的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆