Created
January 26, 2024 01:46
-
-
Save AgentLoneStar007/b61186f140a07c5e4e0243f95067fc35 to your computer and use it in GitHub Desktop.
Update Script for the Generic Linux Package for Discord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script relies on a few factors: | |
1. You've installed Discord using the .tar.gz file from their website. | |
2. You've installed said .tar.gz file to /opt/Discord. | |
3. Your Discord binary is /usr/bin/discord. | |
4. Your home variable is $HOME. (Check this by running "echo $HOME" in a terminal.) | |
This script deletes /opt/Discord, /usr/bin/discord*, and /usr/share/applications/discord*.desktop. | |
It then downloads the generic Linux .tar.gz installer from Discord's website, extracts it to | |
/opt/Discord, and creates a symlink from /opt/Discord/Discord to /usr/bin/discord. Then, it | |
replaces the run command in /opt/Discord/discord.desktop from /usr/share/discord/Discord to /usr/bin/discord. | |
Finally, it copies this .desktop file to /usr/share/applications. | |
""" | |
import os | |
import signal | |
import sys | |
from shutil import rmtree, copy | |
import tarfile | |
import subprocess | |
import fileinput | |
import pwd | |
# Check if requests is installed | |
try: | |
import requests | |
except ModuleNotFoundError: | |
print("STOPPING! You don't have the requests module installed, which this script relies on! You can install it by" | |
"running \"pip install requests\".") | |
sys.exit(1) | |
# Check if script is being run as root | |
if os.geteuid() != 0: | |
print("STOPPING! This script must be run as root!") | |
sys.exit(1) | |
try: | |
username: str = input("Enter your non-root account username: ") | |
user_info: pwd.struct_passwd = pwd.getpwnam(username) | |
user_home: str = user_info.pw_dir | |
print("Set home directory to ", user_home, ".") | |
except KeyError: | |
print(f"STOPPING! The provided user \"{username}\" doesn't exist!") | |
sys.exit(1) | |
# Try to kill Discord if it's running | |
try: | |
# Get process ID of Discord | |
pid = os.popen("pgrep " + "discord").read().strip() | |
if not pid: | |
print("Skipping killing Discord because it's not running.") | |
else: | |
os.kill(int(pid), signal.SIGTERM) | |
except OSError as e: | |
print("STOPPING! Couldn't kill Discord process, or it couldn't be found!") | |
sys.exit(1) | |
# Remove directories | |
dirs_or_files_needing_removal: list = ["/usr/bin/discord*", "/opt/Discord/", "/usr/share/applications/discord*.desktop", | |
f"{user_home}/.local/share/applications/discord*.desktop"] | |
for x in dirs_or_files_needing_removal: | |
try: | |
if x.endswith("/") or x.endswith("*"): | |
rmtree(x) | |
else: | |
os.remove(x) | |
except FileNotFoundError as e: | |
print("Skipping a directory or file because it wasn't found. Error: ", e) | |
except Exception as e: | |
print("STOPPING! The following error occurred while deleting files and directories: ", e) | |
sys.exit(1) | |
# Download the new version of Discord | |
try: | |
url = "https://discord.com/api/download?platform=linux&format=tar.gz" | |
filename = "discord.tar.gz" | |
response: requests.Response = requests.get(url, stream=True) | |
if response.status_code == 200: | |
with open(filename, 'wb') as f: | |
for chunk in response.iter_content(1024): | |
f.write(chunk) | |
print("Downloaded Discord.") | |
else: | |
print("STOPPING! Download of Discord failed with the following HTTP response code:", response.status_code) | |
sys.exit(1) | |
except Exception as e: | |
print("STOPPING! Failed to download new version of Discord. Error: ", e) | |
sys.exit(1) | |
# Extract downloaded Discord archive to /opt | |
try: | |
with tarfile.open("discord.tar.gz", "r:gz") as tar: | |
tar.extractall(path="/opt") # Extract to the specified path | |
print("Extracted downloaded Discord archive to /opt.") | |
except tarfile.TarError as e: | |
print("STOPPING! Error extracting tar file: ", e) | |
sys.exit(1) | |
except Exception as e: | |
print("STOPPING! The following error occurred while extracting the Discord file to the /opt directory: ", e) | |
sys.exit(1) | |
# Create symbolic link | |
try: | |
command: list = ["ln", "-sf", "/opt/Discord/Discord", "/usr/bin/discord"] | |
subprocess.run(command, check=True) # Check=True raises an exception on non-zero exit codes | |
print("Created link to Discord binary in /usr/bin.") | |
except subprocess.CalledProcessError as e: | |
print("STOPPING! Failed to create link between /opt/Discord/Discord and /usr/bin/discord with the following error: ", e) | |
sys.exit(1) | |
# Update discord.desktop file | |
try: | |
file_path = "/opt/Discord/discord.desktop" | |
old_text = "/usr/share/discord/Discord" | |
new_text = "/usr/bin/discord" | |
with fileinput.FileInput(file_path, inplace=True) as file: | |
for line in file: | |
print(line.replace(old_text, new_text), end='') | |
print("Updated discord.desktop file.") | |
except Exception as e: | |
print("STOPPING! Failed to update discord.desktop file with the following error: ", e) | |
sys.exit(1) | |
# Copy discord.desktop file to /usr/share/applications | |
try: | |
copy("/opt/Discord/discord.desktop", "/usr/share/applications/discord.desktop") | |
print("Copied discord.desktop file to /usr/share/applications/") | |
except Exception as e: | |
print("STOPPING! The following error occurred when copying discord.desktop to /usr/share/applications: ", e) | |
sys.exit(1) | |
try: | |
os.remove("discord.tar.gz") | |
print("Cleaned up.") | |
except Exception as e: | |
print("Failed to remove Discord archive with the following error: ", e) | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment