Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EDM115/5b6918c4433de7038588c78d602f7de5 to your computer and use it in GitHub Desktop.
Save EDM115/5b6918c4433de7038588c78d602f7de5 to your computer and use it in GitHub Desktop.
Discord on Linux : auto upgrade and auto install BetterDiscord
[Desktop Entry]
Name=Discord PTB - EDM115
StartupWMClass=discord
Comment=All-in-one voice and text chat for gamers that's free, secure, and works on both your desktop and phone.
GenericName=Internet Messenger
Exec=gnome-terminal -- sudo /path/to/launch-discord.sh -b ptb -bd true -u edm115
Icon=discord-ptb
Type=Application
Categories=Network;InstantMessaging;
Path=/usr/bin

Discord on Linux : auto upgrade and auto install BetterDiscord

If you've installed discord on Linux through apt, you know how much of a pain it is to manually download and install it every now and then. It's even worse if you're having to reinstall BetterDiscord every time
So here's 2 simple scripts to fix this :

launch-discord.sh

  • Versatile script to launch discord
  • Auto launches the upgrade script if needed (have to be in the same folder)
  • Detaches the Discord instance from the terminal once done

Note

requires to use sudo when an upgrade is needed. In such case, needs the user param to be passed

Important

Edit the path to the upgrade script on line 60 to an absolute path, this way you can run the command from any directory

Usage :

./launch-discord.sh -b BUILD -bd BOOL -u USER
  • -b or --build : the build you have installed. can be stable, ptb or canary
  • -bd or --betterdiscord : precise if you have BetterDiscord installed. can be true or anything else for false. can be ommited
  • -u or --user : specify the user that will be used to run Discord, which can't be ran as root, so simply running the script as sudo will break things, this is why this param is needed

Example : ./launch-discord.sh -b ptb -bd true -u $(whoami)

upgrade-discord.sh

  • Super simple script to upgrade Discord
  • Automatically fetches the latest version if none is passed
  • Launches Discord and lets it finishing to download its updates before installing BetterDiscord

Usage :

sudo ./upgrade-discord.sh -b BUILD -v VER -bd BOOL -u USER
  • sudo : Because further commands requires sudo
  • -b or --build : the build you have installed. can be stable, ptb or canary
  • -v or --version : the version to download. if this script is run through launch-discord.sh, it will be the one that discord requests. format : x.x.xx(x), when I'm writing this the versions are 0.0.50 for stable, 0.0.80 for ptb and 0.0.357 for canary. if not provided, downloads the latest version available
  • -bd or --betterdiscord : precise if you have BetterDiscord installed. can be true or anything else for false. can be ommited
  • -u or --user : specify the user that will be used to run Discord, which can't be ran as root, so simply running the script as sudo will break things, this is why this param is needed

Example : sudo ./upgrade-discord.sh -b ptb -v 0.0.80 -bd true -u $(whoami)

better-discord.desktop

  • A simple file so you can have on your desktop, app menu or dash the correct version

Usage :

sudo nano /usr/share/applications/better-discord.desktop

Copy-paste the content

Important

Edit the path to the launch script (absolute path), and customize the arguments. Here the user have to be hardcoded

Prerequisites

  • Having wget and apt-get on your machine
  • For BetterDiscord : having it installed through betterdiscordctl
  • Allow the scripts to be executable (chmod +x launch-discord.sh upgrade-discord.sh)

Notes

This may or may not work in the future, depending of how discord logs things when starting

