RunTimeError with asyncio

14 Views Asked by At

I'm trying to run a separate loop while having my discord bot do other things like Slash Commands. I am using asyncio, await, and threading to do so.

bot = Client(intents=Intents.DEFAULT)

async def timeout_police(timeout2, interval, client):
    print("Delivered client", client)
    print("Called func")
    while True:
         await asyncio.sleep(interval) # should be 60
         #define elapsed_time
            if elapsed_time > timeout2: # should be 7200 for 2 hours
                print("A run has timed out!")
                await client.get_user(MYID).send("Run has timed out") #Problem Here

            else:
                print(f"{elapsed_time} is not at the timeout value {timeout2}")

async def timeout_check(client):
    print("Called timeout Check")
    while True:
        await timeout_police(15, 5, client)

def thread_function(client): #use threading so this can run with the bot
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(timeout_check(client))

@listen() #discord bot starts here
async def on_ready():
    timeout_thread = threading.Thread(target=thread_function, args=(bot,))
    timeout_thread.start()
    print("Ready")

The error is caused by await client.get_user(MYID).send("Run has timed out") I'm assuming there is a problem with using await, though I'm not sure what the problem exactly is. I get RuntimeError: Timeout context manager should be used inside a task

0

There are 0 best solutions below