我如何做到这一点,如果没有人提到它返回一个值discord.py

时间:2020-05-20 15:11:04

标签: python discord discord.py discord.py-rewrite

好吧,所以我基本上想做到这一点,如果消息中没有人提及,则该机器人返回“ Who's info bruh”。

@bot.command(pass_context=True)
async def info(ctx, user: discord.Member):
    if user == None:
        await ctx.channel.send("**Who's info bruh**")
        return
    embed = discord.Embed(title="{}'s info".format(user.name), description="Here's what i could find", color=colorran)
    embed.add_field(name="Name", value=user.name, inline=True)
    embed.add_field(name="ID", value=user.id, inline=True)
    embed.add_field(name="Status", value=user.status, inline=True)
    embed.add_field(name="Highest Role", value=user.top_role)
    embed.add_field(name="Joined", value=user.joined_at.__format__('%A, %d. %B %Y'))
    embed.set_thumbnail(url=user.avatar_url)
    embed.set_footer(text=f"Local Meme Bot Development - Requested By: {ctx.author}", icon_url=ctx.author.avatar_url)
    await ctx.send(embed=embed)

此代码返回

discord.ext.commands.errors.MissingRequiredArgument: user is a required argument that is missing.

1 个答案:

答案 0 :(得分:1)

如果未传递任何参数,则可以设置默认参数,例如

@bot.command() # context is automatically passed in rewrite
async def info(ctx, user: discord.Member = None):
    if not user: # more pythonic way of checking if a value is None
        await ctx.send("**Whose info?**") # or you can set user = ctx.author, up to you
    else:
        embed = discord.Embed(title="{}'s info".format(user.name), description="Here's what i could find", color=colorran)
        embed.add_field(name="Name", value=user.name, inline=True)
        embed.add_field(name="ID", value=user.id, inline=True)
        embed.add_field(name="Status", value=user.status, inline=True)
        embed.add_field(name="Highest Role", value=user.top_role)
        embed.add_field(name="Joined", value=user.joined_at.__format__('%A, %d. %B %Y'))
        embed.set_thumbnail(url=user.avatar_url)
        embed.set_footer(text=f"Local Meme Bot Development - Requested By: {ctx.author}", icon_url=ctx.author.avatar_url)
        await ctx.send(embed=embed)

或者,您可以捕获错误:

@bot.command()
async def info(ctx, user: discord.Member):
    # embed-setting code
@info.error
async def info_error(ctx, error):
    if isinstance(error, discord.ext.commands.MissingRequiredArgument):
        await ctx.send("**Whose info?**")
    else:
        print(error)

参考: