Why are there disconnects seemingly at random? #9722
-
I have recently looked into my bots logs and noticed that theres 2 scenarios where it reconnects. The first one seems to be intended as it sends a reconnect opcode.
But sometimes theres also this:
after which theres usually an error like:
It does automatically reconnect afterwards but Im just wondering what the reason is for discord to close the connection without asking for a reconnect. Is it a normal thing to happen? In the end its handled well and you dont notice it, though it still throws an error which shouldnt normally happen on a regular basis. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is just part of the connection flow. Discord disconnects clients every 15 minutes to 2 hours for load balancing reasons. Some of those rarely get invalidated and you have to do a full reconnect but most of the time it's just resumed properly. |
Beta Was this translation helpful? Give feedback.
-
It seems that it is possible to bypass this by sending dummy audio data at least in my testing. Without it, the bot never lasts more than 4 hours, whilst now after some tweaks it has been up for 7 hours so far. I used this function. async def voice_keepalive(self, voice_client):
"""Keeps the voice connection alive by periodically sending silence packets"""
print("Starting voice keepalive task")
while True:
if voice_client.is_connected():
# Send a silence packet every 15 seconds
voice_client.send_audio_packet(b'\xF8\xFF\xFE', encode=False)
await asyncio.sleep(15)
else:
break Then I used Asyncio (python library) to make this a task that runs asynchronously. Here's an example of executing the task after the call begins. Note that I'm using discord self (a fork of discord.py) since my selfbot is supposed to be used in my group chats, the implementation for a bot would be very similar. self.keepalive_task = None # ideally, you would have this initialized once when the script first starts.
voice_client = await group_channel.connect()
await message.channel.send(f'** User "{self.user.name}" has successfully joined the call.**')
# Start the keepalive task
if self.keepalive_task:
self.keepalive_task.cancel()
self.keepalive_task = asyncio.create_task(self.voice_keepalive(voice_client)) Very late but I hope this helps. |
Beta Was this translation helpful? Give feedback.
This is just part of the connection flow. Discord disconnects clients every 15 minutes to 2 hours for load balancing reasons. Some of those rarely get invalidated and you have to do a full reconnect but most of the time it's just resumed properly.