Discord MusicBot on an original Raspberry Pi

If you are anything like me, you probably ordered a Raspberry Pi when it first came out and used it as a small webserver for a few years and then forgot about it. Well, here is one way you can breath new life into the Raspberry Pi - using it to host a Discord Radio Bot!

Why you will want to follow this guide

This should be a 30 minute job, right? Just plug it in, fire up apt-get and pull some packages, right? Wrong. See, the lastest version of python avalible for the original Raspberry Pi at time of writing is Python 3.4, which is not a high enough version of Python to run SexualRhinoceros's MusicBot. So we need to compile a newer version of Python from scratch. Also, in most guides on building Python for the Raspberry Pi, it doesn't end up including the SSL module, which is also a requirement for installing the bot, so this guide will include that too. Also, you will need ffmpeg, with x264 support, which also isn't built for the original Raspberry Pi right now. Since messing up the configuration of any of these systems can cost you hours more compiling time, its best to get it right first try.

Building OpenSSL

First things first, you need to get OpenSSL. Open SSL provideds part of the implementation of SSL that Python uses as a module. To do this, run the following commands:

cd ~
curl https://www.openssl.org/source/openssl-1.1.1c.tar.gz | tar xz && cd openssl-1.1.1c && ./config shared --prefix=/usr/local/
make && sudo make install
This will download the source of OpenSSL, extract it, then build it and finally install it. Warning: go get yourself a cup of tea, and some videos to watch. Building this will take a while, especially on an original Raspberry Pi, where it might take hours.

Building Python

The second part of the puzzle is building Python with SSL support. Run the following commands:

cd ~
export LDFLAGS="-L/usr/local/lib/"
export LD_LIBRARY_PATH="/usr/local/lib/"
export CPPFLAGS="-I/usr/local/include -I/usr/local/include/openssl"
sudo apt-get update
sudo apt-get install build-essential checkinstall -y
sudo apt install libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev libtk8.5 libgdm-dev libdb4o-cil-dev libpcap-dev
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev -y
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz && tar xzf Python-3.7.3.tgz && cd Python-3.7.3 
sudo ./configure --enable-optimizations --prefix=/usr/local/ 
sudo make && sudo make install
This will set some flags related to the inclusion of OpenSSL in the python build, then get some packages required to do the build, download the python source, extract it, configure it, then build it and finally install it. Warning: again, go get yourself a cup of tea, and some more videos to watch. Probably go do some work, as building it will take a while, probably a few hours. To check it was successful, run the following:

>>>import ssl
You should see: 'OpenSSL 1.1.1c 28 May 2019' if it was a sucessful installation with OpenSSL.

Installing ffmpeg

The bot uses ffmpeg both to extract audio from videos, and to normalize the volume across all the tracks played. As of the time of writing, ffmpeg isn't built for the Raspberry Pi, so we will have to build it ourselves. However, to build it with the necessary video support, we first need to build the x264 library. The following commands will download, configure, make and install the x264 library.

sudo apt-get install git -y

cd ~
git clone --depth 1 http://git.videolan.org/git/x264 && cd x264
sudo ./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl && make -j4
sudo make install
The make command will take a long time. Next we need ffmpeg. The following commands will download, configure, make and install ffmpeg.

cd ~
git clone git://source.ffmpeg.org/ffmpeg --depth=1 && cd ffmpeg
./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree && make -j4
sudo make install
Again, the make command will take a long time.

Installing the Bot

Finally, doing what I started the project to do in the first place.

cd ~
# Installing system dependencies
sudo apt-get install libav-tools libopus-dev libffi-dev libsodium-dev -y

# Install pip if it isn't already
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.7 get-pip.py

# Get the code
git clone https://github.com/Just-Some-Bots/MusicBot.git ~/MusicBot -b master && cd MusicBot
sudo python3.7 -m pip install -U pip

# You will need to install the certificates for SSL
sudo python3.7 -m pip install -U certifi
sudo cp /usr/local/lib/python3.7/site-packages/certifi/cacert.pem /usr/local/ssl/cert.pem

sudo python3.7 -m pip install --upgrade setuptools
sudo python3.7 -m pip install -U -r requirements.txt 
Running that last command may also take a while. Lots of things take a while on the Raspberry Pi, its quite slow. Finally, run

sudo python3.7 run.py
to start the bot. You will need to configure it with Discord tokens and what you want it to play, but I won't go into detail on how to do that in this guide so see the bot's documentation.

You are finally done!