17
17
18
18
import discord
19
19
import isodate
20
- from aiohttp import ClientSession
20
+ from aiohttp import ClientSession , ClientResponseError
21
21
from discord .ext import commands , tasks
22
22
from discord .ext .commands .view import StringView
23
23
from emoji import UNICODE_EMOJI
@@ -631,6 +631,8 @@ async def on_ready(self):
631
631
)
632
632
logger .warning ("If the external servers are valid, you may ignore this message." )
633
633
634
+ self .post_metadata .start ()
635
+ self .autoupdate .start ()
634
636
self ._started = True
635
637
636
638
async def convert_emoji (self , name : str ) -> str :
@@ -1582,6 +1584,7 @@ async def before_post_metadata(self):
1582
1584
await self .wait_for_connected ()
1583
1585
if not self .config .get ("data_collection" ) or not self .guild :
1584
1586
self .post_metadata .cancel ()
1587
+ return
1585
1588
1586
1589
logger .debug ("Starting metadata loop." )
1587
1590
logger .line ("debug" )
@@ -1592,44 +1595,55 @@ async def autoupdate(self):
1592
1595
latest = changelog .latest_version
1593
1596
1594
1597
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
1596
1602
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
1597
1613
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
1599
1622
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 ]
1601
1627
user = data ["user" ]
1628
+ embed .add_field (
1629
+ name = "Merge Commit" ,
1630
+ value = f"[`{ short_sha } `]({ html_url } ) " f"{ message } - { user ['username' ]} " ,
1631
+ )
1602
1632
embed .set_author (
1603
1633
name = user ["username" ] + " - Updating Bot" ,
1604
1634
icon_url = user ["avatar_url" ],
1605
1635
url = user ["url" ],
1606
1636
)
1607
1637
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 } " )
1609
1639
1610
1640
embed .description = latest .description
1611
1641
for name , value in latest .fields .items ():
1612
1642
embed .add_field (name = name , value = value )
1613
1643
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 )
1626
1646
else :
1627
- try :
1628
- # update fork if gh_token exists
1629
- await self .api .update_repository ()
1630
- except InvalidConfigError :
1631
- pass
1632
-
1633
1647
command = "git pull"
1634
1648
proc = await asyncio .create_subprocess_shell (
1635
1649
command ,
@@ -1643,7 +1657,7 @@ async def autoupdate(self):
1643
1657
1644
1658
if err and not res :
1645
1659
logger .warning (f"Autoupdate failed: { err } " )
1646
- self .autoupdate_loop .cancel ()
1660
+ self .autoupdate .cancel ()
1647
1661
return
1648
1662
1649
1663
elif res != "Already up to date." :
@@ -1660,7 +1674,7 @@ async def autoupdate(self):
1660
1674
description = "If you do not have an auto-restart setup, please manually start the bot." ,
1661
1675
color = self .main_color ,
1662
1676
)
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 } " )
1664
1678
if self .config ["update_notifications" ]:
1665
1679
await channel .send (embed = embed )
1666
1680
return await self .close ()
@@ -1672,16 +1686,19 @@ async def before_autoupdate(self):
1672
1686
1673
1687
if self .config .get ("disable_autoupdates" ):
1674
1688
logger .warning ("Autoupdates disabled." )
1675
- self .autoupdate_loop .cancel ()
1689
+ self .autoupdate .cancel ()
1690
+ return
1676
1691
1677
1692
if self .hosting_method == HostingMethod .DOCKER :
1678
1693
logger .warning ("Autoupdates disabled as using Docker." )
1679
- self .autoupdate_loop .cancel ()
1694
+ self .autoupdate .cancel ()
1695
+ return
1680
1696
1681
1697
if not self .config .get ("github_token" ) and self .hosting_method == HostingMethod .HEROKU :
1682
1698
logger .warning ("GitHub access token not found." )
1683
1699
logger .warning ("Autoupdates disabled." )
1684
- self .autoupdate_loop .cancel ()
1700
+ self .autoupdate .cancel ()
1701
+ return
1685
1702
1686
1703
def format_channel_name (self , author , exclude_channel = None , force_null = False ):
1687
1704
"""Sanitises a username for use with text channel names
0 commit comments