3535import aiohttp
3636from discord .ext import commands
3737from discord .ext .commands .view import StringView
38+ from motor .motor_asyncio import AsyncIOMotorClient
39+
3840from colorama import init , Fore , Style
3941import emoji
4042
41- from core .api import Github , ModmailApiClient
43+ from core .clients import Github , ModmailApiClient , SelfhostedClient
4244from core .thread import ThreadManager
4345from core .config import ConfigManager
4446from core .changelog import ChangeLog
@@ -58,7 +60,10 @@ def __init__(self):
5860 self .threads = ThreadManager (self )
5961 self .session = aiohttp .ClientSession (loop = self .loop )
6062 self .config = ConfigManager (self )
61- self .modmail_api = ModmailApiClient (self )
63+ self .selfhosted = bool (self .config .get ('mongo_uri' ))
64+ if self .selfhosted :
65+ self .db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
66+ self .modmail_api = SelfhostedClient (self ) if self .selfhosted else ModmailApiClient (self )
6267 self .data_task = self .loop .create_task (self .data_loop ())
6368 self .autoupdate_task = self .loop .create_task (self .autoupdate_loop ())
6469 self ._add_commands ()
@@ -158,9 +163,16 @@ async def get_pre(bot, message):
158163 return [bot .prefix , f'<@{ bot .user .id } > ' , f'<@!{ bot .user .id } > ' ]
159164
160165 async def on_connect (self ):
161- print (line + Fore .RED + Style .BRIGHT )
162- await self .validate_api_token ()
163166 print (line )
167+ print (Fore .CYAN , end = '' )
168+ if not self .selfhosted :
169+ print ('MODE: Using the Modmail API' )
170+ print (line )
171+ await self .validate_api_token ()
172+ print (line )
173+ else :
174+ print ('Mode: Selfhosting logs.' )
175+ print (line )
164176 print (Fore .CYAN + 'Connected to gateway.' )
165177 await self .config .refresh ()
166178 status = self .config .get ('status' )
@@ -355,12 +367,16 @@ async def validate_api_token(self):
355367 try :
356368 self .config .modmail_api_token
357369 except KeyError :
370+ print (Fore .RED + Style .BRIGHT , end = '' )
358371 print ('MODMAIL_API_TOKEN not found.' )
359372 print ('Set a config variable called MODMAIL_API_TOKEN with a token from https://dashboard.modmail.tk' )
373+ print ('If you want to selfhost logs, input a MONGO_URI config variable.' )
374+ print ('A modmail api token is not needed if you are selfhosting logs.' )
360375 valid = False
361376 else :
362377 valid = await self .modmail_api .validate_token ()
363378 if not valid :
379+ print (Fore .RED + Style .BRIGHT , end = '' )
364380 print ('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk' )
365381 finally :
366382 if not valid :
@@ -393,11 +409,18 @@ async def data_loop(self):
393409 await asyncio .sleep (3600 )
394410
395411 async def autoupdate_loop (self ):
396- while True :
397- if self .config .get ('disable_autoupdates' ):
398- await asyncio .sleep (3600 )
399- continue
412+ await self .wait_until_ready ()
400413
414+ if self .config .get ('disable_autoupdates' ):
415+ print ('Autoupdates disabled.' )
416+ return
417+
418+ if self .selfhosted and not self .config .get ('github_access_token' ):
419+ print ('Github access token not found.' )
420+ print ('Autoupdates disabled.' )
421+ return
422+
423+ while True :
401424 metadata = await self .modmail_api .get_metadata ()
402425
403426 if metadata ['latest_version' ] != self .version :
0 commit comments