Skip to content

Commit 1f84f39

Browse files
committed
Add autoupdate functionality
1 parent b4b7778 commit 1f84f39

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

bot.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'''
22
MIT License
33
4-
Copyright (c) 2017 Kyb3r
4+
Copyright (c) 2017 kyb3r
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -58,7 +58,7 @@
5858

5959
class ModmailBot(commands.Bot):
6060

61-
mutable_config_keys = ['prefix', 'status', 'guild_id', 'mention']
61+
mutable_config_keys = ['prefix', 'status', 'guild_id', 'mention', 'autoupdates', 'modmail_guild_id']
6262

6363
def __init__(self):
6464
super().__init__(command_prefix=self.get_pre)
@@ -69,6 +69,7 @@ def __init__(self):
6969
self.config = ConfigManager(self)
7070
self.modmail_api = ModmailApiClient(self)
7171
self.data_task = self.loop.create_task(self.data_loop())
72+
self.autoupdate_task = self.loop.create_task(self.autoupdate_loop())
7273
self._add_commands()
7374

7475
def _add_commands(self):
@@ -80,7 +81,7 @@ def _add_commands(self):
8081
'││││ │ │││││├─┤││',
8182
'┴ ┴└─┘─┴┘┴ ┴┴ ┴┴┴─┘', sep='\n')
8283
print(f'v{__version__}')
83-
print('Author: kyb3r' + Style.RESET_ALL)
84+
print('Authors: kyb3r, fourjr' + Style.RESET_ALL)
8485
print(line + Fore.CYAN)
8586

8687

@@ -94,6 +95,7 @@ def _add_commands(self):
9495
async def logout(self):
9596
await self.session.close()
9697
self.data_task.cancel()
98+
self.autoupdate_task.cancel()
9799
await super().logout()
98100

99101
def run(self):
@@ -313,6 +315,41 @@ async def data_loop(self):
313315
await self.session.post('https://api.modmail.tk/metadata', json=data)
314316

315317
await asyncio.sleep(3600)
318+
319+
async def autoupdate_loop(self):
320+
while True:
321+
if not self.config.get('autoupdates'):
322+
await asyncio.sleep(3600)
323+
continue
324+
325+
metadata = await self.modmail_api.get_metadata()
326+
327+
if metadata[version] != self.bot.version:
328+
data = await self.modmail_api.update_repository()
329+
330+
em = discord.Embed(title='Updating bot', color=discord.Color.green())
331+
332+
commit_data = data['data']
333+
user = data['user']
334+
em.set_author(name=user['username'], icon_url=user['avatar_url'], url=user['url'])
335+
em.set_footer(text=f"Updating modmail v{self.version} -> v{metadata['latest_version']}")
336+
337+
if commit_data:
338+
em.description = 'Bot successfully updated, the bot will restart momentarily'
339+
message = commit_data['commit']['message']
340+
html_url = commit_data["html_url"]
341+
short_sha = commit_data['sha'][:6]
342+
em.add_field(name='Merge Commit', value=f"[`{short_sha}`]({html_url}) {message} - {user['username']}")
343+
else:
344+
em.description = 'Already up to date with master repository.'
345+
346+
em.add_field(name='Latest Commit', value=await self.get_latest_updates(limit=1), inline=False)
347+
348+
channel = self.main_category.channels[0]
349+
await channel.send(embed=em)
350+
351+
await asyncio.sleep(3600)
352+
316353

317354
async def get_latest_updates(self, limit=3):
318355
latest_commits = ''

cogs/modmail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ async def setup(self, ctx):
2424

2525
categ = await self.bot.modmail_guild.create_category(
2626
name='Mod Mail',
27-
overwrites=self.bot.overwrites(ctx, modrole=modrole)
27+
overwrites=self.bot.overwrites(ctx)
2828
)
2929

3030
await categ.edit(position=0)
3131

32-
c = await self.bot.modmail_guild.create_text_channel(name='thread-logs', category=categ)
32+
c = await self.bot.modmail_guild.create_text_channel(name='bot-logs', category=categ)
3333
await c.edit(topic='Manually add user id\'s to block users.\n\n'
3434
'Blocked\n-------\n\n')
3535

core/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_user_info(self):
4444
return self.request(self.github + '/userinfo')
4545

4646
def update_repository(self):
47-
return self.request(self.github + '/update-repository')
47+
return self.request(self.github + '/update')
4848

4949
def get_metadata(self):
5050
return self.request(self.base + '/metadata')
@@ -59,7 +59,7 @@ def get_config(self):
5959
return self.request(self.config)
6060

6161
def update_config(self, data):
62-
valid_keys = ['prefix', 'status', 'guild_id', 'mention', 'snippets', 'aliases']
62+
valid_keys = ['prefix', 'status', 'guild_id', 'mention', 'snippets', 'aliases', 'autoupdates', 'modmail_guild_id']
6363
data = {k: v for k, v in data.items() if k in valid_keys}
6464
return self.request(self.config, method='PATCH', payload=data)
6565

0 commit comments

Comments
 (0)