如何在命令中提及可选成员?
How do I make mentioning a member optional within a command?
我创建了一个代码,可以根据命令发送拥抱的 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)
我创建了一个代码,可以根据命令发送拥抱的 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)