@@ -2021,181 +2021,6 @@ async def on_command_error(
20212021 else :
20222022 logger .error ("Unexpected exception:" , exc_info = exception )
20232023
2024- @tasks .loop (hours = 1 )
2025- async def post_metadata (self ):
2026- info = await self .application_info ()
2027-
2028- delta = discord .utils .utcnow () - self .start_time
2029- data = {
2030- "bot_id" : self .user .id ,
2031- "bot_name" : str (self .user ),
2032- "avatar_url" : self .user .display_avatar .url ,
2033- "guild_id" : self .guild_id ,
2034- "guild_name" : self .guild .name ,
2035- "member_count" : len (self .guild .members ),
2036- "uptime" : delta .total_seconds (),
2037- "latency" : f"{ self .ws .latency * 1000 :.4f} " ,
2038- "version" : str (self .version ),
2039- "selfhosted" : True ,
2040- "last_updated" : str (discord .utils .utcnow ()),
2041- }
2042-
2043- if info .team is not None :
2044- data .update (
2045- {
2046- "owner_name" : info .team .owner .name if info .team .owner is not None else "No Owner" ,
2047- "owner_id" : info .team .owner_id ,
2048- "team" : True ,
2049- }
2050- )
2051- else :
2052- data .update (
2053- {
2054- "owner_name" : info .owner .name ,
2055- "owner_id" : info .owner .id ,
2056- "team" : False ,
2057- }
2058- )
2059-
2060- async with self .session .post ("https://api.modmail.dev/metadata" , json = data ):
2061- logger .debug ("Uploading metadata to Modmail server." )
2062-
2063- @post_metadata .before_loop
2064- async def before_post_metadata (self ):
2065- await self .wait_for_connected ()
2066- if not self .config .get ("data_collection" ) or not self .guild :
2067- self .post_metadata .cancel ()
2068- return
2069-
2070- logger .debug ("Starting metadata loop." )
2071-
2072- @tasks .loop (hours = 1 )
2073- async def autoupdate (self ):
2074- changelog = await Changelog .from_url (self )
2075- latest = changelog .latest_version
2076-
2077- if self .version < Version (latest .version ):
2078- error = None
2079- data = {}
2080- try :
2081- # update fork if gh_token exists
2082- data = await self .api .update_repository ()
2083- except InvalidConfigError :
2084- pass
2085- except ClientResponseError as exc :
2086- error = exc
2087- if self .hosting_method == HostingMethod .HEROKU :
2088- if error is not None :
2089- logger .error (f"Autoupdate failed! Status: { error .status } ." )
2090- logger .error (f"Error message: { error .message } " )
2091- self .autoupdate .cancel ()
2092- return
2093-
2094- commit_data = data .get ("data" )
2095- if not commit_data :
2096- return
2097-
2098- logger .info ("Bot has been updated." )
2099-
2100- if not self .config ["update_notifications" ]:
2101- return
2102-
2103- embed = discord .Embed (color = self .main_color )
2104- message = commit_data ["commit" ]["message" ]
2105- html_url = commit_data ["html_url" ]
2106- short_sha = commit_data ["sha" ][:6 ]
2107- user = data ["user" ]
2108- embed .add_field (
2109- name = "Merge Commit" ,
2110- value = f"[`{ short_sha } `]({ html_url } ) { message } - { user ['username' ]} " ,
2111- )
2112- embed .set_author (
2113- name = user ["username" ] + " - Updating Bot" ,
2114- icon_url = user ["avatar_url" ],
2115- url = user ["url" ],
2116- )
2117-
2118- embed .set_footer (text = f"Updating Modmail v{ self .version } -> v{ latest .version } " )
2119-
2120- embed .description = latest .description
2121- for name , value in latest .fields .items ():
2122- embed .add_field (name = name , value = value )
2123-
2124- channel = self .update_channel
2125- await channel .send (embed = embed )
2126- else :
2127- command = "git pull"
2128- proc = await asyncio .create_subprocess_shell (
2129- command ,
2130- stderr = PIPE ,
2131- stdout = PIPE ,
2132- )
2133- err = await proc .stderr .read ()
2134- err = err .decode ("utf-8" ).rstrip ()
2135- res = await proc .stdout .read ()
2136- res = res .decode ("utf-8" ).rstrip ()
2137-
2138- if err and not res :
2139- logger .warning (f"Autoupdate failed: { err } " )
2140- self .autoupdate .cancel ()
2141- return
2142-
2143- elif res != "Already up to date." :
2144- if os .getenv ("PIPENV_ACTIVE" ):
2145- # Update pipenv if possible
2146- await asyncio .create_subprocess_shell (
2147- "pipenv sync" ,
2148- stderr = PIPE ,
2149- stdout = PIPE ,
2150- )
2151- message = ""
2152- else :
2153- message = "\n \n Do manually update dependencies if your bot has crashed."
2154-
2155- logger .info ("Bot has been updated." )
2156- channel = self .update_channel
2157- if self .hosting_method in (
2158- HostingMethod .PM2 ,
2159- HostingMethod .SYSTEMD ,
2160- ):
2161- embed = discord .Embed (title = "Bot has been updated" , color = self .main_color )
2162- embed .set_footer (
2163- text = f"Updating Modmail v{ self .version } " f"-> v{ latest .version } { message } "
2164- )
2165- if self .config ["update_notifications" ]:
2166- await channel .send (embed = embed )
2167- else :
2168- embed = discord .Embed (
2169- title = "Bot has been updated and is logging out." ,
2170- description = f"If you do not have an auto-restart setup, please manually start the bot. { message } " ,
2171- color = self .main_color ,
2172- )
2173- embed .set_footer (text = f"Updating Modmail v{ self .version } -> v{ latest .version } " )
2174- if self .config ["update_notifications" ]:
2175- await channel .send (embed = embed )
2176- return await self .close ()
2177-
2178- @autoupdate .before_loop
2179- async def before_autoupdate (self ):
2180- await self .wait_for_connected ()
2181- logger .debug ("Starting autoupdate loop" )
2182-
2183- if self .config .get ("disable_autoupdates" ):
2184- logger .warning ("Autoupdates disabled." )
2185- self .autoupdate .cancel ()
2186- return
2187-
2188- if self .hosting_method == HostingMethod .DOCKER :
2189- logger .warning ("Autoupdates disabled as using Docker." )
2190- self .autoupdate .cancel ()
2191- return
2192-
2193- if not self .config .get ("github_token" ) and self .hosting_method == HostingMethod .HEROKU :
2194- logger .warning ("GitHub access token not found." )
2195- logger .warning ("Autoupdates disabled." )
2196- self .autoupdate .cancel ()
2197- return
2198-
21992024 @tasks .loop (hours = 1 , reconnect = False )
22002025 async def log_expiry (self ):
22012026 log_expire_after = self .config .get ("log_expiration" )
0 commit comments