fix(pterodactyl): fixed retry functionality - no longer blocks the bot

This commit is contained in:
Seaswimmer 2024-03-01 15:11:57 -05:00
parent a166168507
commit 1bc1c7a90e
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -26,20 +26,20 @@ class Pterodactyl(commands.Cog):
async def cog_load(self) -> None:
self.retry_counter = 0
self.task = await self.get_task()
self.task = self.get_task()
async def cog_unload(self) -> None:
self.task.cancel()
self.retry_counter = 0
await self.client._session.close() # pylint: disable=protected-access
async def get_task(self) -> asyncio.Task:
def get_task(self) -> asyncio.Task:
from pterodactyl.websocket import establish_websocket_connection
task = self.bot.loop.create_task(establish_websocket_connection(self), name="Pterodactyl Websocket Connection")
task.add_done_callback(self.error_callback)
return task
async def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out
def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out
try:
fut.result()
except asyncio.CancelledError:
@ -50,8 +50,7 @@ class Pterodactyl(commands.Cog):
if self.retry_counter < 5:
self.retry_counter += 1
logger.info("Retrying in %s seconds...", 5 * self.retry_counter)
await asyncio.sleep(5 * self.retry_counter)
self.task = self.get_task()
self.task = self.bot.loop.call_later(5 * self.retry_counter, self.get_task)
else:
logger.info("Retry limit reached. Stopping task.")
@ -66,7 +65,7 @@ class Pterodactyl(commands.Cog):
logger.error("WebSocket connection closed: %s", e)
self.task.cancel()
self.retry_counter = 0
self.task = await self.get_task()
self.task = self.get_task()
if message.channel.id == await config.chat_channel() and message.author.bot is False:
logger.debug("Received chat message from %s: %s", message.author.id, message.content)
channel = self.bot.get_channel(await config.console_channel())
@ -80,7 +79,7 @@ class Pterodactyl(commands.Cog):
logger.error("WebSocket connection closed: %s", e)
self.task.cancel()
self.retry_counter = 0
self.task = await self.get_task()
self.task = self.get_task()
async def get_chat_command(self, username: str, message: str, color: discord.Color) -> str:
command: str = await config.chat_command()
@ -93,7 +92,7 @@ class Pterodactyl(commands.Cog):
logger.info("Configuration value set: api_key\nRestarting task...")
self.task.cancel()
self.retry_counter = 0
self.task = await self.get_task()
self.task = self.get_task()
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"])
async def pterodactyl(self, ctx: commands.Context) -> None:
@ -118,7 +117,7 @@ class Pterodactyl(commands.Cog):
logger.info("Configuration value set: base_url = %s\nRestarting task...", base_url)
self.task.cancel()
self.retry_counter = 0
self.task = await self.get_task()
self.task = self.get_task()
@pterodactyl_config.command(name = "serverid")
async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None:
@ -128,7 +127,7 @@ class Pterodactyl(commands.Cog):
logger.info("Configuration value set: server_id = %s\nRestarting task...", server_id)
self.task.cancel()
self.retry_counter = 0
self.task = await self.get_task()
self.task = self.get_task()
@pterodactyl_config.command(name = "consolechannel")
async def pterodactyl_config_console_channel(self, ctx: commands.Context, channel: discord.TextChannel) -> None: