1

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.

1
  • The very first line of code in the function is an elif, which is an error. Please fix the function. Commented Sep 16, 2018 at 16:05

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

10 Comments

I don't think that's correct. The "await client.send_message(message.author, status)" is under the if condition as well. So if that gets run, it means that the if condition is True.
As posted, the await statement is not under the row[0] == key condition.
Yeah you are right, the indentation is wrong. But the point is, not even the print(row[0]) gets run.
If the indentation is wrong, fix it so we can see the actual code you're running.
print(row[0]) is right above the if condition, so it should get run regardless of the outcome of if row[0] == key
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.