如何在表单中找到用户名?

How do I find the user's name in a form?

所以我目前正在开发一个 Discord.py 机器人,并试图确定用户是否已经通过机器人使用 GSpread 进行了注册。我尝试了一些类似的东西:

if findUser is None:
   print("User not found!")
if findUser:
   print("Found user!")

它在找到用户时有效,但当我从电子表格中删除用户名时,它就崩溃了。

@bot.command()
async def finduser(message):
    user = (message.author.name+message.author.discriminator)
    print(user)
    findUser = sheet.find(user, in_column=3)

    if findUser:
        print("User found!")
    else:
        print("User not found!")

无论如何,如果有人能帮助我,那就太棒了!

-ekalb2020

如果只改变条件呢?由于它适用于第一个示例,您可以在第二个示例中使用它

@bot.command()
async def finduser(message):
    user = (message.author.name+message.author.discriminator)
    print(user)
    findUser = sheet.find(user, in_column=3)

    if findUser is None:
        print("User not found!")

    else:
        print("User found!")

我想通了!挺简单的,就是搞错了语句的用法!

@bot.command()
async def finduser(message): 
    user = (message.author.name+message.author.discriminator)
    print(user)
    
    try:
        sheet.find(user, in_column=3)
    except:
        print("User not found!")
    else:
        print("User found!")

在此处查找有关 try 语句的更多信息:Determine if variable is defined in Python