LonamiWebs/Telethon

UsernameNotOccupiedError when a group is deleted

Closed this issue · 1 comments

Code that causes the issue

from telethon import TelegramClient, events

@client.on(events.NewMessage(chats=['channel_1', 'channel_2']))
async def handler(event):
    try:
        sender = await event.get_sender()
        message = event.message
        print(message)
    except Exception as e:
        print(f"Error: {e}")

Expected behavior

I was expecting the code print the error

Actual behavior

If a group is deleted during script execution (while I listen for new messages) and another one of the groups I listen for posts a message, the exception is never triggered and my script crashes due to the following error:

telethon.errors.rpcerrorlist.UsernameNotOccupiedError: The username is not in use by anyone else yet (caused by ResolveUsernameRequest)

The above exception was the direct cause of the following exception:

    raise ValueError('No user has "{}" as username'
ValueError: No user has "my_channel_username" as username

Traceback

No response

Telethon version

1.36.0

Python version

3.10

Operating system (including distribution name and version)

Ubuntu 22.04

Other details

If I use JoinChannelRequest(<channel_name>) (to handle channels subscription) no error is raised when a channel is deleted.

Checklist

  • The error is in the library's code, and not in my own.
  • I have searched for this issue before posting it and there isn't an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

Events chats filters must be resolved after the client has connected. If that fails, due to the way handlers are defined, it can't easily be caught.

You can instead replace the usernames and use chat identifiers to filter on.