Skip to content

Commit 429b7c9

Browse files
authored
Reworked script workflow (#25 #23)
2 parents 177a310 + 95332aa commit 429b7c9

File tree

2 files changed

+100
-48
lines changed

2 files changed

+100
-48
lines changed

funcs.py

+50-11
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
logging.basicConfig(level=logging.INFO, format='(%(asctime)s) %(message)s')
1414

1515
# Paths and Constants
16-
BDAI_SCRIPT_VERSION = '1.3.0'
16+
BDAI_SCRIPT_VERSION = '1.3.1'
1717
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'
1819
BDAI_RAW_RELEASE_URL_TEMPLATE = 'https://github.com/Zwylair/BetterDiscordAutoInstaller/archive/refs/tags/{tag}.zip'
1920
BDAI_RELEASE_URL_TEMPLATE = 'https://github.com/Zwylair/BetterDiscordAutoInstaller/releases/download/{tag}/BetterDiscordAutoInstaller-{tag}.zip'
2021
SETTINGS_PATH = 'settings.json'
@@ -33,6 +34,7 @@
3334
LAST_INSTALLED_DISCORD_VERSION: str | None
3435
DISABLE_DISCORD_VERSION_CHECKING: bool
3536
DISABLE_BDAI_AUTOUPDATE: bool
37+
LAST_INSTALLED_BETTERDISCORD_VERSION: str | None
3638

3739
def find_discord_path():
3840
global DISCORD_PARENT_PATH
@@ -43,22 +45,33 @@ def find_discord_path():
4345

4446
def load_settings():
4547
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)
4755

48-
settings = json.load(open(SETTINGS_PATH)) if os.path.exists(SETTINGS_PATH) else {}
4956
DISCORD_PARENT_PATH = settings.get('discord_installed_path', None)
5057
LAST_INSTALLED_DISCORD_VERSION = settings.get('last_installed_discord_version', None)
5158
DISABLE_DISCORD_VERSION_CHECKING = settings.get('disable_version_check', False)
5259
DISABLE_BDAI_AUTOUPDATE = settings.get('disable_bdai_autoupdate', False)
60+
LAST_INSTALLED_BETTERDISCORD_VERSION = settings.get('last_installed_betterdiscord_version', None)
5361

5462
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)
6275

