I usually don't program in Python anymore (but instead languages like C and C++) but I was interested it trying to write a discord bot that can also store data on the server (or more likely my computer or a raspberry pi 4) so sorry if this is some noob mistake but anyways:
Command prompt wasn't telling me there was an error but whenever this code was ran to check a file for the users discord id and then a ')' to signal that's the end of the id then if there is it checks for the string 'location:' in that line (and if there's any errors when checking for the id or 'location:' then it will send an error message in discord) all inside a text file with no extension, then it takes that line with the id and of course 'location:' (because they are both on the same line) and stores it into a variable
That variable is then split up into two more variables using the [string].split()[] before and after the 'location:' (before = [0] and after = [1]) string then it deletes the line that was then before and recreates it by putting it all back together but with a new 'location: ' and the discord role (which is the thing being skipped) in between the two new variables inside the user_data text file (in parts for memory reasons)
But here's the problem the l_role_to_give variable can't get passed (or into) the for loop (which I checked by using 'print(l_role_to_give)') anyways sorry about writing an entire essay on my code but here's the actual code (that I'll show):
async def give_access_role_user_data(context, l_role_to_give):
with open(user_data_dir, 'r+') as user_d:
user_id_index = 0
for line in user_d:
user_id_index += 1
if((str(context.author.id) + ')') in line):
if(('location:') in line):
user_id_line = line
user_id_line_1 = user_id_line.split('location:')[0]
user_id_line_2 = user_id_line_1.split('location:')[1]
del line
user_d.write('\n')
user_d.write(user_id_line_1)
user_d.write('location: ' + l_role_to_give)
user_d.write(' ' + user_id_line_2)
#Writes the everything back with the location (slowly in order to save memory)
else:
await context.send(f'`ERROR: Location variable not found`')
break
else:
await context.send(f'`ERROR: User ID not found`')
break
Alright thanks in advance and if you have any questions feel free to ask
Edit: I changed the code:
async def give_access_role_user_data(context, l_role_to_give):
with open(user_data_dir, 'r+') as user_d:
user_d.seek(0)
user_id_index = 0
for line in user_d:
user_id_index += 1
if((str(context.author.id) + ')') in line):
if(('location:') in line):
l_role_to_give = l_role_to_give
user_id_line_1 = line.split('location:')[0]
user_id_line_2 = line.split('location:')[1]
user_d.write(user_id_line_1)
user_d.write('location: ' + l_role_to_give)
user_d.write(' ' + user_id_line_2)
user_d.write(' ')
user_d.write(' ')
user_d.write(' ')
user_d.write(' ')
user_d.write(' ')
user_d.write(' ')
user_d.write(' ')
user_d.write(' ')
user_d.write('\n')
else:
await context.send(f'`ERROR: Location variable not found`')
break
else:
await context.send(f'`ERROR: User ID not found`')
break
and then figured out the problem (but not how to fix it) in for line in user_d
line is not defined
So it wasn't what I thought it was so I need help again
so again thanks in advance