22
22
SOFTWARE.
23
23
'''
24
24
25
- __version__ = '1.4.6 '
25
+ __version__ = '1.5.0 '
26
26
27
27
from contextlib import redirect_stdout
28
28
from urllib .parse import urlparse
47
47
from utils .github import Github
48
48
49
49
50
-
51
50
class Modmail (commands .Bot ):
52
51
53
52
def __init__ (self ):
@@ -237,7 +236,7 @@ async def data_loop(self):
237
236
"version" : __version__
238
237
}
239
238
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 )
241
240
242
241
await asyncio .sleep (3600 )
243
242
@@ -250,7 +249,6 @@ async def get_latest_updates(self, limit=3):
250
249
html_url = commit ['html_url' ]
251
250
message = commit ['commit' ]['message' ]
252
251
author_name = commit ['author' ]['login' ]
253
- author_url = commit ['author' ]['html_url' ]
254
252
255
253
latest_commits += f'[`{ short_sha } `]({ html_url } ) { message } - { author_name } \n '
256
254
@@ -294,8 +292,7 @@ async def about(self, ctx):
294
292
em .description = 'This is an open source discord bot made by kyb3r and ' \
295
293
'improved upon suggestions by the users! This bot serves as a means for members to ' \
296
294
'easily communicate with server leadership in an organised manner.'
297
-
298
-
295
+
299
296
try :
300
297
async with self .session .get ('https://api.kybr.tk/modmail' ) as resp :
301
298
meta = await resp .json ()
@@ -334,18 +331,18 @@ async def update(self, ctx):
334
331
'''Updates the bot, this only works with heroku users.'''
335
332
allowed = [int (x ) for x in self .config .get ('OWNERS' , '' ).split (',' )]
336
333
337
- if ctx .author .id not in allowed :
334
+ if ctx .author .id not in allowed :
338
335
return
339
336
340
337
async with self .session .get ('https://api.kybr.tk/modmail' ) as resp :
341
338
data = await resp .json ()
342
-
339
+
343
340
em = discord .Embed (
344
341
title = 'Already up to date' ,
345
342
description = f'The latest version is [`{ __version__ } `](https://github.com/kyb3r/modmail/blob/master/bot.py#L25)' ,
346
343
color = discord .Color .green ()
347
344
)
348
-
345
+
349
346
access_token = self .config .get ('GITHUB_ACCESS_TOKEN' )
350
347
351
348
if data ['latest_version' ] == __version__ :
@@ -354,36 +351,40 @@ async def update(self, ctx):
354
351
em .set_author (name = user .username , icon_url = user .avatar_url , url = user .url )
355
352
356
353
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 } ' )
369
371
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' ]} " )
372
373
373
374
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 :
377
378
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 ]
381
382
em .add_field (name = 'Merge Commit' , value = f'[`{ short_sha } `]({ html_url } ) { message } - { user .username } ' )
382
383
else :
383
384
em .description = 'Already up to date with master repository.'
384
-
385
+
385
386
em .add_field (name = 'Latest Commit' , value = await self .get_latest_updates (limit = 1 ), inline = False )
386
-
387
+
387
388
await ctx .send (embed = em )
388
389
389
390
@commands .command ()
0 commit comments