0

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

Sammyueru
  • 23
  • 7
  • What do you mean by "the l_role_to_give variable can't get passed (or into) the for loop"? There's nothing special about it with respect to the for loop. It's just a local variable like any other. Is it possible it's not a string? Is there a specific error you're getting? – Iguananaut Jun 27 '21 at 02:08
  • @Iguananaut nope no errors I'm getting it just doesn't see the variable after that point with anything because I tested it with a print(l_role_to_give) on each part and after the for loop it wont print it Edit: it is a string – Sammyueru Jun 27 '21 at 02:10
  • It also looks like you're making a mess for yourself by trying to write to a file while also reading from it. I'm not sure why you want to do that. You said something about "for memory reasons" but how large is the file? Trying to update a file's contents in-place is a complicated problem. – Iguananaut Jun 27 '21 at 02:13
  • You could instead use a JSON file or something like that, read it in, modify some values, and write it out again. Unless it's hundreds or thousands of megabytes in size this is not a problem, and otherwise you should be using a database or something like that. – Iguananaut Jun 27 '21 at 02:15
  • @Iguananaut it's not exactly that's it's long It's just that it can become longer the more data your store in the other spots but then also it can have many users although that part isn't really a problem since it's all on one line – Sammyueru Jun 27 '21 at 02:16
  • Alternatively you could use Python's [shelve](https://docs.python.org/3/library/shelve.html) module. – Iguananaut Jun 27 '21 at 02:16
  • @Iguananaut I think it would be more fun to experiment without using a JSON file and use normal text files – Sammyueru Jun 27 '21 at 02:17
  • You are probably making your life harder for yourself but OK. I don't think a Discord server can even have enough active users where this can become a problem. – Iguananaut Jun 27 '21 at 02:19
  • Note: `del line` does not do what it looks like you think it does. – Iguananaut Jun 27 '21 at 02:20
  • @Iguananaut really then how do I delete a singular line then? without taking the entire file in though – Sammyueru Jun 27 '21 at 02:22
  • Same as in C, you'd have to seek the file to the beginning of the line and start writing from there, taking care to not overwrite the next line break. If the new line is longer than the old line you'd have to resize the file and move all subsequent lines forward. Like I said, modifying a file in-place can be complicated, which is why there are special libraries for it. – Iguananaut Jun 27 '21 at 02:25

0 Answers0