skip to Main Content

Hi how can I avoid reusing the same user?

I can’t find a solution to this. I have a list of 3 usernames but after he changes once he is unable to change to another one again.. Does anyone have a solution for this?

username_list = ['zRRrrwx2231221s', 'bzRRrrwx2231as', 'azRRrrwx2231s']


random_username = random.choice(username_list)
print('A randomly selected username is:', random_username)

with TelegramClient(StringSession(ses_string), api_id, api_hash) as client:
    while True:
        try:
            time.sleep(10)
            
            result = client(functions.channels.UpdateUsernameRequest(
               channel= 'zRRrrwx2231221s' ,
               username = random_username
            ))

telethon.errors.rpcerrorlist.UsernameNotModifiedError: The username is not different from the current username (caused by UpdateUsernameRequest)

I just want it to switch between the three users regularly – I don’t care if one of them catches I’ll switch.. but it can’t run thanks to the helpers

2

Answers


  1. This random_username = random.choice(username_list) happens only once when you call it and is a variable. To make it possible to call it again you need a function like:

    def rnd_username():
        random_username = random.choice(username_list)
        print('A randomly selected username is:', random_username)
        return random_username
    

    Now instead of username = random_username you need to call username = rnd_username()

    Login or Signup to reply.
  2. As I mentioned in comments, it’s always trying to pick the same username. But the error you’re getting appears to be if you "change" the username, but it’s the same as before.

    https://tl.telethon.dev/methods/account/update_username.html

    If that’s the case, you have two options.

    1: remember the username and just skip the change, so you don’t get the error. This has the added benefit of not attempting an api call that you know will fail.

    import time
    import random
    
    username_list = ['zRRrrwx2231221s', 'bzRRrrwx2231as', 'azRRrrwx2231s']
    previous_username = None
    while True:
        try:
            random_username = random.choice(username_list)
            if random_username == previous_username:
                print('we decided to stick with the same username (do nothing)')
            else:
                print('changing username to', random_username)
                #result = client(functions.channels.UpdateUsernameRequest(
                   #channel= 'zRRrrwx2231221s' ,
                   #username = random_username
                #))
    
            previous_username = random_username
            time.sleep(10)
        except Exception as e:
            print(type(e), str(e))
    

    2: just do it anyway, catch the exception, and then ignore it. Easier to ask forgiveness than permission programming. So add a handler for UsernameNotModifiedError.

    while True:
        random_username = random.choice(username_list)
        try:
            result = client(functions.channels.UpdateUsernameRequest(
               channel= 'zRRrrwx2231221s' ,
               username = random_username
            ))
        except UsernameNotModifiedError:
            pass
        time.sleep(10)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search