#!/bin/bash
echo "--- Launch Discord script by EDM115 ---"
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-b|--build) build="$2"; shift ;;
-bd|--betterdiscord) betterdiscord="$2"; shift ;;
-u|--user) user="$2"; shift ;;
*) echo "Unknown parameter passed : $1"; exit 1 ;;
esac
shift
done
# Validate build parameter
if [[ -z "$build" ]]; then
echo "Build not provided. Usage : -b/--build [stable|ptb|canary]"
echo "Exiting..."
exit 1
fi
# Define variables based on build
case $build in
"stable") appname="discord"; pname="Discord" ;;
"ptb") appname="discord-ptb"; pname="DiscordPTB" ;;
"canary") appname="discord-canary"; pname="DiscordCanary" ;;
*) echo "Invalid build. Use 'stable', 'ptb', or 'canary'"; exit 1 ;;
esac
# Determine how to launch the app based on whether it's run as root
if [[ $EUID -eq 0 ]]; then
# Check if user is set when script is run as root
if [[ -z "$user" ]]; then
echo "No user specified. Use -u/--user to specify a user."
exit 1
fi
launchcommand="sudo -u $user bash -c \"'$appname'\""
else
# Use the current user to run the app
user="$(whoami)"
launchcommand="$appname"
fi
# Launch discord and capture stdout
eval "$launchcommand" | while read line
do
echo "$line"
# Check for the update-manually message
if [[ "$line" == *"update-manually"* ]]; then
# Extract version from the line
version=$(echo "$line" | grep -oP 'update-manually \K[0-9.]+')
# Kill discord
pgrep -i $pname | xargs -I {} kill -9 {}
sleep 2
# Call updater script with the new version and restart discord
sudo /home/edm115/Documents/upgrade-discord.sh -v $version -b $build -bd $betterdiscord -u $user | while read update_line
do
if [[ "$update_line" == *"Done !"* ]]; then
eval "$launchcommand" &
break 2
fi
done
break
fi
# Check for the splashScreen.pageReady message (no update)
if [[ "$line" == *"splashScreen.pageReady"* ]]; then
xwininfo -root -tree | grep -i $pname | awk '{print $1}' | xargs -I {} xkill -id {}
nohup bash -c "$launchcommand" >/dev/null 2>&1 &
break
fi
done
#!/bin/bash
echo "--- Upgrade Discord script by EDM115 ---"
# Ensure the script is ran as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Use sudo"
echo "Exiting..."
exit 1
fi
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-v|--version) version="$2"; shift ;;
-b|--build) build="$2"; shift ;;
-bd|--betterdiscord) betterdiscord="$2"; shift ;;
-u|--user) user="$2"; shift ;;
*) echo "Unknown parameter passed : $1"; exit 1 ;;
esac
shift
done
# Validate build parameter
if [[ -z "$build" ]]; then
echo "Build not provided. Usage : -b/--build [stable|ptb|canary]"
echo "Exiting..."
exit 1
fi
# Define variables and URLs based on build
case $build in
"stable") appname="discord"; pname="Discord"; base_url="https://dl.discordapp.net/apps/linux/" ;;
"ptb") appname="discord-ptb"; pname="DiscordPTB"; base_url="https://dl-ptb.discordapp.net/apps/linux/" ;;
"canary") appname="discord-canary"; pname="DiscordCanary"; base_url="https://dl-canary.discordapp.net/apps/linux/" ;;
*) echo "Invalid build. Use 'stable', 'ptb', or 'canary'"; exit 1 ;;
esac
# Ensure a user is specified as the script is run as root, see https://issues.chromium.org/issues/40480798
if [[ -z "$user" ]]; then
echo "No user specified. Please specify a user with -u/--user."
exit 1
fi
launchcommand="sudo -u $user bash -c \"'$appname'\""
# Handle versioning
if [[ -z "$version" ]]; then
echo "No version specified, downloading latest version..."
url="https://discord.com/api/download/$build?platform=linux&format=deb"
dest="/tmp/$appname-latest.deb"
version="latest"
else
url="${base_url}${version}/${appname}-${version}.deb"
dest="/tmp/$appname-$version.deb"
fi
# Download the file
echo "Downloading $appname from $url..."
wget --content-disposition --trust-server-names $url -O $dest
# Install the package
echo "Installing $appname..."
sudo apt-get install "$dest"
# Remove the installer
rm "$dest"
# If BetterDiscord is enabled, upgrade it
if [[ "$betterdiscord" == "true" ]]; then
echo "Reappliying BetterDiscord..."
eval "$launchcommand" > /tmp/discord_upgrade_output.log 2>&1 &
# Monitor until "splashScreen.pageReady" appears
tail -f /tmp/discord_upgrade_output.log | while read line; do
if [[ "$line" == *"splashScreen.pageReady"* ]]; then
sleep 8
xwininfo -root -tree | grep -i $pname | awk '{print $1}' | xargs -I {} xkill -id {}
break
fi
done
rm /tmp/discord_upgrade_output.log
# Upgrade betterdiscordctl
echo "Upgrading BetterDiscord..."
sudo betterdiscordctl self-upgrade
# Install BetterDiscord
if [[ "$build" == "stable" ]]; then
bdparams=""
else
bdparams="-f '$build'"
fi
bdcommand="sudo -u $user bash -c \"betterdiscordctl $bdparams install\""
eval "$bdcommand"
echo "Betterdiscord updated"
fi
echo "$appname updated to v.$version"
echo "Done !"
@EDM115
Copy link
Author

EDM115 commented Jun 12, 2024

@a-sajjad72 this is very probably due to me not being able to kill discord properly, I tried 4 ways already and all of them trigger an error at some point
also note than this script have only be tested with ubuntu, the script may/will break on other distros 😢

@a-sajjad72
Copy link

@a-sajjad72 this is very probably due to me not being able to kill discord properly, I tried 4 ways already and all of them trigger an error at some point also note than this script have only be tested with ubuntu, the script may/will break on other distros 😢

I am also using Ubuntu machine.

Distributor ID:	Ubuntu
Description:	Ubuntu 24.04 LTS
Release:	24.04
Codename:	noble

@HK4040
Copy link

HK4040 commented Feb 25, 2025

Hi @a-sajjad72 @EDM115 i created a custom script for this pupose u can check this out
https://github.com/HK4040/Discord_AutoUpdate_linux

@Rambomst
Copy link

Rambomst commented Mar 25, 2025

@EDM115 when I launch discord using this script it kills my firefox instance every time. Once discord has started I can't hear audio within it. If I try and download a video from within discord I get a popup saying Firefox isn't able to start. Not really sure what is happening but when I launch DIscord normally its fine.

@EDM115
Copy link
Author

EDM115 commented Mar 26, 2025

@Rambomst oh wtf
this shouldn't happen. hmmm...
i will try to fix definitely this script on the coming weeks

@Rambomst
Copy link

Let me know if you want me to do any debugging. I am running popos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment