Skip to content

Commit 115c52c

Browse files
committed
Added an update command
1 parent af0342a commit 115c52c

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

bot.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
'''
2424

25-
__version__ = '1.2.1'
25+
__version__ = '1.3.0'
2626

2727
import discord
2828
from discord.ext import commands
@@ -42,6 +42,38 @@
4242
import inspect
4343
from contextlib import redirect_stdout
4444

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+
4577

4678
class Modmail(commands.Bot):
4779

@@ -285,6 +317,37 @@ async def about(self, ctx):
285317
em.set_footer(text=footer)
286318

287319
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)
288351

289352
@commands.command()
290353
@commands.has_permissions(administrator=True)

0 commit comments

Comments
 (0)