You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes the bot encounters an error, and I have written an error handler for it, but it doesn't seem to be helping. The bot doesn't reconnect to the stage channel. The error handler works, but it only reconnects when the connection is completely lost.
However, when the bot encounters an error, nothing happens, and it remains in the channel without doing anything.
ERROR - discord.player : Exception in voice thread Thread-3
...
OSError: [WinError 10065] A socket operation was attempted to an unreachable host
My code
importasyncioimportloggingfromdiscord.extimportcommandsfromdiscordimportFFmpegPCMAudioimportdiscordimportsettingsfromsettingsimportFFMPEG_LOCATION, RADIO_STREAMlogger=logging.getLogger(__name__)
classMusic(commands.Cog):
def__init__(self, bot: commands.Bot):
logger.info("Initializing 'Music' extension")
self.bot=botself.current_track=Noneself.player=Noneself.voice_channel=Noneself.bot.loop.create_task(self.connect_and_play())
asyncdefconnect_and_play(self):
awaitself.bot.wait_until_ready()
self.voice_channel=self.bot.get_channel(~)
ifnotisinstance(self.voice_channel, discord.StageChannel):
logger.warning("Voice channel not found. Please check the correctness of the voice channel ID.")
returnifnotself.player:
try:
self.player=self.voice_channel.guild.voice_clientifself.playerisNoneornotself.player.is_connected():
self.player=awaitself.voice_channel.connect(self_deaf=False, reconnect=True)
self.player.play(FFmpegPCMAudio(executable=settings.FFMPEG_LOCATION, source=settings.RADIO_STREAM))
member=self.voice_channel.guild.meawaitmember.edit(suppress=False)
logger.info("Bot connected to voice channel and started playing music")
exceptExceptionase:
logger.error("Error connecting to voice channel:")
logger.error(str(e))
awaitself.reconnect()
else:
logger.warning("Bot is already connected to a voice channel.")
self.player.stop()
awaitself.player.disconnect()
returnasyncdefdisconnect(self):
ifself.playerandself.player.is_playing():
self.player.stop()
awaitself.player.disconnect()
@commands.Cog.listener()asyncdefon_warning(self, warning):
ifany(word.startswith("Could not connect to voice... Retrying...") forwordinstr(warning)):
logger.warning("Error connecting to voice channel. Retrying...")
awaitself.reconnect()
asyncdefreconnect(self):
logger.warning("Connection lost. Reconnecting to the voice channel...")
awaitasyncio.sleep(5)
self.player.stop()
awaitself.disconnect()
awaitasyncio.sleep(2)
whileTrue:
try:
self.player=awaitself.voice_channel.connect(self_deaf=False, reconnect=True)
self.player.play(FFmpegPCMAudio(executable=settings.FFMPEG_LOCATION, source=settings.RADIO_STREAM))
member=self.voice_channel.guild.meawaitmember.edit(suppress=False)
logger.info("Bot reconnected to voice channel and started playing music")
breakexceptExceptionase:
logger.error("Error connecting to voice channel:")
logger.error(str(e))
logger.warning("Retrying in 5 seconds...")
awaitasyncio.sleep(5)
asyncdefpoll_voice_ws(self):
whileTrue:
try:
awaitself.player.ws.recv()
exceptdiscord.errors.ConnectionClosed:
logger.error("WebSocket connection closed. Reconnecting...")
awaitself.reconnect()
breakdefcog_unload(self):
self.bot.loop.create_task(self.disconnect())
logger.warning("Unloading 'Music' extension")
asyncdefsetup(bot: commands.Bot):
awaitbot.add_cog(Music(bot))
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Sometimes the bot encounters an error, and I have written an error handler for it, but it doesn't seem to be helping. The bot doesn't reconnect to the stage channel. The error handler works, but it only reconnects when the connection is completely lost.
However, when the bot encounters an error, nothing happens, and it remains in the channel without doing anything.
My code
Beta Was this translation helpful? Give feedback.
All reactions