6376
def download_plugin(url, save_path):
6477
"""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):
206219
subprocess.Popen(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
207220

208221
def install_betterdiscord(discord_path: str):
222+
global LAST_INSTALLED_BETTERDISCORD_VERSION
223+
209224
appdata = os.getenv('appdata')
210225
bd_asar_path = os.path.join(appdata, 'BetterDiscord', 'data', 'betterdiscord.asar')
211226

@@ -237,7 +252,10 @@ def install_betterdiscord(discord_path: str):
237252

238253
with open(index_js_path, 'wb') as f:
239254
f.writelines(content)
240-
255+
256+
LAST_INSTALLED_BETTERDISCORD_VERSION = fetch_latest_betterdiscord_release()
257+
dump_settings()
258+
241259
logger.info(f"Patched {index_js_path} to include BetterDiscord.")
242260

243261
def check_for_updates() -> bool:
@@ -261,3 +279,24 @@ def run_updater():
261279

262280
updater_run_command = ['updater.exe'] if getattr(sys, 'frozen', False) else [sys.executable, 'updater.py']
263281
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

main.py

+50-37
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,75 @@
11
import time
2-
from funcs import * # Import functions from funcs.py
2+
from funcs import * # Import functions
33
import funcs # For editing the variables
44

55
def main():
6-
try:
7-
load_settings()
8-
except Exception as e:
9-
logger.error(str(e))
10-
sys.exit(1)
6+
load_settings()
117

128
logger.info(f'BetterDiscordAutoInstaller v{BDAI_SCRIPT_VERSION}')
139

10+
logger.info('Checking for BetterDiscordAutoInstaller updates...')
1411
if check_for_updates() and not funcs.DISABLE_BDAI_AUTOUPDATE:
1512
run_updater()
1613

14+
logger.info("Retrieving Discord installation folder...")
1715
funcs.DISCORD_PARENT_PATH = find_discord_path()
1816
if not funcs.DISCORD_PARENT_PATH:
1917
logger.error("No valid Discord installation found.")
18+
2019
funcs.DISCORD_PARENT_PATH = input("Enter the path to your Discord installation: ").strip()
2120
if not os.path.exists(funcs.DISCORD_PARENT_PATH):
2221
logger.error(f"Invalid path provided: {funcs.DISCORD_PARENT_PATH}")
2322
sys.exit(1)
2423

25-
try:
26-
discord_core_folder = get_latest_installed_discord_folder_name(funcs.DISCORD_PARENT_PATH)
27-
discord_path = os.path.join(funcs.DISCORD_PARENT_PATH, discord_core_folder)
24+
if not is_discord_running():
25+
logger.info("Discord is not running. Starting updater.")
26+
start_discord(funcs.DISCORD_PARENT_PATH)
27+
time.sleep(1)
28+
29+
logger.info("Waiting for end of Discord updating.")
30+
while is_discord_updating(funcs.DISCORD_PARENT_PATH):
31+
time.sleep(0.5)
2832

29-
if discord_core_folder == funcs.LAST_INSTALLED_DISCORD_VERSION and not funcs.DISABLE_DISCORD_VERSION_CHECKING:
30-
logger.info("Discord is already up to date. No update needed. Exiting in 3 seconds...")
31-
time.sleep(3)
32-
sys.exit(0)
33+
discord_core_folder = get_latest_installed_discord_folder_name(funcs.DISCORD_PARENT_PATH)
34+
discord_path = os.path.join(funcs.DISCORD_PARENT_PATH, discord_core_folder)
35+
is_discord_up_to_date = discord_core_folder == funcs.LAST_INSTALLED_DISCORD_VERSION and not funcs.DISABLE_DISCORD_VERSION_CHECKING
3336

37+
if is_discord_up_to_date:
38+
logger.info("Discord is up to date.")
39+
else:
3440
funcs.LAST_INSTALLED_DISCORD_VERSION = discord_core_folder
3541
dump_settings()
36-
except FileNotFoundError as e:
37-
logger.error(str(e))
38-
sys.exit(1)
39-
40-
logger.info("Killing any running Discord processes...")
41-
kill_discord()
42-
time.sleep(2)
43-
44-
logger.info("Installing BetterDiscord...")
45-
install_betterdiscord(discord_path)
46-
47-
PLUGIN_URLS = [
48-
'https://raw.githubusercontent.com/riolubruh/YABDP4Nitro/main/YABDP4Nitro.plugin.js',
49-
'https://mwittrien.github.io/BetterDiscordAddons/Plugins/PluginRepo/PluginRepo.plugin.js'
50-
]
51-
PLUGIN_SAVE_PATHS = [
52-
os.path.join(APPDATA, 'BetterDiscord/plugins/YABDP4Nitro.plugin.js'),
53-
os.path.join(APPDATA, 'BetterDiscord/plugins/PluginRepo.plugin.js')
54-
]
55-
56-
install_plugins(PLUGIN_URLS, PLUGIN_SAVE_PATHS, APPDATA)
57-
58-
logger.info("Restarting Discord...")
59-
start_discord(funcs.DISCORD_PARENT_PATH)
42+
43+
is_betterdiscord_up_to_date = not check_for_betterdiscord_updates()
44+
if not is_discord_up_to_date or not is_betterdiscord_up_to_date:
45+
if not is_discord_up_to_date:
46+
logger.info("Discord was updated.")
47+
48+
if not is_betterdiscord_up_to_date:
49+
logger.info("Found an available update for BetterDiscord.")
50+
51+
logger.info("Killing any running Discord processes...")
52+
kill_discord()
53+
time.sleep(2)
54+
55+
logger.info("Installing BetterDiscord...")
56+
install_betterdiscord(discord_path)
57+
58+
PLUGIN_URLS = [
59+
'https://raw.githubusercontent.com/riolubruh/YABDP4Nitro/main/YABDP4Nitro.plugin.js',
60+
'https://mwittrien.github.io/BetterDiscordAddons/Plugins/PluginRepo/PluginRepo.plugin.js'
61+
]
62+
PLUGIN_SAVE_PATHS = [
63+
os.path.join(APPDATA, 'BetterDiscord/plugins/YABDP4Nitro.plugin.js'),
64+
os.path.join(APPDATA, 'BetterDiscord/plugins/PluginRepo.plugin.js')
65+
]
66+
67+
install_plugins(PLUGIN_URLS, PLUGIN_SAVE_PATHS, APPDATA)
68+
69+
logger.info("Restarting Discord...")
70+
start_discord(funcs.DISCORD_PARENT_PATH)
71+
else:
72+
logger.info("BetterDiscord is up to date.")
6073

6174
logger.info("Installation complete. Exiting in 3 seconds...")
6275
time.sleep(3)

0 commit comments

Comments
 (0)