From 4e948545cad69cdfa3302367b685a60b0cc4bb45 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 14 Aug 2021 15:06:12 +0200 Subject: [PATCH] retrying to connect to the lavalink server until it worked --- src/cogs/music.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cogs/music.py b/src/cogs/music.py index 4431a24..6a3ad72 100644 --- a/src/cogs/music.py +++ b/src/cogs/music.py @@ -27,6 +27,8 @@ import re import typing import wavelink from discord.ext import commands, menus +from time import sleep +import socket # Genius API from lyricsgenius import Genius @@ -355,8 +357,17 @@ class Music(commands.Cog, wavelink.WavelinkMixin): 'region': self.keys["REGION_DISCORD"] }} - for n in nodes.values(): - await self.bot.wavelink.initiate_node(**n) + nodeValues = list(nodes.values()) + n = 0 + while n < len(nodeValues): + location = (nodeValues[n]["host"], nodeValues[n]["port"]) + a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + if a_socket.connect_ex(location) == 0: + await self.bot.wavelink.initiate_node(**nodeValues[n]) # Port is open, connecting... + n += 1 + else: + sleep(1) # Port is not open, retrying in 1 second... @wavelink.WavelinkMixin.listener() async def on_node_ready(self, node: wavelink.Node):