简体   繁体   English

我如何检查指定用户是否是有效用户(也在服务器中)(discord.py)

[英]How do i check if a specified user is a valid user (who is also in the server) (discord.py)

So when this command is executed it fetches a discord user and i want to make it so that if the specified user isnt real or isnt in the discord server it will delete the message and send a dm to the message author saying that the user isnt real因此,当执行此命令时,它会获取 discord 用户,我想这样做,以便如果指定的用户不是真实的或不在 discord 服务器中,它将删除该消息并向消息作者发送一个 dm,说该用户不是真实的

  @commands.command()
  async def rickroll(self, ctx, user : discord.User = None):

    if user == None:
      none_embed = discord.Embed(title = "Missing argument - You didnt specify the user", color = 0xff0000)
      await ctx.author.send(embed = none_embed)
      await ctx.message.delete()
      return

    rickroll = ("just for the sake of the question i didnt write the lyrics otherwise it will be too long")

    rickroll_embed = discord.Embed(title = f"You have been rickrolled by {ctx.author}", description = rickroll, color = 0x00ceea)
    await ctx.message.delete()
    await user.send(embed = rickroll_embed)

    author_embed = discord.Embed(title = f"Sent the rickroll to {user}!", color = 0x00ceea)
    await ctx.author.send(embed = author_embed)

The error message that comes up if the user is not in the server is a UserNotFound error.如果user不在服务器中,则出现的错误消息是UserNotFound错误。 You have to build your own error handler for that, discord.py helps you with that.您必须为此构建自己的错误处理程序, discord.py可以帮助您。

Have a look at the following code:看看下面的代码:

@rickroll.error
async def rickroll_error(self, ctx, error):
    if isinstance(error, commands.UserNotFound):
        await ctx.author.send("User not found.") # Or whatever you want to send

This code has to be under your actual rickroll command in order to work.此代码必须在您的实际rickroll命令下才能工作。

See the docs for more.有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM