@@ -62,6 +62,7 @@ def __init__(self):
6262 self .session = aiohttp .ClientSession (loop = self .loop )
6363 self .config = ConfigManager (self )
6464 self .selfhosted = bool (self .config .get ('mongo_uri' ))
65+ self ._connected = asyncio .Event ()
6566 if self .selfhosted :
6667 self .db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
6768 self .modmail_api = SelfhostedClient (self ) if self .selfhosted else ModmailApiClient (self )
@@ -198,6 +199,7 @@ async def on_connect(self):
198199 await self .validate_api_token ()
199200 print (line )
200201 else :
202+ await self .validate_database_connection ()
201203 print ('Mode: Selfhosting logs.' )
202204 print (line )
203205 print (Fore .CYAN + 'Connected to gateway.' )
@@ -212,9 +214,14 @@ async def on_connect(self):
212214 activity = discord .Activity (type = activity_type , name = message ,
213215 url = url )
214216 await self .change_presence (activity = activity )
217+
218+ self ._connected .set ()
215219
216220 async def on_ready (self ):
217221 """Bot startup, sets uptime."""
222+
223+ await self ._connected .wait ()
224+
218225 print (textwrap .dedent (f"""
219226 { line }
220227 { Fore .CYAN } Client ready.
@@ -426,7 +433,6 @@ def overwrites(self, ctx):
426433 return overwrites
427434
428435 async def validate_api_token (self ):
429- valid = True
430436 try :
431437 self .config .modmail_api_token
432438 except KeyError :
@@ -439,7 +445,7 @@ async def validate_api_token(self):
439445 else :
440446 valid = await self .modmail_api .validate_token ()
441447 if not valid :
442- print (Fore .RED + Style . BRIGHT , end = '' )
448+ print (Fore .RED , end = '' )
443449 print ('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk' )
444450 finally :
445451 if not valid :
@@ -448,6 +454,21 @@ async def validate_api_token(self):
448454 username = (await self .modmail_api .get_user_info ())['user' ]['username' ]
449455 print (Style .RESET_ALL + Fore .CYAN + 'Validated token.' )
450456 print (f'GitHub user: { username } ' + Style .RESET_ALL )
457+
458+ async def validate_database_connection (self ):
459+ try :
460+ doc = await self .db .command ('buildinfo' )
461+ except Exception as e :
462+ valid = False
463+ print (Fore .RED , end = '' )
464+ print ('Something went wrong while connecting to the database.' )
465+ print (type (e ).__name__ , e , sep = ': ' )
466+ else :
467+ valid = True
468+ print (Style .RESET_ALL + Fore .CYAN + 'Successfully connected to the database.' )
469+ finally :
470+ if not valid :
471+ await self .logout ()
451472
452473 async def data_loop (self ):
453474 await self .wait_until_ready ()
@@ -479,11 +500,13 @@ async def autoupdate_loop(self):
479500
480501 if self .config .get ('disable_autoupdates' ):
481502 print ('Autoupdates disabled.' )
503+ print (line )
482504 return
483505
484506 if self .selfhosted and not self .config .get ('github_access_token' ):
485507 print ('Github access token not found.' )
486508 print ('Autoupdates disabled.' )
509+ print (line )
487510 return
488511
489512 while True :
0 commit comments