Skip to content

Commit b446861

Browse files
committed
Merge branch 'autoupdate' of https://github.com/Jerrie-Aries/modmail into Jerrie-Aries-autoupdate
2 parents 6e5099e + 23221a2 commit b446861

File tree

3 files changed

+166
-67
lines changed

3 files changed

+166
-67
lines changed

bot.py

+45-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import discord
1919
import isodate
20-
from aiohttp import ClientSession
20+
from aiohttp import ClientSession, ClientResponseError
2121
from discord.ext import commands, tasks
2222
from discord.ext.commands.view import StringView
2323
from emoji import UNICODE_EMOJI
@@ -631,6 +631,8 @@ async def on_ready(self):
631631
)
632632
logger.warning("If the external servers are valid, you may ignore this message.")
633633

634+
self.post_metadata.start()
635+
self.autoupdate.start()
634636
self._started = True
635637

636638
async def convert_emoji(self, name: str) -> str:
@@ -1582,6 +1584,7 @@ async def before_post_metadata(self):
15821584
await self.wait_for_connected()
15831585
if not self.config.get("data_collection") or not self.guild:
15841586
self.post_metadata.cancel()
1587+
return
15851588

15861589
logger.debug("Starting metadata loop.")
15871590
logger.line("debug")
@@ -1592,44 +1595,55 @@ async def autoupdate(self):
15921595
latest = changelog.latest_version
15931596

15941597
if self.version < parse_version(latest.version):
1595-
if self.hosting_method == HostingMethod.HEROKU:
1598+
error = None
1599+
data = {}
1600+
try:
1601+
# update fork if gh_token exists
15961602
data = await self.api.update_repository()
1603+
except InvalidConfigError:
1604+
pass
1605+
except ClientResponseError as exc:
1606+
error = exc
1607+
if self.hosting_method == HostingMethod.HEROKU:
1608+
if error is not None:
1609+
logger.error(f"Autoupdate failed! Status: {error.status}.")
1610+
logger.error(f"Error message: {error.message}")
1611+
self.autoupdate.cancel()
1612+
return
15971613

1598-
embed = discord.Embed(color=self.main_color)
1614+
commit_data = data.get("data")
1615+
if not commit_data:
1616+
return
1617+
1618+
logger.info("Bot has been updated.")
1619+
1620+
if not self.config["update_notifications"]:
1621+
return
15991622

1600-
commit_data = data["data"]
1623+
embed = discord.Embed(color=self.main_color)
1624+
message = commit_data["commit"]["message"]
1625+
html_url = commit_data["html_url"]
1626+
short_sha = commit_data["sha"][:6]
16011627
user = data["user"]
1628+
embed.add_field(
1629+
name="Merge Commit",
1630+
value=f"[`{short_sha}`]({html_url}) " f"{message} - {user['username']}",
1631+
)
16021632
embed.set_author(
16031633
name=user["username"] + " - Updating Bot",
16041634
icon_url=user["avatar_url"],
16051635
url=user["url"],
16061636
)
16071637

1608-
embed.set_footer(text=f"Updating Modmail v{self.version} " f"-> v{latest.version}")
1638+
embed.set_footer(text=f"Updating Modmail v{self.version} -> v{latest.version}")
16091639

16101640
embed.description = latest.description
16111641
for name, value in latest.fields.items():
16121642
embed.add_field(name=name, value=value)
16131643

1614-
if commit_data:
1615-
message = commit_data["commit"]["message"]
1616-
html_url = commit_data["html_url"]
1617-
short_sha = commit_data["sha"][:6]
1618-
embed.add_field(
1619-
name="Merge Commit",
1620-
value=f"[`{short_sha}`]({html_url}) " f"{message} - {user['username']}",
1621-
)
1622-
logger.info("Bot has been updated.")
1623-
channel = self.log_channel
1624-
if self.config["update_notifications"]:
1625-
await channel.send(embed=embed)
1644+
channel = self.update_channel
1645+
await channel.send(embed=embed)
16261646
else:
1627-
try:
1628-
# update fork if gh_token exists
1629-
await self.api.update_repository()
1630-
except InvalidConfigError:
1631-
pass
1632-
16331647
command = "git pull"
16341648
proc = await asyncio.create_subprocess_shell(
16351649
command,
@@ -1643,7 +1657,7 @@ async def autoupdate(self):
16431657

16441658
if err and not res:
16451659
logger.warning(f"Autoupdate failed: {err}")
1646-
self.autoupdate_loop.cancel()
1660+
self.autoupdate.cancel()
16471661
return
16481662

16491663
elif res != "Already up to date.":
@@ -1660,7 +1674,7 @@ async def autoupdate(self):
16601674
description="If you do not have an auto-restart setup, please manually start the bot.",
16611675
color=self.main_color,
16621676
)
1663-
embed.set_footer(text=f"Updating Modmail v{self.version} " f"-> v{latest.version}")
1677+
embed.set_footer(text=f"Updating Modmail v{self.version} -> v{latest.version}")
16641678
if self.config["update_notifications"]:
16651679
await channel.send(embed=embed)
16661680
return await self.close()
@@ -1672,16 +1686,19 @@ async def before_autoupdate(self):
16721686

