运行异步函数时,For Loop被忽略(使用Python API的Discord Bot)

时间:2018-09-16 15:43:26

标签: python python-3.x discord discord.py

我正在制造一个不和谐的机器人,该机器人检查某个密钥是否在我的数据库中。所有键都列在我的CSV文件的第一列中。 row [2]值包含一个布尔参数,该参数告诉用户密钥是否已激活。我的问题是第二个for循环被完全忽略了。它不尝试运行for循环,而是直接尝试运行显然引发以下异常的await client.send_message(message.author, status)

  

分配前已引用本地变量“状态”

@client.event
async def on_message(message):

    elif message.content.startswith("!activate"):
        with open('database.csv', 'rt') as csvfile:
            content = csv.reader(csvfile, delimiter=',')
            key_list = []
            for row in content:
                key_list.append(row[0])
            key = message.content.split(" ")[1]
            try:

                if key in key_list:

                    for row in content:
                        print(row[0])
                        if row[0] == key:
                            print(row[2])
                            status = row[2]
                    await client.send_message(message.author, status)
                else:
                    await client.send_message(message.author, "Your key was not found in our database. Make sure you use the proper format: ```!activate KEY```")  
            except Exception as E:
                print(E)
                await client.send_message(message.author, "Your key was not found in our database. Make sure you use the proper format: ```!activate KEY```")

预先感谢大家。

1 个答案:

答案 0 :(得分:1)

您的代码中存在以下条件:

if row[0] == key:
    print(row[2])
    status = row[2]

status在其他任何地方均未定义,因此如果row[0]不等于key,则status未定义。

在这种情况下,status应该是什么?