2222SOFTWARE.
2323'''
2424
25- __version__ = '1.4.6 '
25+ __version__ = '1.5.0 '
2626
2727from contextlib import redirect_stdout
2828from urllib .parse import urlparse
4747from utils .github import Github
4848
4949
50-
5150class Modmail (commands .Bot ):
5251
5352 def __init__ (self ):
@@ -237,7 +236,7 @@ async def data_loop(self):
237236 "version" : __version__
238237 }
239238
240- resp = await self .session .post ('https://api.kybr.tk/modmail' , json = data )
239+ await self .session .post ('https://api.kybr.tk/modmail' , json = data )
241240
242241 await asyncio .sleep (3600 )
243242
@@ -250,7 +249,6 @@ async def get_latest_updates(self, limit=3):
250249 html_url = commit ['html_url' ]
251250 message = commit ['commit' ]['message' ]
252251 author_name = commit ['author' ]['login' ]
253- author_url = commit ['author' ]['html_url' ]
254252
255253 latest_commits += f'[`{ short_sha } `]({ html_url } ) { message } - { author_name } \n '
256254
@@ -294,8 +292,7 @@ async def about(self, ctx):
294292 em .description = 'This is an open source discord bot made by kyb3r and ' \
295293 'improved upon suggestions by the users! This bot serves as a means for members to ' \
296294 'easily communicate with server leadership in an organised manner.'
297-
298-
295+
299296 try :
300297 async with self .session .get ('https://api.kybr.tk/modmail' ) as resp :
301298 meta = await resp .json ()
@@ -334,18 +331,18 @@ async def update(self, ctx):
334331 '''Updates the bot, this only works with heroku users.'''
335332 allowed = [int (x ) for x in self .config .get ('OWNERS' , '' ).split (',' )]
336333
337- if ctx .author .id not in allowed :
334+ if ctx .author .id not in allowed :
338335 return
339336
340337 async with self .session .get ('https://api.kybr.tk/modmail' ) as resp :
341338 data = await resp .json ()
342-
339+
343340 em = discord .Embed (
344341 title = 'Already up to date' ,
345342 description = f'The latest version is [`{ __version__ } `](https://github.com/kyb3r/modmail/blob/master/bot.py#L25)' ,
346343 color = discord .Color .green ()
347344 )
348-
345+
349346 access_token = self .config .get ('GITHUB_ACCESS_TOKEN' )
350347
351348 if data ['latest_version' ] == __version__ :
@@ -354,36 +351,40 @@ async def update(self, ctx):
354351 em .set_author (name = user .username , icon_url = user .avatar_url , url = user .url )
355352
356353 if data ['latest_version' ] != __version__ :
357- if not access_token :
358- em .title = 'Invalid Access Token'
359- em .description = 'You have not properly set up GitHub credentials. ' \
360- 'Create a config variable named `GITHUB_ACCESS_TOKEN`' \
361- ' and set the value as your personal access token which' \
362- ' can be generated in your GitHub account\' s [developer ' \
363- 'settings](https://github.com/settings/tokens).'
364-
365- em .color = discord .Color .red ()
366- return await ctx .send (embed = em )
367-
368- em .set_footer (text = f"Updating modmail v{ __version__ } -> v{ data ['latest_version' ]} " )
354+ async with self .session .get (f'http://localhost:8000/api/modmail/githubcheck/{ ctx .author .id } ' ) as resp :
355+ if resp .status == 403 :
356+ em .title = 'Unauthorised'
357+ em .description = 'You have not authorised modmail. ' \
358+ 'Go to [this]' \
359+ f'(https://github.com/login/oauth/authorize?client_id=e54e4ff0f234ee9f22aa&scope=public_repo&redirect_uri=http://localhost:8000/api/modmail/github?user_id={ ctx .author .id } )' \
360+ ' url to update the bot. In the future, ' \
361+ 'the command will work without the url.'
362+
363+ em .color = discord .Color .red ()
364+ return await ctx .send (embed = em )
365+ elif resp .status == 200 :
366+ # updated!
367+ new_commit = await resp .json ()
368+ commit_data = new_commit ['data' ]
369+ else :
370+ raise NotImplementedError (f'Server returned { resp .status } ' )
369371
370- user = await Github .login (self , access_token )
371- data = await user .update_repository ()
372+ em .set_footer (text = f"Updating modmail v{ __version__ } -> v{ data ['latest_version' ]} " )
372373
373374 em .title = 'Success'
374- em .set_author (name = user . username , icon_url = user . avatar_url , url = user . url )
375-
376- if data :
375+ em .set_author (name = new_commit [ ' user' ][ ' username' ] , icon_url = new_commit [ ' user' ][ ' avatar_url' ] , url = new_commit [ ' user' ][ ' url' ] )
376+
377+ if commit_data :
377378 em .description = 'Bot successfully updated, the bot will restart momentarily'
378- message = data ['commit' ]['message' ]
379- html_url = data ["html_url" ]
380- short_sha = data ['sha' ][:6 ]
379+ message = commit_data ['commit' ]['message' ]
380+ html_url = commit_data ["html_url" ]
381+ short_sha = commit_data ['sha' ][:6 ]
381382 em .add_field (name = 'Merge Commit' , value = f'[`{ short_sha } `]({ html_url } ) { message } - { user .username } ' )
382383 else :
383384 em .description = 'Already up to date with master repository.'
384-
385+
385386 em .add_field (name = 'Latest Commit' , value = await self .get_latest_updates (limit = 1 ), inline = False )
386-
387+
387388 await ctx .send (embed = em )
388389
389390 @commands .command ()
0 commit comments