Skip to content

Commit 5dedd79

Browse files
committed
Oauth - 1.5.0
1 parent 88d57a7 commit 5dedd79

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed

bot.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
'''
2424

25-
__version__ = '1.4.6'
25+
__version__ = '1.5.0'
2626

2727
from contextlib import redirect_stdout
2828
from urllib.parse import urlparse
@@ -47,7 +47,6 @@
4747
from utils.github import Github
4848

4949

50-
5150
class 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()

config.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
2-
"TOKEN": null,
3-
"PREFIX": null,
4-
"GUILD_ID": null,
5-
"STATUS": null
6-
}
2+
"TOKEN": "NDAzNzE1OTMyMTIxMzMzNzYw.DvzrRQ.D-UzEUeZx52-7V9zHQ7wrz1TnGw",
3+
"PREFIX": "dev.",
4+
"GUILD_ID": 522924682698293281,
5+
"STATUS": "DM ganugajgnag",
6+
"OWNERS": "325012556940836864,180314310298304512"
7+
}

utils/github.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ class Github:
33
merge_url = 'https://api.github.com/repos/{username}/modmail/merges'
44
commit_url = 'https://api.github.com/repos/kyb3r/modmail/commits'
55

6-
def __init__(self, bot, access_token=None, username=None):
7-
self.bot = bot
8-
self.session = bot.session
6+
def __init__(self, app, access_token=None, username=None):
7+
self.app = app
8+
self.session = app.session
99
self.access_token = access_token
1010
self.username = username
1111
self.avatar_url = None
1212
self.url = None
1313
self.headers = None
1414
if self.access_token:
15-
self.headers = {'Authorization': 'Bearer '+ str(access_token)}
16-
15+
self.headers = {'Authorization': 'token ' + str(access_token)}
16+
1717
async def get_latest_commits(self, limit=3):
1818
resp = await self.request(self.commit_url)
1919
for index in range(limit):
2020
yield resp[index]
21-
21+
2222
async def update_repository(self, sha=None):
2323
if sha is None:
2424
resp = await self.request(self.head)
@@ -42,12 +42,12 @@ async def request(self, url, method='GET', payload=None):
4242
return await resp.json()
4343
except:
4444
return await resp.text()
45-
45+
4646
@classmethod
4747
async def login(cls, bot, access_token):
4848
self = cls(bot, access_token)
4949
resp = await self.request('https://api.github.com/user')
5050
self.username = resp['login']
5151
self.avatar_url = resp['avatar_url']
5252
self.url = resp['html_url']
53-
return self
53+
return self

0 commit comments

Comments
 (0)