1
1
'''
2
2
MIT License
3
3
4
- Copyright (c) 2017 Kyb3r
4
+ Copyright (c) 2017 kyb3r
5
5
6
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
of this software and associated documentation files (the "Software"), to deal
58
58
59
59
class ModmailBot (commands .Bot ):
60
60
61
- mutable_config_keys = ['prefix' , 'status' , 'guild_id' , 'mention' ]
61
+ mutable_config_keys = ['prefix' , 'status' , 'guild_id' , 'mention' , 'autoupdates' , 'modmail_guild_id' ]
62
62
63
63
def __init__ (self ):
64
64
super ().__init__ (command_prefix = self .get_pre )
@@ -69,6 +69,7 @@ def __init__(self):
69
69
self .config = ConfigManager (self )
70
70
self .modmail_api = ModmailApiClient (self )
71
71
self .data_task = self .loop .create_task (self .data_loop ())
72
+ self .autoupdate_task = self .loop .create_task (self .autoupdate_loop ())
72
73
self ._add_commands ()
73
74
74
75
def _add_commands (self ):
@@ -80,7 +81,7 @@ def _add_commands(self):
80
81
'││││ │ │││││├─┤││' ,
81
82
'┴ ┴└─┘─┴┘┴ ┴┴ ┴┴┴─┘' , sep = '\n ' )
82
83
print (f'v{ __version__ } ' )
83
- print ('Author : kyb3r' + Style .RESET_ALL )
84
+ print ('Authors : kyb3r, fourjr ' + Style .RESET_ALL )
84
85
print (line + Fore .CYAN )
85
86
86
87
@@ -94,6 +95,7 @@ def _add_commands(self):
94
95
async def logout (self ):
95
96
await self .session .close ()
96
97
self .data_task .cancel ()
98
+ self .autoupdate_task .cancel ()
97
99
await super ().logout ()
98
100
99
101
def run (self ):
@@ -313,6 +315,41 @@ async def data_loop(self):
313
315
await self .session .post ('https://api.modmail.tk/metadata' , json = data )
314
316
315
317
await asyncio .sleep (3600 )
318
+
319
+ async def autoupdate_loop (self ):
320
+ while True :
321
+ if not self .config .get ('autoupdates' ):
322
+ await asyncio .sleep (3600 )
323
+ continue
324
+
325
+ metadata = await self .modmail_api .get_metadata ()
326
+
327
+ if metadata [version ] != self .bot .version :
328
+ data = await self .modmail_api .update_repository ()
329
+
330
+ em = discord .Embed (title = 'Updating bot' , color = discord .Color .green ())
331
+
332
+ commit_data = data ['data' ]
333
+ user = data ['user' ]
334
+ em .set_author (name = user ['username' ], icon_url = user ['avatar_url' ], url = user ['url' ])
335
+ em .set_footer (text = f"Updating modmail v{ self .version } -> v{ metadata ['latest_version' ]} " )
336
+
337
+ if commit_data :
338
+ em .description = 'Bot successfully updated, the bot will restart momentarily'
339
+ message = commit_data ['commit' ]['message' ]
340
+ html_url = commit_data ["html_url" ]
341
+ short_sha = commit_data ['sha' ][:6 ]
342
+ em .add_field (name = 'Merge Commit' , value = f"[`{ short_sha } `]({ html_url } ) { message } - { user ['username' ]} " )
343
+ else :
344
+ em .description = 'Already up to date with master repository.'
345
+
346
+ em .add_field (name = 'Latest Commit' , value = await self .get_latest_updates (limit = 1 ), inline = False )
347
+
348
+ channel = self .main_category .channels [0 ]
349
+ await channel .send (embed = em )
350
+
351
+ await asyncio .sleep (3600 )
352
+
316
353
317
354
async def get_latest_updates (self , limit = 3 ):
318
355
latest_commits = ''
0 commit comments