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



好的,所以我基本上想让它在消息中没有提到时,机器人会返回"谁的信息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.

如果没有传递任何参数,您可以设置默认参数,如下所示

@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)

引用:

  • 默认参数
  • discord.Embed()
  • Embed.add_field()
  • Embed.set_thumbnail()
  • Embed.set_footer()
  • commands.MissingRequiredArgument- 此处也发现其他例外
  • Command.error- 错误修饰器

相关内容

最新更新