13
13
logging .basicConfig (level = logging .INFO , format = '(%(asctime)s) %(message)s' )
14
14
15
15
# Paths and Constants
16
- BDAI_SCRIPT_VERSION = '1.3.0 '
16
+ BDAI_SCRIPT_VERSION = '1.3.1 '
17
17
BDAI_LATEST_RELEASE_PAGE_URL = 'https://github.com/Zwylair/BetterDiscordAutoInstaller/releases/latest'
18
+ BD_LATEST_RELEASE_PAGE_URL = 'https://github.com/rauenzi/BetterDiscordApp/releases/latest'
18
19
BDAI_RAW_RELEASE_URL_TEMPLATE = 'https://github.com/Zwylair/BetterDiscordAutoInstaller/archive/refs/tags/{tag}.zip'
19
20
BDAI_RELEASE_URL_TEMPLATE = 'https://github.com/Zwylair/BetterDiscordAutoInstaller/releases/download/{tag}/BetterDiscordAutoInstaller-{tag}.zip'
20
21
SETTINGS_PATH = 'settings.json'
33
34
LAST_INSTALLED_DISCORD_VERSION : str | None
34
35
DISABLE_DISCORD_VERSION_CHECKING : bool
35
36
DISABLE_BDAI_AUTOUPDATE : bool
37
+ LAST_INSTALLED_BETTERDISCORD_VERSION : str | None
36
38
37
39
def find_discord_path ():
38
40
global DISCORD_PARENT_PATH
@@ -43,22 +45,33 @@ def find_discord_path():
43
45
44
46
def load_settings ():
45
47
global DISCORD_PARENT_PATH , LAST_INSTALLED_DISCORD_VERSION , \
46
- DISABLE_DISCORD_VERSION_CHECKING , DISABLE_BDAI_AUTOUPDATE
48
+ DISABLE_DISCORD_VERSION_CHECKING , DISABLE_BDAI_AUTOUPDATE , LAST_INSTALLED_BETTERDISCORD_VERSION
49
+
50
+ try :
51
+ settings = json .load (open (SETTINGS_PATH )) if os .path .exists (SETTINGS_PATH ) else {}
52
+ except Exception as e :
53
+ logger .error (str (e ))
54
+ sys .exit (1 )
47
55
48
- settings = json .load (open (SETTINGS_PATH )) if os .path .exists (SETTINGS_PATH ) else {}
49
56
DISCORD_PARENT_PATH = settings .get ('discord_installed_path' , None )
50
57
LAST_INSTALLED_DISCORD_VERSION = settings .get ('last_installed_discord_version' , None )
51
58
DISABLE_DISCORD_VERSION_CHECKING = settings .get ('disable_version_check' , False )
52
59
DISABLE_BDAI_AUTOUPDATE = settings .get ('disable_bdai_autoupdate' , False )
60
+ LAST_INSTALLED_BETTERDISCORD_VERSION = settings .get ('last_installed_betterdiscord_version' , None )
53
61
54
62
def dump_settings ():
55
- settings = {
56
- 'discord_installed_path' : DISCORD_PARENT_PATH ,
57
- 'last_installed_discord_version' : LAST_INSTALLED_DISCORD_VERSION ,
58
- 'disable_version_check' : DISABLE_DISCORD_VERSION_CHECKING ,
59
- 'disable_bdai_autoupdate' : DISABLE_BDAI_AUTOUPDATE ,
60
- }
61
- json .dump (settings , open (SETTINGS_PATH , 'w' ))
63
+ try :
64
+ settings = {
65
+ 'discord_installed_path' : DISCORD_PARENT_PATH ,
66
+ 'last_installed_discord_version' : LAST_INSTALLED_DISCORD_VERSION ,
67
+ 'disable_version_check' : DISABLE_DISCORD_VERSION_CHECKING ,
68
+ 'disable_bdai_autoupdate' : DISABLE_BDAI_AUTOUPDATE ,
69
+ 'last_installed_betterdiscord_version' : LAST_INSTALLED_BETTERDISCORD_VERSION ,
70
+ }
71
+ json .dump (settings , open (SETTINGS_PATH , 'w' ), indent = 2 )
72
+ except FileNotFoundError as e :
73
+ logger .error (str (e ))
74
+ sys .exit (1 )
62
75
63
76
def download_plugin (url , save_path ):
64
77
"""Downloads a plugin or library from the specified URL and saves it to the given path."""
@@ -206,6 +219,8 @@ def start_discord(discord_parent_path: str):
206
219
subprocess .Popen (command , shell = True , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
207
220
208
221
def install_betterdiscord (discord_path : str ):
222
+ global LAST_INSTALLED_BETTERDISCORD_VERSION
223
+
209
224
appdata = os .getenv ('appdata' )
210
225
bd_asar_path = os .path .join (appdata , 'BetterDiscord' , 'data' , 'betterdiscord.asar' )
211
226
@@ -237,7 +252,10 @@ def install_betterdiscord(discord_path: str):
237
252
238
253
with open (index_js_path , 'wb' ) as f :
239
254
f .writelines (content )
240
-
255
+
256
+ LAST_INSTALLED_BETTERDISCORD_VERSION = fetch_latest_betterdiscord_release ()
257
+ dump_settings ()
258
+
241
259
logger .info (f"Patched { index_js_path } to include BetterDiscord." )
242
260
243
261
def check_for_updates () -> bool :
@@ -261,3 +279,24 @@ def run_updater():
261
279
262
280
updater_run_command = ['updater.exe' ] if getattr (sys , 'frozen' , False ) else [sys .executable , 'updater.py' ]
263
281
subprocess .run (updater_run_command )
282
+
283
+ def fetch_latest_betterdiscord_release () -> str :
284
+ latest_release_url = requests .head (BD_LATEST_RELEASE_PAGE_URL , allow_redirects = True )
285
+ return latest_release_url .url .split ('/' )[- 1 ]
286
+
287
+ def check_for_betterdiscord_updates () -> bool :
288
+ """Checks for updates and return True if there is an available update, False otherwise"""
289
+ return fetch_latest_betterdiscord_release () != LAST_INSTALLED_BETTERDISCORD_VERSION
290
+
291
+ def is_discord_running () -> bool :
292
+ for process in psutil .process_iter (['name' ]):
293
+ if process .info .get ('name' ) in ['Discord.exe' , 'DiscordPTB.exe' , 'DiscordCanary.exe' ]:
294
+ return True
295
+ return False
296
+
297
+ def is_discord_updating (discord_parent_path : str ) -> bool :
298
+ try :
299
+ with open (os .path .join (discord_parent_path , "Discord_updater_rCURRENT.log" )) as updater_log_file :
300
+ return "Updater main thread exiting" not in updater_log_file .readlines ()[- 1 ]
301
+ except FileNotFoundError :
302
+ return False
0 commit comments