Skip to content

Commit a05c5e1

Browse files
authored
Revert "Remove update functionality from the bot. (#293)" (#294)
This reverts commit 4222da3.
1 parent 4222da3 commit a05c5e1

File tree

4 files changed

+158
-8
lines changed

4 files changed

+158
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
# [Unreleased] (3.0.0)
7+
# [Unreleased]
88

99
### Added
1010

1111
- Sponsors command that will list sponsors.
1212

13-
### Removed
14-
15-
- Removed autoupdate functionality and the `update` command in favour of the [Pull app](https://github.com/apps/pull).
16-
17-
Read more about updating your bot [here](https://github.com/kyb3r/modmail/wiki/updating)
18-
1913
### Changed
2014
- Channel names now can contain unicode characters.
2115
- Debug logs are now located in a unique file for each bot. (Internal change)

bot.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(self):
7474
self.plugin_db = PluginDatabaseClient(self)
7575

7676
self.metadata_task = self.loop.create_task(self.metadata_loop())
77+
self.autoupdate_task = self.loop.create_task(self.autoupdate_loop())
7778
self._load_extensions()
7879

7980
@property
@@ -191,6 +192,11 @@ def run(self, *args, **kwargs):
191192
self.loop.run_until_complete(self.metadata_task)
192193
except asyncio.CancelledError:
193194
logger.debug(info("data_task has been cancelled."))
195+
try:
196+
self.autoupdate_task.cancel()
197+
self.loop.run_until_complete(self.autoupdate_task)
198+
except asyncio.CancelledError:
199+
logger.debug(info("autoupdate_task has been cancelled."))
194200

195201
self.loop.run_until_complete(self.logout())
196202
for task in asyncio.Task.all_tasks():
@@ -898,6 +904,62 @@ async def validate_database_connection(self):
898904
else:
899905
logger.info(info("Successfully connected to the database."))
900906

907+
async def autoupdate_loop(self):
908+
await self.wait_until_ready()
909+
910+
if self.config.get("disable_autoupdates"):
911+
logger.warning(info("Autoupdates disabled."))
912+
logger.info(LINE)
913+
return
914+
915+
if not self.config.get("github_access_token"):
916+
logger.warning(info("GitHub access token not found."))
917+
logger.warning(info("Autoupdates disabled."))
918+
logger.info(LINE)
919+
return
920+
921+
logger.info(info("Autoupdate loop started."))
922+
923+
while not self.is_closed():
924+
changelog = await Changelog.from_url(self)
925+
latest = changelog.latest_version
926+
927+
if parse_version(self.version) < parse_version(latest.version):
928+
data = await self.api.update_repository()
929+
930+
embed = discord.Embed(color=discord.Color.green())
931+
932+
commit_data = data["data"]
933+
user = data["user"]
934+
embed.set_author(
935+
name=user["username"] + " - Updating Bot",
936+
icon_url=user["avatar_url"],
937+
url=user["url"],
938+
)
939+
940+
embed.set_footer(
941+
text=f"Updating Modmail v{self.version} " f"-> v{latest.version}"
942+
)
943+
944+
embed.description = latest.description
945+
for name, value in latest.fields.items():
946+
embed.add_field(name=name, value=value)
947+
948+
if commit_data:
949+
message = commit_data["commit"]["message"]
950+
html_url = commit_data["html_url"]
951+
short_sha = commit_data["sha"][:6]
952+
embed.add_field(
953+
name="Merge Commit",
954+
value=f"[`{short_sha}`]({html_url}) "
955+
f"{message} - {user['username']}",
956+
)
957+
logger.info(info("Bot has been updated."))
958+
channel = self.log_channel
959+
await channel.send(embed=embed)
960+
961+
await asyncio.sleep(3600)
962+
901963
async def metadata_loop(self):
902964
await self.wait_until_ready()
903965
if not self.guild:

cogs/utility.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,83 @@ async def github(self, ctx):
423423
embed.set_thumbnail(url=user["avatar_url"])
424424
await ctx.send(embed=embed)
425425

426+
@commands.command()
427+
@checks.has_permissions(PermissionLevel.OWNER)
428+
@github_access_token_required
429+
@trigger_typing
430+
async def update(self, ctx, *, flag: str = ""):
431+
"""
432+
Update Modmail.
433+
434+
This only works for Heroku users who have configured their bot for updates.
435+
436+
To stay up-to-date with the latest commit
437+
from GitHub, specify "force" as the flag.
438+
"""
439+
440+
changelog = await Changelog.from_url(self.bot)
441+
latest = changelog.latest_version
442+
443+
desc = (
444+
f"The latest version is [`{self.bot.version}`]"
445+
"(https://github.com/kyb3r/modmail/blob/master/bot.py#L25)"
446+
)
447+
448+
if (
449+
parse_version(self.bot.version) >= parse_version(latest.version)
450+
and flag.lower() != "force"
451+
):
452+
embed = Embed(
453+
title="Already up to date", description=desc, color=self.bot.main_color
454+
)
455+
456+
data = await self.bot.api.get_user_info()
457+
if not data.get("error"):
458+
user = data["user"]
459+
embed.set_author(
460+
name=user["username"], icon_url=user["avatar_url"], url=user["url"]
461+
)
462+
else:
463+
data = await self.bot.api.update_repository()
464+
465+
commit_data = data["data"]
466+
user = data["user"]
467+
468+
if commit_data:
469+
embed = Embed(color=self.bot.main_color)
470+
471+
embed.set_footer(
472+
text=f"Updating Modmail v{self.bot.version} "
473+
f"-> v{latest.version}"
474+
)
475+
476+
embed.set_author(
477+
name=user["username"] + " - Updating bot",
478+
icon_url=user["avatar_url"],
479+
url=user["url"],
480+
)
481+
482+
embed.description = latest.description
483+
for name, value in latest.fields.items():
484+
embed.add_field(name=name, value=value)
485+
# message = commit_data['commit']['message']
486+
html_url = commit_data["html_url"]
487+
short_sha = commit_data["sha"][:6]
488+
embed.add_field(
489+
name="Merge Commit", value=f"[`{short_sha}`]({html_url})"
490+
)
491+
else:
492+
embed = Embed(
493+
title="Already up to date with master repository.",
494+
description="No further updates required",
495+
color=self.bot.main_color,
496+
)
497+
embed.set_author(
498+
name=user["username"], icon_url=user["avatar_url"], url=user["url"]
499+
)
500+
501+
return await ctx.send(embed=embed)
502+
426503
@commands.command(aliases=["presence"])
427504
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
428505
async def activity(self, ctx, activity_type: str.lower, *, message: str = ""):

core/decorators.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,21 @@ async def wrapper(self, ctx: commands.Context, *args, **kwargs):
1010
await ctx.trigger_typing()
1111
return await func(self, ctx, *args, **kwargs)
1212

13-
return wrapper
13+
return wrapper
14+
15+
16+
def github_access_token_required(func):
17+
@functools.wraps(func)
18+
async def wrapper(self, ctx: commands.Context, *args, **kwargs):
19+
if self.bot.config.get("github_access_token"):
20+
return await func(self, ctx, *args, **kwargs)
21+
22+
desc = (
23+
"You can only use this command if you have a "
24+
"configured `GITHUB_ACCESS_TOKEN`. Get a "
25+
"personal access token from developer settings."
26+
)
27+
embed = Embed(color=Color.red(), title="Unauthorized", description=desc)
28+
await ctx.send(embed=embed)
29+
30+
return wrapper

0 commit comments

Comments
 (0)