如何使我的 Discord py 机器人与另一个异步函数一起异步运行?

huangapple go评论132阅读模式
英文:

How do I run a discord py bot asynchronously with another async function?

问题

I tried many ways, but I was unable to run the bot correctly in a thread.

Tried this simple approach using threading

  1. import threading
  2. sync_thread = threading.Thread(target=self.run, args=(DISCORD_BOT_TOKEN,))
  3. async_thread = threading.Thread(target=asyncio.run, args=(self.start_alerts() ,))
  4. sync_thread.start()
  5. async_thread.start()
  6. # Wait for both threads to complete
  7. sync_thread.join()
  8. async_thread.join()

但是在以下这行代码上出现了以下错误。我不仅在fetch_user这里出现了这个错误,而且在像发送消息到频道这样的任何机器人操作中都出现了这个错误。

  1. admin = await self.fetch_user(DISCORD_ADMIN_ID)
  1. File "/venv/lib/python3.10/site-packages/discord/client.py", line 2525, in fetch_user
  2. data = await self.http.get_user(user_id)
  3. File "/venv/lib/python3.10/site-packages/discord/http.py", line 624, in request
  4. async with self.__session.request(method, url, **kwargs) as response:
  5. File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
  6. self._resp = await self._coro
  7. File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 467, in _request
  8. with timer:
  9. File "/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
  10. raise RuntimeError(
  11. RuntimeError: Timeout context manager should be used inside a task
英文:

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

  1. import threading
  2. sync_thread = threading.Thread(target=self.run, args=(DISCORD_BOT_TOKEN,))
  3. async_thread = threading.Thread(target=asyncio.run, args=(self.start_alerts() ,))
  4. sync_thread.start()
  5. async_thread.start()
  6. # Wait for both threads to complete
  7. sync_thread.join()
  8. 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.

  1. admin = await self.fetch_user(DISCORD_ADMIN_ID)
  1. File "/venv/lib/python3.10/site-packages/discord/client.py", line 2525, in fetch_user
  2. data = await self.http.get_user(user_id)
  3. File "/venv/lib/python3.10/site-packages/discord/http.py", line 624, in request
  4. async with self.__session.request(method, url, **kwargs) as response:
  5. File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
  6. self._resp = await self._coro
  7. File "/venv/lib/python3.10/site-packages/aiohttp/client.py", line 467, in _request
  8. with timer:
  9. File "/venv/lib/python3.10/site-packages/aiohttp/helpers.py", line 701, in __enter__
  10. raise RuntimeError(
  11. RuntimeError: Timeout context manager should be used inside a task

答案1

得分: 0

找到了解决这个问题的答案。将其添加在这里,而不是删除这个问题。

https://stackoverflow.com/a/73903794/11721258

英文:

Found an answer that solves this issue. Adding it here instead of deleting the question.

https://stackoverflow.com/a/73903794/11721258

huangapple
  • 本文由 发表于 2023年5月21日 14:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定