|
22 | 22 | SOFTWARE.
|
23 | 23 | '''
|
24 | 24 |
|
25 |
| -__version__ = '1.2.1' |
| 25 | +__version__ = '1.3.0' |
26 | 26 |
|
27 | 27 | import discord
|
28 | 28 | from discord.ext import commands
|
|
42 | 42 | import inspect
|
43 | 43 | from contextlib import redirect_stdout
|
44 | 44 |
|
| 45 | +class Github: |
| 46 | + head = 'https://api.github.com/repos/kyb3r/modmail/git/refs/heads/master' |
| 47 | + merge_url = 'https://api.github.com/repos/{username}/modmail/merges' |
| 48 | + |
| 49 | + def __init__(self, bot, access_token, username=None): |
| 50 | + self.bot = bot |
| 51 | + self.session = bot.session |
| 52 | + self.access_token = access_token |
| 53 | + self.username = username |
| 54 | + self.headers = {'Authorization': 'Bearer '+access_token} |
| 55 | + |
| 56 | + async def update_repository(self): |
| 57 | + sha = (await self.request(self.head))['object']['sha'] |
| 58 | + payload = { |
| 59 | + 'base': 'master', |
| 60 | + 'head': sha, |
| 61 | + 'commit_message': 'Updating bot' |
| 62 | + } |
| 63 | + merge_url = self.merge_url.format(username=self.username) |
| 64 | + |
| 65 | + await self.request(merge_url, method='POST', payload=payload) |
| 66 | + |
| 67 | + async def request(self, url, method='GET', payload=None): |
| 68 | + async with self.session.request(method, url, headers=self.headers, json=payload) as resp: |
| 69 | + return await resp.json() |
| 70 | + |
| 71 | + @classmethod |
| 72 | + async def login(cls, bot, access_token): |
| 73 | + self = cls(bot, access_token) |
| 74 | + self.username = (await self.request('https://api.github.com/user'))['login'] |
| 75 | + return self |
| 76 | + |
45 | 77 |
|
46 | 78 | class Modmail(commands.Bot):
|
47 | 79 |
|
@@ -285,6 +317,37 @@ async def about(self, ctx):
|
285 | 317 | em.set_footer(text=footer)
|
286 | 318 |
|
287 | 319 | await ctx.send(embed=em)
|
| 320 | + |
| 321 | + |
| 322 | + @commands.command() |
| 323 | + @commands.has_permissions(administrator=True) |
| 324 | + async def update(self, ctx): |
| 325 | + await ctx.trigger_typing() |
| 326 | + |
| 327 | + async with self.session.get('https://api.kybr.tk/modmail') as resp: |
| 328 | + data = await resp.json() |
| 329 | + |
| 330 | + em = discord.Embed( |
| 331 | + title='Cannot update', |
| 332 | + description=f'The bot is already up to date v`{__version__}`', |
| 333 | + color=discord.Color.green() |
| 334 | + ) |
| 335 | + |
| 336 | + if data['latest_version'] == __version__: |
| 337 | + return await ctx.send(embed=em) |
| 338 | + |
| 339 | + access_token = self.config.get('GITHUB_ACCESS_TOKEN') |
| 340 | + |
| 341 | + if not access_token: |
| 342 | + em.description = 'You have not properly set up github credentials.' |
| 343 | + em.color = discord.Color.red() |
| 344 | + return await ctx.send(embed=em) |
| 345 | + |
| 346 | + user = await Github.login(self, access_token) |
| 347 | + await user.update_repository() |
| 348 | + em.title = 'Success' |
| 349 | + em.description = 'Bot successfully updated, the bot will restart momentarily' |
| 350 | + await ctx.send(embed=em) |
288 | 351 |
|
289 | 352 | @commands.command()
|
290 | 353 | @commands.has_permissions(administrator=True)
|
|
0 commit comments