mirror of
https://github.com/MODSetter/SurfSense.git
synced 2025-09-04 11:39:19 +00:00
feat: added more validation, discord bot connection timeout
This commit is contained in:
parent
6b38129623
commit
afc1100f6d
1 changed files with 56 additions and 8 deletions
|
@ -33,27 +33,68 @@ class DiscordConnector(commands.Bot):
|
||||||
super().__init__(command_prefix="!", intents=intents) # command_prefix is required but not strictly used here
|
super().__init__(command_prefix="!", intents=intents) # command_prefix is required but not strictly used here
|
||||||
self.token = token
|
self.token = token
|
||||||
self._bot_task = None # Holds the async bot task
|
self._bot_task = None # Holds the async bot task
|
||||||
|
self._is_running = False # Flag to track if the bot is running
|
||||||
|
|
||||||
# Event to confirm bot is ready
|
# Event to confirm bot is ready
|
||||||
@self.event
|
@self.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
logger.info(f"Logged in as {self.user} (ID: {self.user.id})")
|
logger.info(f"Logged in as {self.user} (ID: {self.user.id})")
|
||||||
|
|
||||||
|
@self.event
|
||||||
|
async def on_connect():
|
||||||
|
logger.debug("Bot connected to Discord gateway.")
|
||||||
|
|
||||||
|
@self.event
|
||||||
|
async def on_disconnect():
|
||||||
|
logger.debug("Bot disconnected from Discord gateway.")
|
||||||
|
self._is_running = False # Reset flag on disconnect
|
||||||
|
|
||||||
|
@self.event
|
||||||
|
async def on_resumed():
|
||||||
|
logger.debug("Bot resumed connection to Discord gateway.")
|
||||||
|
|
||||||
async def start_bot(self):
|
async def start_bot(self):
|
||||||
"""Starts the bot to connect to Discord."""
|
"""Starts the bot to connect to Discord."""
|
||||||
logger.info("Starting Discord bot...")
|
logger.info("Starting Discord bot...")
|
||||||
|
|
||||||
if not self.token:
|
if not self.token:
|
||||||
raise ValueError("Discord bot token not set. Call set_token(token) first.")
|
raise ValueError("Discord bot token not set. Call set_token(token) first.")
|
||||||
await self.start(self.token)
|
|
||||||
logger.info("Discord bot started successfully.")
|
try:
|
||||||
|
await asyncio.wait_for(self.start(self.token), timeout=60.0)
|
||||||
|
logger.info("Discord bot started successfully.")
|
||||||
|
except discord.LoginFailure:
|
||||||
|
logger.error("Failed to log in: Invalid token was provided. Please check your bot token.")
|
||||||
|
self._is_running = False
|
||||||
|
raise
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logger.error("Timed out while trying to connect to Discord. "
|
||||||
|
"This might indicate network issues or an invalid token.")
|
||||||
|
self._is_running = False
|
||||||
|
raise
|
||||||
|
except discord.PrivilegedIntentsRequired as e:
|
||||||
|
logger.error(f"Privileged Intents Required: {e}. Make sure all required intents are enabled in your bot's application page.")
|
||||||
|
self._is_running = False
|
||||||
|
raise
|
||||||
|
except discord.ConnectionClosed as e:
|
||||||
|
logger.error(f"Discord connection closed unexpectedly: {e}")
|
||||||
|
self._is_running = False
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"An unexpected error occurred while starting the bot: {e}")
|
||||||
|
self._is_running = False
|
||||||
|
raise
|
||||||
|
|
||||||
async def close_bot(self):
|
async def close_bot(self):
|
||||||
"""Closes the bot's connection to Discord."""
|
"""Closes the bot's connection to Discord."""
|
||||||
logger.info("Closing Discord bot connection...")
|
|
||||||
|
if self._is_running:
|
||||||
await self.close()
|
logger.info("Closing Discord bot connection...")
|
||||||
logger.info("Discord bot connection closed.")
|
await self.close()
|
||||||
|
logger.info("Discord bot connection closed.")
|
||||||
|
self._is_running = False
|
||||||
|
else:
|
||||||
|
logger.info("Bot is not running or already disconnected.")
|
||||||
|
|
||||||
|
|
||||||
def set_token(self, token: str) -> None:
|
def set_token(self, token: str) -> None:
|
||||||
|
@ -76,8 +117,15 @@ class DiscordConnector(commands.Bot):
|
||||||
# Terrible solution, but necessary to avoid blocking the event loop.
|
# Terrible solution, but necessary to avoid blocking the event loop.
|
||||||
await asyncio.sleep(1) # Yield control to the event loop
|
await asyncio.sleep(1) # Yield control to the event loop
|
||||||
|
|
||||||
await self.wait_until_ready()
|
try:
|
||||||
logger.info("Bot is ready.")
|
await asyncio.wait_for(self.wait_until_ready(), timeout=60.0)
|
||||||
|
logger.info("Bot is ready.")
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logger.error(f"Bot did not become ready within 60 seconds. Connection may have failed.")
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"An unexpected error occurred while waiting for the bot to be ready: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
async def get_guilds(self) -> list[dict]:
|
async def get_guilds(self) -> list[dict]:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Reference in a new issue