I want the bot to run in one thread, and I want an async function to run in another. This async function will basically check if it is 2 pm, and send messages to all the servers it is in. If it is not 2 pm, the thread will sleep.
I tried many ways, but I was unable to run the bot correctly in a thread.
Tried this simple approach using threading
import threading
sync_thread = threading.Thread(target=self.run, args=(DISCORD_BOT_TOKEN,))
async_thread = threading.Thread(target=asyncio.run, args=(self.start_alerts() ,))
sync_thread.start()
async_thread.start()
# Wait for both threads to complete
sync_thread.join()
async_thread.join()
But it gives the following error on this line. I'm getting this error not just on fetch_user
but on any bot operation like sending message to a channel.
admin = await self.fetch_user(DISCORD_ADMIN_ID)
File "/venv/lib/python3.10/site-packages/discord/client.py", line 2525, in fetch_user
data = await self.http.get_user(user_id)
File "/venv/lib/python3.10/site-packages/discord/http.py", line 624, in request
async with self.__session.request(method, url, **kwargs) as response:
File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
self._resp = await self._coro
File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 467, in _request
with timer:
File "/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task
Found an answer that solves this issue. Adding it here instead of deleting the question.