forked from Just-Some-Bots/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·1217 lines (1061 loc) · 40.1 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# MusicBot and this file are provided under an MIT license.
# Please see the LICENSE file for details.
#
# This file attempts to provide automatic install of MusicBot and dependencies on
# a variety of different Linux distros.
#
# TODO: Venv install with pre-cloned repo needs fixed.
#-----------------------------------------------Configs-----------------------------------------------#
# Original repo is here:
#MusicBotGitURL="https://github.com/Just-Some-Bots/MusicBot.git"
# This is currently required since upstream does not have the updated service file.
MusicBotGitURL="https://github.com/itsTheFae/MusicBot.git"
CloneDir="MusicBot"
VenvDir="MusicBotVenv"
InstallDir=""
ServiceName="musicbot"
EnableUnlistedBranches=0
DEBUG=0
#----------------------------------------------Constants----------------------------------------------#
DEFAULT_URL_BASE="https://discordapp.com/api"
# Suported versions of python using only major.minor format
PySupported=("3.8" "3.9" "3.10" "3.11" "3.12")
PyBin="python3"
# Path updated by find_python
PyBinPath="$(command -v "$PyBin")"
USER_OBJ_KEYS="id username discriminator verified bot email avatar"
# Status indicator for post-install notice about python venv based install.
InstalledViaVenv=0
declare -A BOT
# Get some notion of the current OS / distro name.
# This will not exhaust options, or ensure a correct name is returned.
# A good example is Raspberry Pi OS 11 which returns Debian.
if [ -n "$(command -v lsb_release)" ] ; then
# Most debian-based distros might have this installed, but not always.
# Redhat-based distros usually need to install it via redhat-lsb-core package.
DISTRO_NAME=$(lsb_release -s -d)
elif [ -f "/etc/os-release" ]; then
# Many distros have this file, but not all of them are version complete.
# For example, CentOS 7 will return "CentOS Linux 7 (Core)"
# If we need to know the minor version, we need /etc/redhat-release instead.
# Same for Debian, which uses /etc/debian_version file instead.
DISTRO_NAME=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="')
elif [ -f "/etc/debian_version" ]; then
DISTRO_NAME="Debian $(cat /etc/debian_version)"
elif [ -f "/etc/redhat-release" ]; then
DISTRO_NAME=$(cat /etc/redhat-release)
else
# In case everything fails, use Kernel name and release.
DISTRO_NAME="$(uname -s) $(uname -r)"
fi
#----------------------------------------------Functions----------------------------------------------#
function get_supported() {
# Search this file and extract names from the supported cases below.
# We control which cases we grab based on the space at the end of each
# case pattern, before ) or | characters.
# This allows adding complex cases which will be excluded from the list.
Avail=$(grep -oh '\*"[[:alnum:] _!\./]*"\*[|)]' "$0" )
Avail="${Avail//\*\"/}"
Avail="${Avail//\"\*/}"
Avail="${Avail//[|)]/}"
echo "$Avail"
}
function distro_supported() {
# Loops over "supported" distros and color-codes the current distro.
OIFS=$IFS
IFS=$'\n'
for dist in $(get_supported) ; do
debug "Testing '$dist' in '$DISTRO_NAME'"
if [[ "$DISTRO_NAME" == *"$dist"* ]] ; then
echo -e "\e[1;32m${DISTRO_NAME}\e[0m"
IFS=$OIFS
return 0
fi
done
IFS=$OIFS
echo -e "\e[1;33m${DISTRO_NAME}\e[0m"
return 1
}
function list_supported() {
# List off "supported" linux distro/versions if asked to and exit.
echo "We detected your OS is: $(distro_supported)"
echo ""
echo "The MusicBot installer might have support for these flavors of Linux:"
get_supported
echo ""
exit 0
}
function show_help() {
# provide help text for the installer and exit.
echo "MusicBot Installer script usage:"
echo " $0 [OPTIONS]"
echo ""
echo "By default, the installer script installs as the user who runs the script."
echo "The user should have permission to install system packages using sudo."
echo "Do NOT run this script with sudo, you will be prompted when it is needed!"
echo "To bypass steps that use sudo, use --no-sudo or --no-sys as desired."
echo " Note: Your system admin must install the packages before hand, by using:"
echo " $0 --sys-only"
echo ""
echo "Available Options:"
echo ""
echo " --list List potentially supported versions and exits."
echo " --help Show this help text and exit."
echo " --sys-only Install only system packages, no bot or pip libraries."
echo " --service Install only the system service for MusicBot."
echo " --no-sys Bypass system packages, install bot and pip libraries."
echo " --no-sudo Skip all steps that use sudo. This implies --no-sys."
echo " --debug Enter debug mode, with extra output. (for developers)"
echo " --any-branch Allow any existing branch to be given at the branch prompt. (for developers)"
echo " --dir [PATH] Directory into which MusicBot will be installed. Default is user Home directory."
echo ""
exit 0
}
function exit_err() {
echo "$@"
exit 1
}
function build_python() {
PyBuildVer="3.10.14"
PySrcDir="Python-${PyBuildVer}"
PySrcFile="${PySrcDir}.tgz"
PySrcUrl="https://www.python.org/ftp/python/${PyBuildVer}/${PySrcFile}"
# Ask if we should build python
echo "We need to build python from source for your system."
echo "It will be installed using the altinstall target to avoid conflicts."
echo " Building Python ${PyBuildVer} from: ${PySrcUrl}"
read -rp "Would you like to continue ? [N/y]" BuildPython
if [ "${BuildPython,,}" == "y" ] || [ "${BuildPython,,}" == "yes" ] ; then
# Build python.
curl -o "$PySrcFile" "$PySrcUrl"
tar -xzf "$PySrcFile"
cd "${PySrcDir}" || exit_err "Fatal: Could not change to python source directory."
./configure --enable-optimizations
sudo make altinstall
# Ensure python bin is updated with altinstall name.
find_python
RetVal=$?
if [ "$RetVal" == "0" ] ; then
# check if pip is available
$PyBin -m pip --version >/dev/null 2>&1
if [ "$?" == "1" ] ; then
# manually install pip package for current user.
$PyBin <(curl -s https://bootstrap.pypa.io/get-pip.py)
fi
else
echo "Error: Could not find python on the PATH after installing it."
exit 1
fi
fi
}
function find_python() {
# compile a list of bin names to try for.
PyBins=("python3") # We hope that python3 maps to a good version.
for Ver in "${PySupported[@]}" ; do
# Typical of source builds and many packages to include the version dot.
PyBins+=("python${Ver}")
# Some distros remove the dot.
PyBins+=("python${Ver//./}")
done
PyBins+=("python") # Fat chance, but might as well try versionless too.
# Test each possible PyBin until the first supported version is found.
for PyBinTest in "${PyBins[@]}" ; do
if ! command -v "$PyBinTest" > /dev/null 2>&1 ; then
continue
fi
# Get version data from python, assume python exists in PATH somewhere.
# shellcheck disable=SC2207
PY_VER=($($PyBinTest -c "import sys; print('%s %s %s' % sys.version_info[:3])" || { echo "0 0 0"; }))
if [[ "${PY_VER[0]}" == "0" ]]; then
continue
fi
PY_VER_MAJOR=$((PY_VER[0]))
PY_VER_MINOR=$((PY_VER[1]))
PY_VER_PATCH=$((PY_VER[2]))
# echo "run.sh detected $PY_BIN version: $PY_VER_MAJOR.$PY_VER_MINOR.$PY_VER_PATCH"
# Major version must be 3+
if [[ $PY_VER_MAJOR -ge 3 ]]; then
# If 3, minor version minimum is 3.8
if [[ $PY_VER_MINOR -eq 8 ]]; then
# if 3.8, patch version minimum is 3.8.7
if [[ $PY_VER_PATCH -ge 7 ]]; then
PyBinPath="$(command -v "$PyBinTest")"
PyBin="$PyBinTest"
debug "Selected: $PyBinTest @ $PyBinPath"
return 0
fi
fi
# if 3.9+ it should work.
if [[ $PY_VER_MINOR -ge 9 ]]; then
PyBinPath="$(command -v "$PyBinTest")"
PyBin="$PyBinTest"
debug "Selected: $PyBinTest @ $PyBinPath"
return 0
fi
fi
done
PyBinPath="$(command -v "python3")"
PyBin="python3"
debug "Default: python3 @ $PyBinPath"
return 1
}
function find_python_venv() {
# activates venv, locates python bin, deactivates venv.
# shellcheck disable=SC1091
source "../bin/activate"
find_python
deactivate
}
function in_existing_repo() {
# check the current working directory is a MusicBot repo clone.
GitDir="${PWD}/.git"
BotDir="${PWD}/musicbot"
ReqFile="${PWD}/requirements.txt"
RunFile="${PWD}/run.py"
if [ -d "$GitDir" ] && [ -d "$BotDir" ] && [ -f "$ReqFile" ] && [ -f "$RunFile" ]; then
return 0
fi
return 1
}
function in_venv() {
# Check if the current directory is inside a Venv, does not activate.
# Assumes the current directory is a MusicBot clone.
if [ -f "../bin/activate" ] ; then
return 0
fi
return 1
}
function pull_musicbot_git() {
echo ""
# Check if we're running inside a previously pulled repo.
# ignore this if InstallDir is set.
if in_existing_repo && [ "$InstallDir" == "" ]; then
echo "Existing MusicBot repo detected."
read -rp "Would you like to install using the current repo? [Y/n]" UsePwd
if [ "${UsePwd,,}" == "y" ] || [ "${UsePwd,,}" == "yes" ] ; then
echo ""
CloneDir="${PWD}"
CloneDirName="$(basename "$PWD")"
# sort out the directory structure, we want musicbot inside its venv.
if [ "$InstalledViaVenv" == "1" ] ; then
# if the venv is inside our repo, move it out.
if [ -d "$VenvDir" ] ; then
echo "Installer needs to move your clone inside the Venv."
echo " - moving $VenvDir up one directory."
mv "./${VenvDir}" "../${VenvDir}"
echo " - moving $CloneDirName into $VenvDir"
cd .. || exit_err "Failed to leave repo directory."
mv "$CloneDir" "${VenvDir}/$CloneDirName"
cd "${VenvDir}/$CloneDirName" || exit_err "Failed to enter clone directory."
CloneDir="${PWD}"
fi
fi
$PyBin -m pip install --upgrade -r requirements.txt
echo ""
if [ ! -f "./config/options.ini" ] ; then
echo "Creating default options.ini file from example_options.ini file."
echo ""
cp ./config/example_options.ini ./config/options.ini
fi
return 0
fi
echo "Installer will attempt to create a new directory for MusicBot."
fi
# test if we install at home-directory or a specified path.
if [ "$InstallDir" == "" ] ; then
cd ~ || exit_err "Fatal: Could not change into home directory."
if [ -d "${CloneDir}" ] ; then
echo "Error: A directory named ${CloneDir} already exists in your home directory."
exit_err "Delete the ${CloneDir} directory and try again, or complete the install manually."
fi
else
cd "$InstallDir" || exit_err "Fatal: Could not change into install directory: ${InstallDir}"
if [ "$InstalledViaVenv" != "1" ] ; then
CloneDir="${InstallDir}"
fi
fi
echo ""
echo "MusicBot currently has three branches available."
echo " master - An older MusicBot, for older discord.py. May not work without tweaks!"
echo " review - Newer MusicBot, usually stable with less updates than the dev branch."
echo " dev - The newest MusicBot, latest features and changes which may need testing."
if [ "$EnableUnlistedBranches" == "1" ] ; then
echo " * - WARNING: Any branch name is allowed, if it exists on github."
fi
echo ""
read -rp "Enter the branch name you want to install: " BRANCH
case ${BRANCH,,} in
"dev")
echo "Installing from 'dev' branch..."
git clone "${MusicBotGitURL}" "${CloneDir}" -b dev
;;
"review")
echo "Installing from 'review' branch..."
git clone "${MusicBotGitURL}" "${CloneDir}" -b review
;;
"master")
echo "Installing from 'master' branch..."
git clone "${MusicBotGitURL}" "${CloneDir}" -b master
;;
*)
if [ "$EnableUnlistedBranches" == "1" ] ; then
echo "Installing from '${BRANCH}' branch..."
git clone "${MusicBotGitURL}" "${CloneDir}" -b "$BRANCH"
else
exit_err "Unknown branch name given, install cannot continue."
fi
;;
esac
cd "${CloneDir}" || exit_err "Fatal: Could not change to MusicBot directory."
$PyBin -m pip install --upgrade -r requirements.txt
echo ""
if ! [ -f ./config/options.ini ] ; then
echo "Creating default options.ini file from example_options.ini file."
echo ""
cp ./config/example_options.ini ./config/options.ini
fi
}
function install_as_venv() {
# Create and activate a venv using python that is installed.
find_python
$PyBin -m venv "${VenvDir}"
InstalledViaVenv=1
CloneDir="${VenvDir}/${CloneDir}"
# shellcheck disable=SC1091
source "${VenvDir}/bin/activate"
find_python
pull_musicbot_git
# exit venv
deactivate
}
function issue_root_warning() {
echo "Just like my opinion, but root and MusicBot shouldn't mix."
echo "The installer will prevent this for the benefit of us all."
}
function ask_for_user() {
# ask the user to supply a valid username. It must exist already.
while :; do
echo ""
read -rp "Please enter an existing User name: " Inst_User
if id -u "$Inst_User" >/dev/null 2>&1; then
if [ "${Inst_User,,}" == "root" ] ; then
issue_root_warning
echo "Try again."
Inst_User=""
else
return 0
fi
else
echo "Username does not exist! Try again."
Inst_User="$(id -un)"
fi
done
}
function ask_for_group() {
# ask the user to supply a valid group name. It must exist already.
while :; do
echo ""
read -rp "Please enter an existing Group name: " Inst_Group
if id -g "$Inst_Group" >/dev/null 2>&1 ; then
if [ "${Inst_Group,,}" == "root" ] ; then
issue_root_warning
echo "Try again."
Inst_Group=""
else
return 0
fi
else
echo "Group does not exist! Try again."
Inst_Group="$(id -gn)"
fi
done
}
function ask_change_user_group() {
User_Group="${Inst_User} / ${Inst_Group}"
echo ""
echo "The installer is currently running as: ${User_Group}"
read -rp "Set a different User / Group to run the service? [N/y]: " MakeChange
case $MakeChange in
[Yy]*)
ask_for_user
ask_for_group
;;
esac
}
function ask_change_service_name() {
echo ""
echo "The service will be installed as: $ServiceName"
read -rp "Would you like to change the name? [N/y]: " ChangeSrvName
case $ChangeSrvName in
[Yy]*)
while :; do
# ASCII letters, digits, ":", "-", "_", ".", and "\"
# but I know \ will complicate shit. so no thanks, sysD.
echo ""
echo "Service names may use only letters, numbers, and the listed special characters."
echo "Spaces are not allowed. Special characters: -_.:"
read -rp "Provide a name for the service: " ServiceName
# validate service name is allowed.
if [[ "$ServiceName" =~ ^[a-zA-Z0-9:-_\.]+$ ]] ; then
# attempt to avoid conflicting service names...
if ! systemctl list-unit-files "$ServiceName" &>/dev/null ; then
return 0
else
echo "A service by this name already exists, try another."
fi
else
echo "Invalid service name, try again."
fi
done
;;
esac
}
function generate_service_file() {
# generate a .service file in the current directory.
cat << EOSF > "$1"
[Unit]
Description=Just-Some-Bots/MusicBot a discord.py bot that plays music.
# Only start this service after networking is ready.
After=network.target
[Service]
# If you do not set these, MusicBot may run as root! You've been warned!
User=${Inst_User}
Group=${Inst_Group}
# Replace with a path where MusicBot was cloned into.
WorkingDirectory=${PWD}
# Here you need to use both the correct python path and a path to run.py
ExecStart=${PyBinPath} ${PWD}/run.py --no-checks
# Set the condition under which the service should be restarted.
# Using on-failure allows the bot's shutdown command to actually stop the service.
# Using always will require you to stop the service via the service manager.
Restart=on-failure
# Time to wait between restarts. Useful to avoid rate limits.
RestartSec=8
[Install]
WantedBy=default.target
EOSF
}
function setup_as_service() {
# Provide steps to generate and install a .service file
# check for existing repo or install --dir option.
if ! in_existing_repo ; then
if [ "$InstallDir" != "" ]; then
cd "$InstallDir" || { exit_err "Could not cd to the supplied install directory."; }
# if we still aren't in a valid repo, warn the user but continue on.
if ! in_existing_repo ; then
echo "WARNING:"
echo " Installer is generating a service file without a valid install!"
echo " The generated file may not contain the correct paths to python or run.py"
echo " Manually edit the service file or re-run using a valid install directory."
echo ""
fi
else
echo "The installer cannot generate a service file without an existing installation."
echo "Please add the --dir option or install the MusicBot first."
echo ""
return 1
fi
fi
# check if we're in a venv install.
if in_venv ; then
debug "Detected a Venv install"
find_python_venv
else
find_python
fi
# TODO: should we assume systemd is all? perhaps check for it first...
Inst_User="$(id -un)"
Inst_Group="$(id -gn)"
echo ""
echo "The installer can also install MusicBot as a system service."
echo "This starts the MusicBot at boot and restarts after failures."
read -rp "Install the musicbot system service? [N/y] " SERVICE
case $SERVICE in
[Yy]*)
ask_change_service_name
ask_change_user_group
if [ "$Inst_User" == "" ] ; then
echo "Cannot set up the service with a blank User name."
return 1
fi
if [ "$Inst_Group" == "" ] ; then
echo "Cannot set up the service with a blank Group name."
return 1
fi
SrvCpyFile="./${ServiceName}.service"
SrvInstFile="/etc/systemd/system/${ServiceName}.service"
echo ""
echo "Setting up MusicBot as a service named: ${ServiceName}"
echo "Generated File: ${SrvCpyFile}"
generate_service_file "${SrvCpyFile}"
if [ "$SKIP_ALL_SUDO" == "0" ] ; then
# Copy the service file into place and enable it.
sudo cp "${SrvCpyFile}" "${SrvInstFile}"
sudo chown root:root "$SrvInstFile"
sudo chmod 644 "$SrvInstFile"
# TODO: maybe we need to reload the daemon...
# sudo systemctl daemon-reload
sudo systemctl enable "$ServiceName"
echo "Installed File: ${SrvInstFile}"
echo ""
echo "MusicBot will start automatically after the next reboot."
read -rp "Would you like to start MusicBot now? [N/y]" StartService
case $StartService in
[Yy]*)
echo "Running: sudo systemctl start $ServiceName"
sudo systemctl start "$ServiceName"
;;
esac
else
echo "Installing of generated service skipped, sudo is required to install it."
echo "The file was left on disk so you can manually install it."
fi
echo ""
;;
esac
return 0
}
function debug() {
local msg=$1
if [[ $DEBUG == '1' ]]; then
echo -e "\e[1;36m[DEBUG]\e[0m $msg" 1>&2
fi
}
function strip_dquote() {
result="${1%\"}"
result="${result#\"}"
echo "$result"
}
function r_data() {
local data=$1
echo "$data" | sed -rn 's/(\{.+)\} ([0-9]+)$/\1}/p'
}
function r_code() {
local data=$1
echo "$data" | sed -rn 's/(\{.+)\} ([0-9]+)$/\2/p'
}
function key() {
local data=$1
local key=$2
echo "$data" | jq ".$key"
}
function r() {
local token=$1
local method=$2
local route=$3
local url="$DEFAULT_URL_BASE/$route"
debug "Attempting to load url $url with token $token"
res=$(curl -k -s \
-w " %{http_code}" \
-H "Authorization: Bot $token" \
-H "Content-Type: application/json" \
-X "$method" \
"$url" | tr -d '\n')
echo "$res"
}
function get_token_and_create_bot() {
# Set bot token
echo ""
echo "Please enter your bot token. This can be found in your discordapp developer page."
read -rp "Enter Token:" -s token
create_bot "$token"
}
function create_bot() {
local bot_token=$1
local me
local me_code
local me_data
me=$(r "$bot_token" "GET" "users/@me")
me_code=$(r_code "$me")
me_data=$(r_data "$me")
if ! [[ $me_code == "200" ]]; then
echo ""
echo "Error getting user profile, is the token correct? ($me_code $me_data)"
exit 1
else
debug "Got user profile: $me_data"
fi
for k in $USER_OBJ_KEYS; do
BOT[$k]=strip_dquote "$(key "$me_data" "$k")"
done
BOT["token"]=$bot_token
# We're logged on!
echo "Logged on with ${BOT["username"]}#${BOT["discriminator"]}"
sed -i "s/bot_token/$bot_token/g" ./config/options.ini
}
function configure_bot() {
read -rp "Would like to configure the bot for basic use? [N/y]" YesConfig
if [ "${YesConfig,,}" != "y" ] && [ "${YesConfig,,}" != "yes" ] ; then
return
fi
get_token_and_create_bot
# Set prefix, if user wants
read -rp "Would you like to change the command prefix? [N/y] " chngprefix
case $chngprefix in
[Yy]*)
echo "Please enter the prefix you'd like for your bot."
read -rp "This is what comes before all commands. The default is [!] " prefix
sed -i "s/CommandPrefix = !/CommandPrefix = $prefix/g" ./config/options.ini
;;
[Nn]*) echo "Using default prefix [!]" ;;
*) echo "Using default prefix [!]" ;;
esac
# Set owner ID, if user wants
read -rp "Would you like to automatically get the owner ID from the OAuth application? [Y/n] " accountcheck
case $accountcheck in
[Yy]*) echo "Getting owner ID from OAuth application..." ;;
[Nn]*)
read -rp "Please enter the owner ID. " ownerid
sed -i "s/OwnerID = auto/OwnerID = $ownerid/g" ./config/options.ini
;;
*) echo "Getting owner ID from OAuth application..." ;;
esac
# Enable/Disable AutoPlaylist
read -rp "Would you like to enable the autoplaylist? [Y/n] " autoplaylist
case $autoplaylist in
[Yy]*) echo "Autoplaylist enabled." ;;
[Nn]*)
echo "Autoplaylist disabled"
sed -i "s/UseAutoPlaylist = yes/UseAutoPlaylist = no/g" ./config/options.ini
;;
*) echo "Autoplaylist enabled." ;;
esac
}
#------------------------------------------CLI Arguments----------------------------------------------#
INSTALL_SYS_PKGS="1"
INSTALL_BOT_BITS="1"
SERVICE_ONLY="0"
SKIP_ALL_SUDO="0"
while [[ $# -gt 0 ]]; do
case ${1,,} in
--list )
shift
list_supported
;;
--help )
shift
show_help
;;
--no-sys )
INSTALL_SYS_PKGS="0"
shift
;;
--no-sudo )
INSTALL_SYS_PKGS="0"
SKIP_ALL_SUDO="1"
shift
;;
--sys-only )
INSTALL_BOT_BITS="0"
shift
;;
--service )
SERVICE_ONLY="1"
shift
;;
--any-branch )
EnableUnlistedBranches=1
shift
;;
--debug )
DEBUG=1
shift
echo "DEBUG MODE IS ENABLED!"
;;
"--dir" )
InstallDir="$2"
shift
shift
if [ "${InstallDir:0-1}" != "/" ] ; then
InstallDir="${InstallDir}/"
fi
if ! [ -d "$InstallDir" ] ; then
exit_err "The install directory given does not exist: '$InstallDir'"
fi
VenvDir="${InstallDir}${VenvDir}"
;;
* )
exit_err "Unknown option $1"
;;
esac
done
if [ "${INSTALL_SYS_PKGS}${INSTALL_BOT_BITS}" == "00" ] ; then
exit_err "The options --no-sys and --sys-only cannot be used together."
fi
#------------------------------------------------Logic------------------------------------------------#
if [ "$SERVICE_ONLY" = "1" ] ; then
setup_as_service
exit 0
fi
# display preamble
cat << EOF
MusicBot Installer
MusicBot and this installer are provided under an MIT license.
This software is provided "as is" and may not be fit for any particular use, stated or otherwise.
Please read the LICENSE file for full details.
This installer attempts to provide automatic install for MusicBot and dependency packages.
It may use methods which are out-of-date on older OS versions, or fail on newer versions.
It is recommended that you personally check the installer script before running it,
and verify the steps for your OS and distro version are correct.
Please consider contributing corrections or new steps if you find issues with this installer.
You may also find installation guides on the wiki or community help on our discord server.
Wiki:
https://just-some-bots.github.io/MusicBot/
Discord:
https://discord.gg/bots
For a list of potentially supported OS, run the command:
$0 --list
EOF
echo "We detected your OS is: $(distro_supported)"
read -rp "Would you like to continue with the installer? [Y/n]: " iagree
if [[ "${iagree,,}" != "y" && "${iagree,,}" != "yes" ]] ; then
exit 2
fi
# check if we are running as root, and if so make a more informed choice.
if [ "$(id -u)" -eq "0" ] && [ "$INSTALL_BOT_BITS" == "1" ] ; then
# in theory, we could prompt for a user and do all the setup.
# better that folks learn to admin their own systems though.
echo ""
echo -e "\e[1;37m\e[41m Warning \e[0m You are using root and installing MusicBot."
echo " This can break python permissions and will create MusicBot files as root."
echo " Meaning, little or no support and you have to fix stuff manually."
echo " Running MuiscBot as root is not recommended. You have been warned."
echo ""
read -rp "Type 'I understand' (without quotes) to continue installing:" iunderstand
if [[ "${iunderstand,,}" != "i understand" ]] ; then
echo ""
exit_err "Try again with --sys-only or change to a non-root user and use --no-sys and/or --no-sudo"
fi
fi
# check if we can sudo or not
if [ "$SKIP_ALL_SUDO" == "0" ] ; then
echo "Checking if user can sudo..."
if ! sudo -v ; then
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
echo -e "\e[1;31mThe current user cannot run sudo to install system packages.\e[0m"
echo "If you have already installed system dependencies, try again with:"
echo " $0 --no-sys"
echo ""
echo "To install system dependencies, switch to root and run:"
echo " $0 --sys-only"
exit 1
fi
echo "Will skip all sudo steps."
SKIP_ALL_SUDO="1"
fi
fi
# attempt to change the working directory to where this installer is.
# if nothing is moved this location might be a clone repo...
if [ "$InstallDir" == "" ] ; then
cd "$(dirname "${BASH_SOURCE[0]}")" || { exit_err "Could not change directory for MusicBot installer."; }
fi
echo ""
if [ "${INSTALL_SYS_PKGS}${INSTALL_BOT_BITS}" == "11" ] ; then
echo "Attempting to install required system packages & MusicBot software..."
else
if [ "${INSTALL_SYS_PKGS}${INSTALL_BOT_BITS}" == "10" ] ; then
echo "Attempting to install only required system packages..."
else
echo "Attempting to install only MusicBot and pip libraries..."
fi
fi
echo ""
case $DISTRO_NAME in
*"Arch Linux"*) # Tested working 2024.03.01 @ 2024/03/31
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
# NOTE: Arch now uses system managed python packages, so venv is required.
sudo pacman -Syu
sudo pacman -S curl ffmpeg git jq python python-pip
fi
if [ "$INSTALL_BOT_BITS" == "1" ] ; then
install_as_venv
fi
;;
*"Pop!_OS"* )
case $DISTRO_NAME in
# Tested working 22.04 @ 2024/03/29
*"Pop!_OS 22.04"*)
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install build-essential software-properties-common \
unzip curl git ffmpeg libopus-dev libffi-dev libsodium-dev \
python3-pip python3-dev jq -y
fi
if [ "$INSTALL_BOT_BITS" == "1" ] ; then
pull_musicbot_git
fi
;;
*"Pop!_OS 24.04"*)
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install build-essential software-properties-common \
unzip curl git ffmpeg libopus-dev libffi-dev libsodium-dev \
python3-full python3-pip python3-venv python3-dev jq -y
fi
if [ "$INSTALL_BOT_BITS" == "1" ] ; then
install_as_venv
fi
;;
*)
echo "Unsupported version of Pop! OS."
exit 1
;;
esac
;;
*"Ubuntu"* )
# Some cases only use major version number to allow for both .04 and .10 minor versions.
case $DISTRO_NAME in
*"Ubuntu 18.04"*) # Tested working 18.04 @ 2024/03/29
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
sudo apt-get update -y
sudo apt-get upgrade -y
# 18.04 needs to build a newer version from source.
sudo apt-get install build-essential software-properties-common \
libopus-dev libffi-dev libsodium-dev libssl-dev \
zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev \
libreadline-dev libsqlite3-dev libbz2-dev \
unzip curl git jq ffmpeg -y
build_python
fi
if [ "$INSTALL_BOT_BITS" == "1" ] ; then
pull_musicbot_git
fi
;;
# Tested working:
# 20.04 @ 2024/03/28
# 22.04 @ 2024/03/30
*"Ubuntu 20"*|*"Ubuntu 22"*)
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install build-essential software-properties-common \
unzip curl git ffmpeg libopus-dev libffi-dev libsodium-dev \
python3-pip python3-dev jq -y
fi
if [ "$INSTALL_BOT_BITS" == "1" ] ; then
pull_musicbot_git
fi
;;
# Tested working:
# 24.04 @ 2024/09/04
*"Ubuntu 24"*)
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install build-essential software-properties-common \
unzip curl git ffmpeg libopus-dev libffi-dev libsodium-dev \
python3-full python3-pip python3-venv python3-dev jq -y
fi
if [ "$INSTALL_BOT_BITS" == "1" ] ; then
install_as_venv
fi
;;
# Ubuntu version 17 and under is not supported.
*)
echo "Unsupported version of Ubuntu."
echo "If your version is newer than Ubuntu 22, please consider contributing install steps."
exit 1
;;
esac
;;
# NOTE: Raspberry Pi OS 11, i386 arch, returns Debian as distro name.
*"Debian"* )
case $DISTRO_NAME in
*"Debian GNU/Linux 10"*)
if [ "$INSTALL_SYS_PKGS" == "1" ] ; then
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install -y build-essential \
libopus-dev libffi-dev libsodium-dev libssl-dev \