44
44
import aiohttp
45
45
46
46
from utils .paginator import PaginatorSession
47
- from utils .github import Github
48
-
47
+ from utils .api import Github , ModmailApiClient
49
48
50
49
51
50
class Modmail (commands .Bot ):
@@ -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,13 +249,11 @@ 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
257
255
return latest_commits
258
256
259
-
260
257
@property
261
258
def uptime (self ):
262
259
now = datetime .datetime .utcnow ()
@@ -294,8 +291,7 @@ async def about(self, ctx):
294
291
em .description = 'This is an open source discord bot made by kyb3r and ' \
295
292
'improved upon suggestions by the users! This bot serves as a means for members to ' \
296
293
'easily communicate with server leadership in an organised manner.'
297
-
298
-
294
+
299
295
try :
300
296
async with self .session .get ('https://api.kybr.tk/modmail' ) as resp :
301
297
meta = await resp .json ()
@@ -328,62 +324,126 @@ async def about(self, ctx):
328
324
329
325
await ctx .send (embed = em )
330
326
327
+ @commands .group (invoke_without_subcommand = True )
328
+ async def github (self , ctx ):
329
+ if ctx .invoked_subcommand :
330
+ return
331
+
332
+ client = ModmailApiClient (self )
333
+ data = await client .get_user_info ()
334
+
335
+ prefix = self .config .get ('PREFIX' , 'm.' )
336
+
337
+ em = discord .Embed (
338
+ title = 'Github' ,
339
+ color = discord .Color .red (),
340
+ description = f'Not logged in, do `{ prefix } github login` to login with GitHub.'
341
+ )
342
+ em .add_field (name = 'Subcommands' , value = f'`{ prefix } github login`\n `{ prefix } github logout`' )
343
+
344
+ if not data ['error' ]:
345
+ user = data ['user' ]
346
+ em .color = discord .Color .green ()
347
+ em .description = f"Currently logged in."
348
+ em .set_author (name = user ['username' ], icon_url = user ['avatar_url' ], url = user ['url' ])
349
+ em .set_thumbnail (url = user ['avatar_url' ])
350
+ await ctx .send (embed = em )
351
+ else :
352
+ await ctx .send (embed = em )
353
+
354
+ @github .command (name = 'login' )
355
+ async def _login (self , ctx ):
356
+ client = ModmailApiClient (self )
357
+
358
+ oauth_url = 'https://github.com/login/oauth/authorize?client_id' \
359
+ '=e54e4ff0f234ee9f22aa&scope=public_repo&redirect_uri=' \
360
+ 'https://api.kybr.tk/modmail/github/callback' \
361
+ f'?token={ client .token } '
362
+
363
+ em = discord .Embed (
364
+ color = discord .Color .green (),
365
+ title = 'Login with GitHub' ,
366
+ description = 'In order to use the update command, you need ' \
367
+ 'to have fork the [repo](https://github.com/kyb3r/modmail) and ' \
368
+ 'login with GitHub so that we can update your fork to ' \
369
+ 'match the main repository whenever there is an update.' \
370
+ 'Click the link below to be taken to log in with github to authorize Modmail.'
371
+ )
372
+ em .set_thumbnail (url = 'https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png' )
373
+
374
+ em .add_field (name = 'Login' , value = f'[Click Here]({ oauth_url } )' , inline = False )
375
+ em .add_field (name = 'Warning' , value = 'Dont share this link as it contains sensitive information.' )
376
+ await ctx .author .send (embed = em )
377
+
378
+ @github .command (name = 'logout' )
379
+ async def _logout (self , ctx ):
380
+ client = ModmailApiClient (self )
381
+ data = await client .logout ()
382
+
383
+ em = discord .Embed (
384
+ color = discord .Color .green (),
385
+ title = 'Logged out' ,
386
+ description = 'Successfully logged out.'
387
+ )
388
+
389
+ if data ['error' ]:
390
+ em .color = discord .Color .red ()
391
+ em .title = 'Error'
392
+ em .description = 'You are not logged in already.'
393
+
394
+ await ctx .send (embed = em )
331
395
332
396
@commands .command ()
333
397
async def update (self , ctx ):
334
398
'''Updates the bot, this only works with heroku users.'''
335
399
allowed = [int (x ) for x in self .config .get ('OWNERS' , '' ).split (',' )]
336
400
337
- if ctx .author .id not in allowed :
401
+ if ctx .author .id not in allowed :
338
402
return
339
403
340
- async with self .session .get ('https://api.kybr.tk/modmail' ) as resp :
341
- data = await resp .json ()
342
-
404
+ client = ModmailApiClient (self )
405
+
406
+ metadata = await client .get_metadata ()
407
+
343
408
em = discord .Embed (
344
409
title = 'Already up to date' ,
345
410
description = f'The latest version is [`{ __version__ } `](https://github.com/kyb3r/modmail/blob/master/bot.py#L25)' ,
346
411
color = discord .Color .green ()
347
- )
348
-
349
- access_token = self .config .get ('GITHUB_ACCESS_TOKEN' )
350
-
351
- if data ['latest_version' ] == __version__ :
352
- if access_token :
353
- user = await Github .login (self , access_token )
354
- em .set_author (name = user .username , icon_url = user .avatar_url , url = user .url )
355
-
356
- 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).'
412
+ )
413
+
414
+ if metadata ['latest_version' ] == __version__ :
415
+ data = await client .get_user_info ()
416
+ if not data ['error' ]:
417
+ user = data ['user' ]
418
+ em .set_author (name = user ['username' ], icon_url = user ['avatar_url' ], url = user ['url' ])
364
419
420
+ if metadata ['latest_version' ] != __version__ :
421
+ data = await client .update_repository ()
422
+
423
+ if data ['error' ]:
424
+ prefix = self .config .get ('PREFIX' , 'm.' )
425
+ em .title = 'Unauthorised'
426
+ em .description = f"You haven't logged in with github yet. Type the command `{ prefix } github login` to authorize this bot."
365
427
em .color = discord .Color .red ()
366
428
return await ctx .send (embed = em )
367
-
368
- em .set_footer (text = f"Updating modmail v{ __version__ } -> v{ data ['latest_version' ]} " )
369
-
370
- user = await Github .login (self , access_token )
371
- data = await user .update_repository ()
372
429
430
+ commit_data = data ['data' ]
431
+ user = data ['user' ]
373
432
em .title = 'Success'
374
- em .set_author (name = user .username , icon_url = user .avatar_url , url = user .url )
375
-
376
- if data :
433
+ em .set_author (name = user ['username' ], icon_url = user ['avatar_url' ], url = user ['url' ])
434
+ em .set_footer (text = f"Updating modmail v{ __version__ } -> v{ metadata ['latest_version' ]} " )
435
+
436
+ if commit_data :
377
437
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 ]
381
- em .add_field (name = 'Merge Commit' , value = f' [`{ short_sha } `]({ html_url } ) { message } - { user . username } ' )
438
+ message = commit_data ['commit' ]['message' ]
439
+ html_url = commit_data ["html_url" ]
440
+ short_sha = commit_data ['sha' ][:6 ]
441
+ em .add_field (name = 'Merge Commit' , value = f" [`{ short_sha } `]({ html_url } ) { message } - { user [ ' username' ] } " )
382
442
else :
383
443
em .description = 'Already up to date with master repository.'
384
-
444
+
385
445
em .add_field (name = 'Latest Commit' , value = await self .get_latest_updates (limit = 1 ), inline = False )
386
-
446
+
387
447
await ctx .send (embed = em )
388
448
389
449
@commands .command ()
0 commit comments