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

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

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

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()

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

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
英文:

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

答案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:

确定