|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import psutil |
| 4 | + |
| 5 | + |
| 6 | +def start_discord(discord_parent_path: str): |
| 7 | + # running discord from c:/ for prevent locking the script's working dir |
| 8 | + script_working_dir = os.getcwd() |
| 9 | + |
| 10 | + os.chdir('c:/') |
| 11 | + subprocess.Popen(f'{os.path.join(discord_parent_path, "Update.exe")} --processStart Discord.exe') |
| 12 | + os.chdir(script_working_dir) |
| 13 | + |
| 14 | + |
| 15 | +def get_discord_state() -> tuple[bool, bool]: |
| 16 | + is_discord_running = False |
| 17 | + is_discord_updating = True |
| 18 | + |
| 19 | + # The "--service-sandbox-type=audio" argument will only be in the |
| 20 | + # updated discord instance, so it won't be in the update module |
| 21 | + |
| 22 | + for process in psutil.process_iter(['name']): |
| 23 | + if process.info.get('name') == 'Discord.exe': |
| 24 | + is_discord_running = True |
| 25 | + |
| 26 | + try: |
| 27 | + for arg in process.cmdline(): |
| 28 | + if '--service-sandbox-type=audio' in arg: |
| 29 | + is_discord_updating = False |
| 30 | + except psutil.NoSuchProcess: |
| 31 | + pass |
| 32 | + |
| 33 | + return is_discord_running, is_discord_updating |
| 34 | + |
| 35 | + |
| 36 | +def kill_discord(): |
| 37 | + for process in psutil.process_iter(['name']): |
| 38 | + if process.info['name'] == 'Discord.exe': |
| 39 | + try: |
| 40 | + process.kill() |
| 41 | + except psutil.NoSuchProcess: |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +def get_latest_installed_discord_folder_name(discord_parent_path: str) -> str: |
| 46 | + discord_path = [i for i in os.listdir(discord_parent_path) if i.startswith('app-')] # remove all not 'app-' items |
| 47 | + discord_path.sort() |
| 48 | + latest_installed_discord_version = discord_path[-1] # the oldest version will be the last of list |
| 49 | + return latest_installed_discord_version |
0 commit comments