35
35
import aiohttp
36
36
from discord .ext import commands
37
37
from discord .ext .commands .view import StringView
38
+ from motor .motor_asyncio import AsyncIOMotorClient
39
+
38
40
from colorama import init , Fore , Style
39
41
import emoji
40
42
41
- from core .api import Github , ModmailApiClient
43
+ from core .clients import Github , ModmailApiClient , SelfhostedClient
42
44
from core .thread import ThreadManager
43
45
from core .config import ConfigManager
44
46
from core .changelog import ChangeLog
@@ -58,7 +60,10 @@ def __init__(self):
58
60
self .threads = ThreadManager (self )
59
61
self .session = aiohttp .ClientSession (loop = self .loop )
60
62
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 )
62
67
self .data_task = self .loop .create_task (self .data_loop ())
63
68
self .autoupdate_task = self .loop .create_task (self .autoupdate_loop ())
64
69
self ._add_commands ()
@@ -158,9 +163,16 @@ async def get_pre(bot, message):
158
163
return [bot .prefix , f'<@{ bot .user .id } > ' , f'<@!{ bot .user .id } > ' ]
159
164
160
165
async def on_connect (self ):
161
- print (line + Fore .RED + Style .BRIGHT )
162
- await self .validate_api_token ()
163
166
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 )
164
176
print (Fore .CYAN + 'Connected to gateway.' )
165
177
await self .config .refresh ()
166
178
status = self .config .get ('status' )
@@ -355,12 +367,16 @@ async def validate_api_token(self):
355
367
try :
356
368
self .config .modmail_api_token
357
369
except KeyError :
370
+ print (Fore .RED + Style .BRIGHT , end = '' )
358
371
print ('MODMAIL_API_TOKEN not found.' )
359
372
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.' )
360
375
valid = False
361
376
else :
362
377
valid = await self .modmail_api .validate_token ()
363
378
if not valid :
379
+ print (Fore .RED + Style .BRIGHT , end = '' )
364
380
print ('Invalid MODMAIL_API_TOKEN - get one from https://dashboard.modmail.tk' )
365
381
finally :
366
382
if not valid :
@@ -393,11 +409,18 @@ async def data_loop(self):
393
409
await asyncio .sleep (3600 )
394
410
395
411
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 ()
400
413
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 :
401
424
metadata = await self .modmail_api .get_metadata ()
402
425
403
426
if metadata ['latest_version' ] != self .version :
0 commit comments