运行异步函数时忽略循环(使用Python API的Discord Bot)

问题描述 投票:1回答:1

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

在赋值之前引用的局部变量'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```")

在此先感谢大家。

python python-3.x discord discord.py
1个回答
1
投票

你的代码中有这个条件:

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

status没有在其他任何地方定义,所以如果row[0]不等于keystatus是未定义的。

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

© www.soinside.com 2019 - 2024. All rights reserved.