For Loop ignored while running an async function (Discord Bot using the Pyhton API)

1705 views python
0

I'm making a discord bot that checks whether a certain key is in my database or not. All of the keys are listed in the first column of my CSV file. The value row[2] cointains a boolean parameter that tells the user whether the key is already activated or not. My problem is that the second for loop is being completely ignored. Instead of running the for loop, it directly tries to run await client.send_message(message.author, status) that obviously throws the following exception

Local variable 'status' referenced before assignment

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

Thanks in advance everyone.

answered question

1 Answer

9

You have this condition in your code:

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

status is not defined anywhere else, so if row[0] is not equal to key, status is undefined.

What should status be in that case?

posted this

Have an answer?

JD

Please login first before posting an answer.