@@ -62,6 +62,7 @@ def __init__(self):
62
62
self .session = aiohttp .ClientSession (loop = self .loop )
63
63
self .config = ConfigManager (self )
64
64
self .selfhosted = bool (self .config .get ('mongo_uri' ))
65
+ self ._connected = asyncio .Event ()
65
66
if self .selfhosted :
66
67
self .db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
67
68
self .modmail_api = SelfhostedClient (self ) if self .selfhosted else ModmailApiClient (self )
@@ -198,6 +199,7 @@ async def on_connect(self):
198
199
await self .validate_api_token ()
199
200
print (line )
200
201
else :
202
+ await self .validate_database_connection ()
201
203
print ('Mode: Selfhosting logs.' )
202
204
print (line )
203
205
print (Fore .CYAN + 'Connected to gateway.' )
@@ -212,9 +214,14 @@ async def on_connect(self):
212
214
activity = discord .Activity (type = activity_type , name = message ,
213
215
url = url )
214
216
await self .change_presence (activity = activity )
217
+
218
+ self ._connected .set ()
215
219
216
220
async def on_ready (self ):
217
221
"""Bot startup, sets uptime."""
222
+
223
+ await self ._connected .wait ()
224
+
218
225
print (textwrap .dedent (f"""
219
226
{ line }
220
227
{ Fore .CYAN } Client ready.
@@ -426,7 +433,6 @@ def overwrites(self, ctx):
426
433
return overwrites
427
434
428
435
async def validate_api_token (self ):
429
- valid = True
430
436
try :
431
437
self .config .modmail_api_token
432
438
except KeyError :
@@ -439,7 +445,7 @@ async def validate_api_token(self):
439
445
else :
440
446
valid = await self .modmail_api .validate_token ()
441
447
if not valid :
442
- print (Fore .RED + Style . BRIGHT , end = '' )
448
+ print (Fore .RED , end = '' )
443
449
print ('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk' )
444
450
finally :
445
451
if not valid :
@@ -448,6 +454,21 @@ async def validate_api_token(self):
448
454
username = (await self .modmail_api .get_user_info ())['user' ]['username' ]
449
455
print (Style .RESET_ALL + Fore .CYAN + 'Validated token.' )
450
456
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 ()
451
472
452
473
async def data_loop (self ):
453
474
await self .wait_until_ready ()
@@ -479,11 +500,13 @@ async def autoupdate_loop(self):
479
500
480
501
if self .config .get ('disable_autoupdates' ):
481
502
print ('Autoupdates disabled.' )
503
+ print (line )
482
504
return
483
505
484
506
if self .selfhosted and not self .config .get ('github_access_token' ):
485
507
print ('Github access token not found.' )
486
508
print ('Autoupdates disabled.' )
509
+ print (line )
487
510
return
488
511
489
512
while True :
0 commit comments