46
46
from core .thread import ThreadManager
47
47
from core .config import ConfigManager
48
48
from core .changelog import ChangeLog
49
+ from core .objects import Bot
49
50
50
51
51
52
colorama .init ()
54
55
Style .RESET_ALL
55
56
56
57
57
- class ModmailBot (commands . Bot ):
58
+ class ModmailBot (Bot ):
58
59
59
60
def __init__ (self ):
60
61
super ().__init__ (command_prefix = None ) # implemented in `get_prefix`
61
- self .version = __version__
62
- self .start_time = datetime .utcnow ()
63
- self .threads = ThreadManager (self )
64
- self .session = ClientSession (loop = self .loop )
65
- self .config = ConfigManager (self )
66
- self .self_hosted = bool (self .config .get ('mongo_uri' , '' ))
62
+ self ._threads = None
63
+ self ._session = None
64
+ self ._config = None
67
65
self ._connected = Event ()
66
+ self ._db = None
68
67
69
68
if self .self_hosted :
70
- self .db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
71
- self .modmail_api = SelfHostedClient (self )
69
+ self ._db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
70
+ self ._api = SelfHostedClient (self )
72
71
else :
73
- self .modmail_api = ModmailApiClient (self )
72
+ self ._api = ModmailApiClient (self )
74
73
75
74
self .data_task = self .loop .create_task (self .data_loop ())
76
75
self .autoupdate_task = self .loop .create_task (self .autoupdate_loop ())
77
- self ._add_commands ()
76
+ self ._load_extensions ()
78
77
self .owner = None
79
78
79
+ @property
80
+ def version (self ):
81
+ return __version__
82
+
83
+ @property
84
+ def db (self ):
85
+ return self ._db
86
+
87
+ @property
88
+ def self_hosted (self ):
89
+ return bool (self .config .get ('mongo_uri' , '' ))
90
+
91
+ @property
92
+ def api (self ):
93
+ return self ._api
94
+
95
+ @property
96
+ def config (self ):
97
+ if self ._config is None :
98
+ self ._config = ConfigManager (self )
99
+ return self ._config
100
+
101
+ @property
102
+ def session (self ):
103
+ if self ._session is None :
104
+ self ._session = ClientSession (loop = self .loop )
105
+ return self ._session
106
+
107
+ @property
108
+ def threads (self ):
109
+ if self ._threads is None :
110
+ self ._threads = ThreadManager (self )
111
+ return self ._threads
112
+
80
113
async def get_prefix (self , message = None ):
81
114
return [self .prefix , f'<@{ self .user .id } > ' , f'<@!{ self .user .id } > ' ]
82
115
83
- def _add_commands (self ):
116
+ def _load_extensions (self ):
84
117
"""Adds commands automatically"""
85
118
self .remove_command ('help' )
86
119
@@ -397,7 +430,7 @@ async def on_message(self, message):
397
430
398
431
thread = await self .threads .find (channel = ctx .channel )
399
432
if thread is not None :
400
- await self .modmail_api .append_log (message , type_ = 'internal' )
433
+ await self .api .append_log (message , type_ = 'internal' )
401
434
402
435
async def on_guild_channel_delete (self , channel ):
403
436
if channel .guild != self .modmail_guild :
@@ -454,7 +487,8 @@ async def on_command_error(self, ctx, error):
454
487
else :
455
488
raise error
456
489
457
- def overwrites (self , ctx ): # TODO: can this be static?
490
+ @staticmethod
491
+ def overwrites (ctx ):
458
492
"""Permission overwrites for the guild."""
459
493
overwrites = {
460
494
ctx .guild .default_role : discord .PermissionOverwrite (
@@ -486,14 +520,14 @@ async def validate_api_token(self):
486
520
'if you are self-hosting logs.' )
487
521
return await self .logout ()
488
522
else :
489
- valid = await self .modmail_api .validate_token ()
523
+ valid = await self .api .validate_token ()
490
524
if not valid :
491
525
print (Fore .RED , end = '' )
492
526
print ('Invalid MODMAIL_API_TOKEN - get one '
493
527
'from https://dashboard.modmail.tk' )
494
528
return await self .logout ()
495
529
496
- user = await self .modmail_api .get_user_info ()
530
+ user = await self .api .get_user_info ()
497
531
username = user ['user' ]['username' ]
498
532
print (Style .RESET_ALL + Fore .CYAN + 'Validated token.' )
499
533
print ('GitHub user: ' + username + Style .RESET_ALL )
@@ -533,7 +567,7 @@ async def data_loop(self):
533
567
"last_updated" : str (datetime .utcnow ())
534
568
}
535
569
536
- await self .modmail_api .post_metadata (data )
570
+ await self .api .post_metadata (data )
537
571
await sleep (3600 )
538
572
539
573
async def autoupdate_loop (self ):
@@ -551,10 +585,10 @@ async def autoupdate_loop(self):
551
585
return
552
586
553
587
while True :
554
- metadata = await self .modmail_api .get_metadata ()
588
+ metadata = await self .api .get_metadata ()
555
589
556
590
if metadata ['latest_version' ] != self .version :
557
- data = await self .modmail_api .update_repository ()
591
+ data = await self .api .update_repository ()
558
592
559
593
embed = discord .Embed (color = discord .Color .green ())
560
594
@@ -598,20 +632,6 @@ async def get_latest_updates(self, limit=3):
598
632
599
633
return latest_commits
600
634
601
- @property
602
- def uptime (self ):
603
- now = datetime .utcnow ()
604
- delta = now - self .start_time
605
- hours , remainder = divmod (int (delta .total_seconds ()), 3600 )
606
- minutes , seconds = divmod (remainder , 60 )
607
- days , hours = divmod (hours , 24 )
608
-
609
- fmt = '{h}h {m}m {s}s'
610
- if days :
611
- fmt = '{d}d ' + fmt
612
-
613
- return fmt .format (d = days , h = hours , m = minutes , s = seconds )
614
-
615
635
616
636
if __name__ == '__main__' :
617
637
uvloop .install ()
0 commit comments