1717
1818import discord
1919import isodate
20- from aiohttp import ClientSession
20+ from aiohttp import ClientSession , ClientResponseError
2121from discord .ext import commands , tasks
2222from discord .ext .commands .view import StringView
2323from 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
0 commit comments