11
11
from types import SimpleNamespace
12
12
13
13
import discord
14
- from discord .ext import commands
14
+ from discord .ext import commands , tasks
15
15
from discord .ext .commands .view import StringView
16
16
17
17
import isodate
@@ -70,6 +70,7 @@ def __init__(self):
70
70
71
71
self .config = ConfigManager (self )
72
72
self .config .populate_cache ()
73
+
73
74
self .threads = ThreadManager (self )
74
75
75
76
self ._configure_logging ()
@@ -81,7 +82,8 @@ def __init__(self):
81
82
self .db = AsyncIOMotorClient (mongo_uri ).modmail_bot
82
83
self .plugin_db = PluginDatabaseClient (self )
83
84
84
- self .metadata_task = self .loop .create_task (self .metadata_loop ())
85
+ self .metadata_loop = None
86
+
85
87
self ._load_extensions ()
86
88
87
89
@property
@@ -183,12 +185,6 @@ def run(self, *args, **kwargs):
183
185
except Exception :
184
186
logger .critical ("Fatal exception" , exc_info = True )
185
187
finally :
186
- try :
187
- self .metadata_task .cancel ()
188
- self .loop .run_until_complete (self .metadata_task )
189
- except asyncio .CancelledError :
190
- logger .debug ("metadata_task has been cancelled." )
191
-
192
188
self .loop .run_until_complete (self .logout ())
193
189
for task in asyncio .all_tasks (self .loop ):
194
190
task .cancel ()
@@ -437,6 +433,19 @@ async def on_ready(self):
437
433
438
434
logger .line ()
439
435
436
+ self .metadata_loop = tasks .Loop (
437
+ self .post_metadata ,
438
+ seconds = 0 ,
439
+ minutes = 0 ,
440
+ hours = 1 ,
441
+ count = None ,
442
+ reconnect = True ,
443
+ loop = None ,
444
+ )
445
+ self .metadata_loop .before_loop (self .before_post_metadata )
446
+ self .metadata_loop .after_loop (self .after_post_metadata )
447
+ self .metadata_loop .start ()
448
+
440
449
async def convert_emoji (self , name : str ) -> str :
441
450
ctx = SimpleNamespace (bot = self , guild = self .modmail_guild )
442
451
converter = commands .EmojiConverter ()
@@ -958,7 +967,7 @@ async def validate_database_connection(self):
958
967
try :
959
968
await self .db .command ("buildinfo" )
960
969
except Exception as exc :
961
- logger .critical ("Something went wrong " " while connecting to the database." )
970
+ logger .critical ("Something went wrong while connecting to the database." )
962
971
message = f"{ type (exc ).__name__ } : { str (exc )} "
963
972
logger .critical (message )
964
973
@@ -981,43 +990,44 @@ async def validate_database_connection(self):
981
990
else :
982
991
logger .info ("Successfully connected to the database." )
983
992
984
- async def metadata_loop (self ):
993
+ async def post_metadata (self ):
994
+ owner = (await self .application_info ()).owner
995
+ data = {
996
+ "owner_name" : str (owner ),
997
+ "owner_id" : owner .id ,
998
+ "bot_id" : self .user .id ,
999
+ "bot_name" : str (self .user ),
1000
+ "avatar_url" : str (self .user .avatar_url ),
1001
+ "guild_id" : self .guild_id ,
1002
+ "guild_name" : self .guild .name ,
1003
+ "member_count" : len (self .guild .members ),
1004
+ "uptime" : (datetime .utcnow () - self .start_time ).total_seconds (),
1005
+ "latency" : f"{ self .ws .latency * 1000 :.4f} " ,
1006
+ "version" : self .version ,
1007
+ "selfhosted" : True ,
1008
+ "last_updated" : str (datetime .utcnow ()),
1009
+ }
1010
+
1011
+ async with self .session .post ("https://api.modmail.tk/metadata" , json = data ):
1012
+ logger .debug ("Uploading metadata to Modmail server." )
1013
+
1014
+ async def before_post_metadata (self ):
1015
+ logger .info ("Starting metadata loop." )
985
1016
await self .wait_for_connected ()
986
1017
if not self .guild :
987
- return
988
-
989
- owner = (await self .application_info ()).owner
1018
+ self .metadata_loop .cancel ()
990
1019
991
- while not self .is_closed ():
992
- data = {
993
- "owner_name" : str (owner ),
994
- "owner_id" : owner .id ,
995
- "bot_id" : self .user .id ,
996
- "bot_name" : str (self .user ),
997
- "avatar_url" : str (self .user .avatar_url ),
998
- "guild_id" : self .guild_id ,
999
- "guild_name" : self .guild .name ,
1000
- "member_count" : len (self .guild .members ),
1001
- "uptime" : (datetime .utcnow () - self .start_time ).total_seconds (),
1002
- "latency" : f"{ self .ws .latency * 1000 :.4f} " ,
1003
- "version" : self .version ,
1004
- "selfhosted" : True ,
1005
- "last_updated" : str (datetime .utcnow ()),
1006
- }
1007
-
1008
- async with self .session .post ("https://api.modmail.tk/metadata" , json = data ):
1009
- logger .debug ("Uploading metadata to Modmail server." )
1010
-
1011
- await asyncio .sleep (3600 )
1020
+ async def after_post_metadata (self ):
1021
+ logger .info ("Metadata loop has been cancelled." )
1012
1022
1013
1023
1014
1024
if __name__ == "__main__" :
1015
- if os .name != "nt" :
1016
- try :
1017
- import uvloop
1025
+ try :
1026
+ import uvloop
1027
+
1028
+ uvloop .install ()
1029
+ except ImportError :
1030
+ pass
1018
1031
1019
- uvloop .install ()
1020
- except ImportError :
1021
- pass
1022
1032
bot = ModmailBot ()
1023
1033
bot .run ()
0 commit comments