16731687
if self.config.get("disable_autoupdates"):
16741688
logger.warning("Autoupdates disabled.")
1675-
self.autoupdate_loop.cancel()
1689+
self.autoupdate.cancel()
1690+
return
16761691

16771692
if self.hosting_method == HostingMethod.DOCKER:
16781693
logger.warning("Autoupdates disabled as using Docker.")
1679-
self.autoupdate_loop.cancel()
1694+
self.autoupdate.cancel()
1695+
return
16801696

16811697
if not self.config.get("github_token") and self.hosting_method == HostingMethod.HEROKU:
16821698
logger.warning("GitHub access token not found.")
16831699
logger.warning("Autoupdates disabled.")
1684-
self.autoupdate_loop.cancel()
1700+
self.autoupdate.cancel()
1701+
return
16851702

16861703
def format_channel_name(self, author, exclude_channel=None, force_null=False):
16871704
"""Sanitises a username for use with text channel names

cogs/utility.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -1931,15 +1931,15 @@ async def github(self, ctx):
19311931
async def update(self, ctx, *, flag: str = ""):
19321932
"""
19331933
Update Modmail.
1934-
To stay up-to-date with the latest commit rom GitHub, specify "force" as the flag.
1934+
To stay up-to-date with the latest commit from GitHub, specify "force" as the flag.
19351935
"""
19361936

19371937
changelog = await Changelog.from_url(self.bot)
19381938
latest = changelog.latest_version
19391939

19401940
desc = (
19411941
f"The latest version is [`{self.bot.version}`]"
1942-
"(https://github.com/kyb3r/modmail/blob/master/bot.py#L25)"
1942+
"(https://github.com/kyb3r/modmail/blob/master/bot.py#L1)"
19431943
)
19441944

19451945
if self.bot.version >= parse_version(latest.version) and flag.lower() != "force":
@@ -1951,16 +1951,39 @@ async def update(self, ctx, *, flag: str = ""):
19511951
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
19521952
await ctx.send(embed=embed)
19531953
else:
1954-
if self.bot.hosting_method == HostingMethod.HEROKU:
1954+
error = None
1955+
data = {}
1956+
try:
1957+
# update fork if gh_token exists
19551958
data = await self.bot.api.update_repository()
1959+
except InvalidConfigError:
1960+
pass
1961+
except ClientResponseError as exc:
1962+
error = exc
1963+
1964+
if self.bot.hosting_method == HostingMethod.HEROKU:
1965+
if error is not None:
1966+
embed = discord.Embed(
1967+
title="Update failed",
1968+
description=f"Error status: {error.status}.\nError message: {error.message}",
1969+
color=self.bot.error_color,
1970+
)
1971+
return await ctx.send(embed=embed)
1972+
if not data:
1973+
# invalid gh_token
1974+
embed = discord.Embed(
1975+
title="Update failed",
1976+
description="Invalid Github token.",
1977+
color=self.bot.error_color,
1978+
)
1979+
return await ctx.send(embed=embed)
19561980

19571981
commit_data = data["data"]
19581982
user = data["user"]
1959-
19601983
if commit_data and commit_data.get("html_url"):
19611984
embed = discord.Embed(color=self.bot.main_color)
19621985

1963-
embed.set_footer(text=f"Updating Modmail v{self.bot.version} " f"-> v{latest.version}")
1986+
embed.set_footer(text=f"Updating Modmail v{self.bot.version} -> v{latest.version}")
19641987

19651988
embed.set_author(
19661989
name=user["username"] + " - Updating bot",
@@ -1978,21 +2001,14 @@ async def update(self, ctx, *, flag: str = ""):
19782001
else:
19792002
embed = discord.Embed(
19802003
title="Already up to date",
1981-
description="No further updates required",
2004+
description="No further updates required.",
19822005
color=self.bot.main_color,
19832006
)
19842007
embed.set_footer(text="Force update")
19852008
embed.set_author(name=user["username"], icon_url=user["avatar_url"], url=user["url"])
19862009
await ctx.send(embed=embed)
19872010
else:
1988-
# update fork if gh_token exists
1989-
try:
1990-
await self.bot.api.update_repository()
1991-
except InvalidConfigError:
1992-
pass
1993-
19942011
command = "git pull"
1995-
19962012
proc = await asyncio.create_subprocess_shell(
19972013
command,
19982014
stderr=PIPE,

0 commit comments

Comments
 (0)