Telethon 'UnboundLocalError' when accessing a variable outside of the event handler

17 Views Asked by At

Telethon throws an error when I try to change the value of variable "taken", declared outside of the event handler.

from telethon import TelegramClient, events
import asyncio

async def main():
    main_bot = await TelegramClient('main_bot', API_ID, API_HASH).start()
    taken: bool = False

    @main_bot.on(events.NewMessage)
    async def event_handler(event):
        print(taken)
        taken = not taken

    await main_bot.run_until_disconnected()

if __name__ == '__main__':
    asyncio.run(main())

Only printing it doesn't show problems, if I comment out the line taker = not taker the error doesn't occur, if it's present even the line before fails

0

There are 0 best solutions below