如何在不引发异常的情况下关闭 Discord 客户端连接

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

How can I close a discord client connection without exception

问题

让我们假设我有一个简单的discord.Client代码:

  1. import os
  2. import discord
  3. from dotenv import load_dotenv
  4. load_dotenv()
  5. intents = discord.Intents.default()
  6. TOKEN = os.getenv('DISCORD_TOKEN')
  7. GUILD = os.getenv('DISCORD_GUILD')
  8. client = discord.Client(intents=intents)
  9. @client.event
  10. async def on_ready():
  11. guild = None
  12. for guild in client.guilds:
  13. if guild.name == GUILD:
  14. break
  15. print(
  16. f'{client.user} is connected to guild {guild.name} (id: {guild.id})'
  17. )
  18. client.run(TOKEN)

所以,当我停止代码时,我会得到这个异常:

  1. Traceback (most recent call last):
  2. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
  3. self close()
  4. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
  5. self._loop.call_soon(self._call_connection_lost, None)
  6. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon
  7. self._check_closed()
  8. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed
  9. raise RuntimeError('Event loop is closed')
  10. RuntimeError: Event loop is closed
  11. Process finished with exit code 0

这不会以任何方式影响代码和功能,但看到它非常令人讨厌。
那么,我应该如何正确断开/关闭连接?

英文:

Let's say I have a simple discord.Client code:

  1. import os
  2. import discord
  3. from dotenv import load_dotenv
  4. load_dotenv()
  5. intents = discord.Intents.default()
  6. TOKEN = os.getenv('DISCORD_TOKEN')
  7. GUILD = os.getenv('DISCORD_GUILD')
  8. client = discord.Client(intents=intents)
  9. @client.event
  10. async def on_ready():
  11. guild = None
  12. for guild in client.guilds:
  13. if guild.name == GUILD:
  14. break
  15. print(
  16. f'{client.user} is connected to guild {guild.name} (id: {guild.id})'
  17. )
  18. client.run(TOKEN)

So, when I stop the code, I get this exception:

  1. Traceback (most recent call last):
  2. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
  3. self.close()
  4. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
  5. self._loop.call_soon(self._call_connection_lost, None)
  6. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon
  7. self._check_closed()
  8. File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed
  9. raise RuntimeError('Event loop is closed')
  10. RuntimeError: Event loop is closed
  11. Process finished with exit code 0

It does not affect the code and functionality in any way, but it is very annoying to see.
How would I disconnect/close connection properly then?

答案1

得分: 0

@client.event
async def close():
print(f"{client.user} disconnected")

英文:

So, after a bit of searching, I found that the

  1. @client.event
  2. async def close():
  3. print(f"{client.user} disconnected")

works just fine.

huangapple
  • 本文由 发表于 2023年2月8日 13:45:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381779.html
匿名

发表评论

匿名网友

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

确定