import chattedrooms
import asyncio
import traceback

async def aexec(code,globals):
    # Make an async function with the code and `exec` it
    exec(
        f'async def __ex(): ' +
        ''.join(f'\n {l}' for l in code.split('\n')),
        globals=globals
    )

    # Get `__ex` from local variables, call it and return the result
    return await globals['__ex']()

async def main():
    client = chattedrooms.Client()
    print("Logging in... It will ask for your email")
    await client.login(input("EMAIL: "))
    me = await client.account.fetch_me()
    print(f"Logged in as {me['username']}")
    print("Simple REPL")
    eee = {"client":client}
    while True:
        command = input("> ")
        if command == "exit":
            break
        else:
            try:
                await aexec(command,eee)
            except BaseException:
                print(traceback.format_exc())
asyncio.run(main())